https://blog.csdn.net/BO688/article/details/100628287
优点:可用
缺点:需要qq授权
希望:利用自己的域名发送
https://blog.csdn.net/BO688/article/details/104461740
优点:可以利用自己域名,发送到qq或者163等邮箱 ------用java代码和Foxmail都可以发送
缺点:有时会产生很多log日志文件,而且不能发送,重启之后可以发送出去
希望:不会出现堵塞和日志问题
启动时一闪而过解决
james的问题
如何启动并且配置发送邮件
Ps:在foxmail测试时把stmp和pop填成 域名就好,填成stmp.域名反而出错
优点:foxmail可以使用域名发送
缺点:java 发送只能发送内网 ,发送外网就会出现下面的问题
550 repay deny(网上找到是需要开启stmp验证)
就是 535 认证失败(但是开启验证就通过不了)
希望:能访问外网
https://blog.csdn.net/tencentmail/article/details/88988786
https://blog.csdn.net/weixin_30246221/article/details/101356992
到了这里感觉是又回到了最初的起点…
这里感觉是腾讯那里用域名发送了qq邮件…
但是终归实现了我的要求…
缺点:需要注册企业邮箱
下面贴一下自己的修改后的代码
这个是一些上面链接代码的修改封装
private static String emailRegular = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";//校验邮箱是否正确
public static boolean validEmail(String emailAddress) {//判断返回对错
return Pattern.matches(emailRegular, emailAddress);
}
private static String fromMailAddress = "****@****88.cn";//发贱人邮件地址
private static String name = "admin";//自己名字
private static String fromMailPwd = "******";//密码
private static String fromMailSMTPHost = "smtp.exmail.qq.com";//stmp服务器
private static String smtpPort = "465";//端口
private static Properties setProperties(String debug) {
Properties properties = new Properties();//配置
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", fromMailSMTPHost);
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");//变种协议
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
properties.setProperty("mail.smtp.ssl.enable", "true");
properties.setProperty("mail.debug", debug);
return properties;
}
private static Session setSession(boolean debug) {
Properties properties = setProperties(String.valueOf(debug));
Session session = Session.getInstance(properties);
session.setDebug(debug);
return session;
}
private static Message setMail(Session session, String[] toEmailAdresses,String[] names, String emailTitle, String emailContent) throws Exception {
MimeMessage message = new MimeMessage(session);//获得邮件
message.setFrom(new InternetAddress(fromMailAddress, name, "UTF-8"));//设置发件人
setallRecipients(message,toEmailAdresses,names);
message.setSubject(emailTitle);//标题
message.setContent(emailContent, "text/html;charset=utf-8");//内容
message.setSentDate(new Date());//发送时间
message.saveChanges();//保存
return message;
}
private static void setallRecipients(MimeMessage message,String[] mailaddress,String []name)throws Exception{
for(int a=0;a< mailaddress.length;a++){//把收件人邮箱数组加入其中
System.out.println(mailaddress[a]);
if(mailaddress[a]!=null)//判断是否为空
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailaddress[a], name[a], "UTF-8"));
}
}
public static boolean sendEmail(String[] toEmailAdress,String[] name, String emailContent, String emailTitle,boolean debug) {//地址,对方的名字,内容,标题,debug
Session session = setSession(debug);
try {
Message message = setMail(session, toEmailAdress,name, emailTitle, emailContent);
Transport transport = session.getTransport();
transport.connect(fromMailAddress, fromMailPwd);//链接
transport.sendMessage(message, message.getAllRecipients());//发送
transport.close();
} catch (Exception e) {
e.printStackTrace();
for(String E :toEmailAdress){
System.out.println("toEmailAdress = " + E + ", emailContent = " + emailContent + ", emailTitle = " + emailTitle + ", error = " + e.getMessage());
}
return false;
}
return true;
}
相关调用
@RequestMapping("/mail")
public void mail(HttpServletRequest request, HttpServletResponse response) throws IOException {
String next;
boolean checkmail=true;//默认邮箱格式正确
int i;
String address[]=new String[5];//限制最多发送5个
String inputname=request.getParameter("name");//判断前端是否设置名字
String name[]={inputname,inputname,inputname,inputname,inputname};//这里需要和邮箱地址一样对应个数 对方名字
String first=request.getParameter("receiver");//获得第一个邮箱地址
if(first!=null&&first!=""&&validEmail(first)){//判断
address[0]=first;
}else{
checkmail=false;
}
for (i=1;i<=4;i++){/*设置下一个发送对象,从第二个开始*/
next=request.getParameter("receiver"+i);
if(next==null||next==""){break;}/*循环非null的*/
else {
if(validEmail(next)){
address[i]=next;
}else{
checkmail=false;
}
}
}
if(!checkmail){//判断是否出现格式错误
response.getWriter().println("邮箱格式错误");
}
if( sendEmail(address,name,"hello","hello",true)){
response.getWriter().println("已发送");
}else {
response.getWriter().println("发送失败");
}
response.setHeader("refresh", "3;url=mailhtml");
response.getWriter().println("
三秒后跳转......");
}