使用javax.mail包下的类实现发送邮件工具类
Posted cyoba
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用javax.mail包下的类实现发送邮件工具类相关的知识,希望对你有一定的参考价值。
IDEA 依赖:
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
模板实体类:
package springbootdemo.demo.entity; /** * @author * @date 2019/8/19 21:58 */ public class MailTemplate //模板标题 private String mailTitle; //模板内容 private String mailContent; public String getMailTitle() return mailTitle; public void setMailTitle(String mailTitle) this.mailTitle = mailTitle; public String getMailContent() return mailContent; public void setMailContent(String mailContent) this.mailContent = mailContent;
邮件配置类:
1 package springbootdemo.demo.entity; 2 3 /** 4 * @author 5 * @date 2019/8/19 21:26 6 */ 7 public class Email 8 //发送邮箱 9 private String mailUser; 10 //认证用户 11 private String agreedUser; 12 //认证密码,授权码(不是登录密码) 13 private String agreedPwd; 14 //端口号 15 private String port; 16 //邮箱服务/主机(163邮箱的是smtp协议的:smtp.163.com) 17 private String host; 18 //接收邮箱 19 private String receiverUser; 20 //发送协议smtp, 21 private String protocol; 22 //自定义发送名称 23 private String mailUserName; 24 //是否开启ssl验证 25 private String ssl; 26 //是否开启tls 27 private String starttls; 28 //是否同时开启了认证 29 private String auth; 30 31 public String getMailUser() 32 return mailUser; 33 34 35 public void setMailUser(String mailUser) 36 this.mailUser = mailUser; 37 38 39 public String getAgreedUser() 40 return agreedUser; 41 42 43 public void setAgreedUser(String agreedUser) 44 this.agreedUser = agreedUser; 45 46 47 public String getAgreedPwd() 48 return agreedPwd; 49 50 51 public void setAgreedPwd(String agreedPwd) 52 this.agreedPwd = agreedPwd; 53 54 55 public String getPort() 56 return port; 57 58 59 public void setPort(String port) 60 this.port = port; 61 62 63 public String getHost() 64 return host; 65 66 67 public void setHost(String host) 68 this.host = host; 69 70 71 public String getReceiverUser() 72 return receiverUser; 73 74 75 public void setReceiverUser(String receiverUser) 76 this.receiverUser = receiverUser; 77 78 79 public String getProtocol() 80 return protocol; 81 82 83 public void setProtocol(String protocol) 84 this.protocol = protocol; 85 86 87 public String getMailUserName() 88 return mailUserName; 89 90 91 public void setMailUserName(String mailUserName) 92 this.mailUserName = mailUserName; 93 94 95 public String getSsl() 96 return ssl; 97 98 99 public void setSsl(String ssl) 100 this.ssl = ssl; 101 102 103 public String getStarttls() 104 return starttls; 105 106 107 public void setStarttls(String starttls) 108 this.starttls =starttls; 109 110 111 public String getAuth() 112 return auth; 113 114 115 public void setAuth(String auth) 116 this.auth = auth; 117 118
发送邮件工具类:
package springbootdemo.demo.util; import org.springframework.util.StringUtils; import springbootdemo.demo.entity.Email; import springbootdemo.demo.entity.MailTemplate; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties; /** * @author * @date 2019/8/19 18:48 */ public class EmailUtil /** * * @description: 发送邮件 * @author: * @date: 2019/8/19 * @return: */ public static boolean sendEmail(Email email, MailTemplate mailTemplate) Properties properties = new Properties(); properties.put("mail.smtp.host", email.getHost()); properties.put("mail.smtp.ssl.enable", email.getSsl()); properties.put("mail.smtp.auth", email.getAuth()); properties.put("mail.transport.protocol", email.getProtocol()); properties.put("mail.smtp.starttls.enable", email.getStarttls()); Session session = Session.getDefaultInstance(properties); //设置debug,可以查看详细查看log session.setDebug(true); // 由邮件会话新建一个消息对象 ps:注意,这个session是final修饰的,只会初始化一次,相同的邮箱用户如果修改了配置,比如ssl校验,必须重启项目才可以 MimeMessage message = new MimeMessage(session); try /*设置发送邮箱*/ message.setFrom(new InternetAddress(email.getMailUser())); //设置接收者,类型为TO message.setRecipients(Message.RecipientType.TO, email.getReceiverUser()); //设置标题 message.setSubject(mailTemplate.getMailTitle()); //html样式 String content ="<div><p>亲爱的"+email.getMailUserName()+":</p> <p>你有一份新通知</p> <p>"+mailTemplate.getMailContent()+"</p> </div>"; //纯文本 //message.setText(mailTemplate.getMailContent()); /* html文本*/ message.setContent(content, "text/html;charset=gbk"); //new Date返回当前时间的对象,设置发送时间 message.setSentDate(new Date()); //保存发送信息 message.saveChanges(); //设置发送 Transport transport = session.getTransport(); if (StringUtils.isEmpty(email.getPort())) //默认端口号为465 transport.connect(email.getAgreedUser(), email.getAgreedPwd()); else transport.connect(email.getHost(), Integer.valueOf(email.getPort()), email.getAgreedUser(), email.getAgreedPwd()); //发送邮箱,第二参数是接收地址 transport.sendMessage(message, message.getAllRecipients()); //通过流发送,记得关闭流 transport.close(); System.out.println("发送成功"); catch (MessagingException e) e.printStackTrace(); return false; public static void main(String[] args) Email email=new Email(); email.setAgreedPwd("认证密码"); email.setAgreedUser("认证用户(一般都不用填)"); email.setAuth("true"); email.setHost("smtp.163.com"); email.setMailUser("发送邮箱"); email.setMailUserName("发送用户"); email.setReceiverUser("接收邮箱"); email.setPort(""); email.setProtocol("smtp"); email.setSsl("true"); email.setStarttls("false"); MailTemplate mailTemplate=new MailTemplate(); mailTemplate.setMailTitle("发送测试"); mailTemplate.setMailContent("发送测试试试"); sendEmail(email,mailTemplate);
另外,邮箱一定要开通认证,163邮箱为例.
以上是关于使用javax.mail包下的类实现发送邮件工具类的主要内容,如果未能解决你的问题,请参考以下文章
[Java]_[初级]_[Observer和Observable失效后如何使用java.beans包下的类来实现观察者模式]