第一步,在pom文件下加入导包:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.3</version>
</dependency>
第二步,复制MailUtil,把xxxxxxx@qq.com换成你的qq邮箱,shouquanma换成你的授权码
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
public class MailUtil {
public static void sendQQMail(String getterMail, String title, String contents) throws AddressException, MessagingException {
String senderMail = "xxxxxxxxx@qq.com"; //你的qq邮箱
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress(senderMail));
// 设置收件人邮箱地址
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(getterMail)});
//message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@qq.com"));//一个收件人
// 设置邮件标题
message.setSubject(title);
// 设置邮件内容
message.setText(contents);
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect("xxxxxxxxx@qq.com", "shouquanma");// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
public static void send163Mail(String getterMail, String title, String contents) throws IOException {
String USER = ""; // 用户名
String PWD = ""; // 163的授权码
String senderMail = "xxxxx@163.com"; //你的163邮箱
String HOST = "smtp.163.com"; // smtp服务器
String FROM = senderMail; // 发件人地址
String AFFIX = ""; // 附件地址
String AFFIXNAME = ""; // 附件名称
String SUBJECT = title; // 邮件标题
String[] TOS = {getterMail};
Properties props = new Properties();
props.put("mail.smtp.host", HOST);//设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.auth", "true"); //需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.transport.protocol", "smtp");// 连接协议
props.put("mail.smtp.port", 465);// 端口号
props.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
props.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
Session session = Session.getDefaultInstance(props);//用props对象构建一个session
session.setDebug(true);
MimeMessage message = new MimeMessage(session);//用session为参数定义消息对象
try {
message.setFrom(new InternetAddress(FROM));// 加载发件人地址
InternetAddress[] sendTo = new InternetAddress[TOS.length]; // 加载收件人地址
for (int i = 0; i < TOS.length; i++) {
sendTo[i] = new InternetAddress(TOS[i]);
}
message.addRecipients(Message.RecipientType.TO,sendTo);
message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(FROM));//设置在发送给收信人之前给自己(发送方)抄送一份,不然会被当成垃圾邮件,报554错
message.setSubject(SUBJECT);//加载标题
Multipart multipart = new MimeMultipart();//向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
BodyPart contentPart = new MimeBodyPart();//设置邮件的文本内容
contentPart.setText(contents);
multipart.addBodyPart(contentPart);
if(!AFFIX.isEmpty()){//添加附件
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(AFFIX);
messageBodyPart.setDataHandler(new DataHandler(source));//添加附件的内容
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();//添加附件的标题
messageBodyPart.setFileName("=?GBK?B?"+ enc.encode(AFFIXNAME.getBytes()) + "?=");
multipart.addBodyPart(messageBodyPart);
}
message.setContent(multipart);//将multipart对象放到message中
message.saveChanges(); //保存邮件
Transport transport = session.getTransport("smtp");//发送邮件
transport.connect(HOST, USER, PWD);//连接服务器的邮箱
transport.sendMessage(message, message.getAllRecipients());//把邮件发送出去
transport.close();//关闭连接
} catch (Exception e) {
e.printStackTrace();
}
}
}
第三步测试:
MailUtil.sendQQMail("shoujianren@qq.com", "警告,你的服务器磁盘空间不足90%!", "请及时清理");