package com.bofide.common.util.sendMsg;
import com.bofide.bsp.po.marketModule.MarketModuleBase;
import com.bofide.common.util.StringUtil;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* @author wangn
* @date 2020/9/17-15:48
* @description 邮件消息
**/
public class EmailMsg {
//发送账号
private static final String FORM = "xxxxx@163.com";
//授权码
private static final String AUTH = "xxxxxxx";
/**
* 描述:发送邮件公共方法
* @author wangn
* @date 2020/9/18-10:56
* @param [title, emailAddress, emailMsg]
* 标题,发送人,邮件内容
* @return boolean
*/
public static boolean sendMail(String emailAddress,String title, String emailMsg) throws Exception {
// 谁发送(发送方)
String from = FORM;
// 发给谁
String to = emailAddress;
// 发送者的用户名和密码(邮箱登录用)
final String username = FORM; // 此处填写发送的邮箱名
final String password = AUTH; // 此处填写登录的邮箱密码
// 定义properties对象,设置环境信息
Properties properties = new Properties();
/*
* mail.smtp.host :指定连接的邮件服务器的主机名。如:163邮箱就填写smtp.163.com
* 若在本地测试的话,需要在本地安装smtp服务器
*/
properties.setProperty("mail.smtp.host", "smtp.163.com");
// mail.smtp.auth:指定客户端是否要向邮件服务器提交验证
properties.setProperty("mail.smtp.auth", "true");
/*
* mail.transport.protocol:指定邮件发送协议:smtp。smtp:发邮件;pop3:收邮件
* mail.store.protocol:指定邮件接收协议
*/
properties.setProperty("mail.transport.protocol", "smtp");
// QQ邮箱设置SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 获取session对象
Session session = Session.getInstance(properties);
// 当设置为true,JavaMail AP就会将其运行过程和邮件服务器的交互命令信息输出到console中,用于JavaMail的调试
session.setDebug(false);
try {
// 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 设置邮件发送方
message.setFrom(new InternetAddress(from));
// 设置邮件发送的主题<邮件标题>
if (StringUtil.isEmpty(title)){
title = "来自博福的邮件";
}
message.setSubject(title);
//添加抄送
Address address = new InternetAddress(FORM);
message.setRecipient(Message.RecipientType.CC, address);
// 设置邮件发送的内容
message.setContent(emailMsg, "text/html;charset=utf-8");
Transport transport = session.getTransport();
// 连接邮件服务器,“”中填写邮件服务器主机名
transport.connect("smtp.163.com", 465, username, password);
transport.sendMessage(message, new Address[]{new InternetAddress(to)});
transport.close();
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
public static String getContent(String str1,String str2,String str3,String str4,MarketModuleBase marketModuleBase){
String content = str1+"<br>" +
"尊敬的"+str2+"您好:<br>" +
str3+"<br>";
if ("0".equals(marketModuleBase.getInvitation())){
content +="如有意向,请进入公众号发起预约<br>";
}
if ("0".equals(marketModuleBase.getQuotedPrice())){
content +="如有需要,请进入公众号查询报价<br>";
}
if ("0".equals(marketModuleBase.getCardCertificate())&& StringUtil.isNotEmpty(str4)){
content +="赠送您卡券<br>" + str4 +"<br>"+
"已放置您的卡包,请查收<br>" ;
}
if (marketModuleBase.getQuestionnaires().size() > 0){
content+="有份问卷在等您哦,请进入公众号回答问卷<br>";
}
content+= "如有问题,请进入公众号联系客服";
System.out.println("===============>邮件内容:"+content);
return content;
}
}
做了一个邮件发送功能,记录一下方便下次使用
中途遇见了报错
com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163 smtp8,DMCowAA3eeRKaLxf19DsDQ--.17993S2 1606182986,please see http://mail.163.com/help/help_spam_16.htm?ip=47.93.88.80&hostid=smtp8&time=1606182986
根据网上搜到的结果说是需要添加一份抄送自己,还是报错.后面检测到自己的title(也就是邮件的主题为空).开始的时候是可以发送的后面不知道什么原因发送不了了.就单独处理了一下title.如果为空就默认给他一个.
最早之前没有填写title时 是这样的,后面开始报错了 估计被认为是一个垃圾邮件了吧