java实现邮件发送详解
更新时间:2020年03月30日 08:57:46 作者:瑾年丶深蓝
这篇文章主要为大家详细介绍了java实现邮件发送示例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java实现邮件发送逻辑并不复杂(不包含附件),只是根据官方调用官方提供的sdk,首先需要引入maven依赖:
javax.mail
<dependency > <groupId >com.sun.mail</groupId > <artifactId >javax.mail</artifactId > <version >1.6.0</version > </dependency >
然后构造发送邮件所需的实体类
package com.email; import java.io.Serializable; /** * @Author zjt * @Date 2019年03月07 10:37 */ public class EmailEntity implements Serializable { private static final long serialVersionUID = 1L; //邮箱服务器地址 private String host; //主机端口 private Integer port; //发送者的邮箱账号 private String userName; //发送者的密码 private String password; //发送者的邮箱地址 private String fromAddress; //接收者的邮箱地址 private String toAddress; //设置邮件主题 private String subject; //设置邮件内容 private String context; //设置邮件类型 private String contextType; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public Integer getPort() { return port; } public void setPort(Integer port) { this.port = port; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public String getContextType() { return contextType; } public void setContextType(String contextType) { this.contextType = contextType; } }
其次,编写调用邮件发送方法
package com.email; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.*; /** * @Author zjt * @Date 2019年03月07 10:38 */ public class EmailSend { public static boolean EmailSendTest(EmailEntity emailEntity){ try { //配置文件 Properties properties = new Properties(); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.host", emailEntity.getHost()); properties.put("mail.smtp.port", 25); properties.put("mail.smtp.starrttls.enable", "true"); //创建会话 VerifyEmail verifyEmail = new VerifyEmail(emailEntity.getUserName(), emailEntity.getPassword()); Session mailSession = Session.getInstance(properties, verifyEmail); mailSession.setDebug(true); //创建信息对象 Message message = new MimeMessage(mailSession); InternetAddress from = new InternetAddress(emailEntity.getFromAddress()); InternetAddress to = new InternetAddress(emailEntity.getToAddress()); //设置邮件信息的来源 message.setFrom(from); //设置邮件的接收者 message.setRecipient(MimeMessage.RecipientType.TO, to); message.setSubject(emailEntity.getSubject()); //设置邮件发送日期 message.setSentDate(new Date()); //设置邮件内容 message.setContent(emailEntity.getContext() , emailEntity.getContextType()); message.saveChanges(); //发送邮件 Transport transport = mailSession.getTransport("smtp"); transport.connect(emailEntity.getHost(), emailEntity.getUserName(), emailEntity.getPassword()); System.out.println("发送:" + transport); transport.sendMessage(message, message.getAllRecipients()); System.out.println("success"); return true; } catch (MessagingException e) { e.printStackTrace(); System.out.println("fial..."); return false; } } }
在调用邮件发送方法中使用到验证邮箱登录名和密码是否正确的方法
package com.email; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * 验证邮箱 * @Author zjt * @Date 2019年03月07 10:32 */ public class VerifyEmail extends Authenticator { //账号 private String userName; //密码 private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } //构造方法 public VerifyEmail(){ super(); } public VerifyEmail(String userName, String password) { super(); this.userName = userName; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName, password); } }
编写测试类,测试邮件发送方法是否成功
package com.email; import org.junit.jupiter.api.Test; /** * @Author zjt * @Date 2019年03月07 10:26 */ public class TestEmail { @Test public void test(){ EmailEntity email = new EmailEntity(); email.setUserName("*******@163.com"); email.setPassword("******"); email.setHost("smtp.163.com"); email.setPort(25); email.setFromAddress("******@163.com"); email.setToAddress("******@163.com"); email.setSubject("这是一封测试邮件!!!!"); email.setContext("看看这是什么"); email.setContextType("text/html;charset=utf-8"); boolean flag = EmailSend.EmailSendTest(email); System.err.println("邮件发送结果=="+flag); } }
在这里测试的163邮箱发送,需要注意的是,此处的密码不是登录密码呦,而是设置中客户端授权密码呦。
执行测试文件之后,可以登录邮箱看到发送的结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
springboot前后端分离集成CAS单点登录(统一认证)
单点登录是一种身份认证和授权技术,允许用户在多个应用系统中使用同一套用户名和密码进行登录,本文主要介绍了springboot前后端分离集成CAS单点登录,具有一定的参考价值,感兴趣的可以了解一下2024-09-09解析Mybatis SqlSessionFactory初始化原理
本文主要介绍了Mybatis SqlSessionFactory初始化原理,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧2021-07-07
最新评论