java实现邮件发送详解

时间:2021-05-19

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邮箱发送,需要注意的是,此处的密码不是登录密码呦,而是设置中客户端授权密码呦。

执行测试文件之后,可以登录邮箱看到发送的结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章