最近有朋友问邮件怎么发送,就简单写了个demo,因为懒得找jar包,所以项目是创建的maven工程,具体的maven引用的jar如下:
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.4.4</version></dependency><!-- https://mvnrepository.com/artifact/javax.mail/mail --><dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version></dependency>
然后代码见下:
因为QQ是https所以中间多了一段,还有就是如果自己搭的邮件服务器可以填写密码,而不用授权码了,这里我是用的QQ邮箱,所以邮箱要先开启SMTP/POP3服务
package com.allan.until;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import com.sun.mail.util.MailSSLSocketFactory;public class MailUtil { private String email;// 收件人邮箱 private String code;// 激活码 public MailUtil(String email, String code) { this.email = email; this.code = code; } public void sendMail() { // 1.创建连接对象javax.mail.Session // 2.创建邮件对象 javax.mail.Message // 3.发送一封激活邮件 String from = "XXX@qq.com";// 发件人电子邮箱 String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易) //Properties properties = System.getProperties();// 获取系统属性 Properties properties =new Properties(); properties.setProperty("mail.smtp.host", host);// 设置邮件服务器 properties.setProperty("mail.smtp.auth", "true");// 打开认证 try { //QQ邮箱需要下面这段代码,163邮箱不需要 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf); // 1.获取默认session对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("XXX@qq.com", "XXX"); // 发件人邮箱账号、授权码 } }); // 2.创建邮件对象 Message message = new MimeMessage(session); // 2.1设置发件人 message.setFrom(new InternetAddress(from)); // 2.2设置接收人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(email)); // 2.3设置邮件主题 message.setSubject("账号激活"); // 2.4设置邮件内容 String content = "验证码:"+ code; message.setContent(content, "text/html;charset=UTF-8"); // 3.发送邮件 Transport.send(message); System.out.println("邮件成功发送!"); } catch (Exception e) { e.printStackTrace(); } }}