Java javamail发送邮件 使用sina 加密端口发送

2024-06-12 18:38

本文主要是介绍Java javamail发送邮件 使用sina 加密端口发送,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用sina的邮件服务器发送,原来的 

mail.smtp.server=smtp.sina.com
mail.smtp.port=25

后来切换成阿里云服务器之后,发现本来可以发送的邮件发送失败并且报错如下:

java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.sina.com, port: 25;

INFO   | jvm 1    | main    | 2018/10/11 17:02:01.278 |   nested exception is:

INFO   | jvm 1    | main    | 2018/10/11 17:02:01.278 |  java.net.ConnectException: Connection timed out (Connection timed out)

通过调查之后才发现,原来阿里云服务器25端口不能出,被禁了。只能使用Sina的加密端口。更高配置如下:

mail.smtp.server=smtp.sina.com
mail.smtp.port=465

使用加密端口,需要注意的是必须要带上特定的参数才行。

pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

否则回报错如下:

INFO   | jvm 1    | main    | 2018/10/11 17:32:57.548 | Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.sina.com, port: 465, response: -1
INFO   | jvm 1    | main    | 2018/10/11 17:32:57.548 |     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1922) ~[mail-1.4.4.jar:1.4.4]
INFO   | jvm 1    | main    | 2018/10/11 17:32:57.548 | 

上面是说明出现问题的原因和解决办法。下面贴出发送部分邮件部分主要代码:

import ilism.mail.MailAuthenticator;
import ilism.mail.MailSender;
import ilism.mail.MailSenderInfo;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;import org.apache.log4j.Logger;
import org.hsqldb.lib.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;public static void main(String[] args){   
//initialize
MailSenderInfo info  = new MailSenderInfo();
info.setMailServerHost(configurationService.getConfiguration().getString("mail.smtp.server"));//发件箱服务器
info.setMailServerPort(configurationService.getConfiguration().getString("mail.smtp.port"));//发件箱服务器端口
info.setValidate(true);
info.setFromAddress(configurationService.getConfiguration().getString("mail.from"));//from address
info.setUserName(configurationService.getConfiguration().getString("mail.smtp.user"));//用户名
info.setPassword(configurationService.getConfiguration().getString("mail.smtp.password"));//密码
try
{//send emailinfo.setSubject(current.getTitle());info.setContent(current.getContent());info.setToAddress(current.getEmail());if (current.getFileAttachment() != null && !StringUtil.isEmpty(current.getFileAttachment())){//带附件info.setAttachFileNames(new String[]{ current.getFileAttachment() });this.sendHtmlMailWithFileCopy(info);}else{//htmlinfo.setAttachFileNames(null);this.sendHtmlMailCopy(info);}//标记已发送成功current.setHasSendFlag(Boolean.TRUE);modelService.save(current);Thread.sleep(2000);
}
catch (final Exception e)
{if (e instanceof javax.mail.MessagingException){//不能连smtp的等到下一次处理Logger.getLogger(e.getClass()).error("wait for next email send action!", e);}else{throw e;}
}   
}  
//html发送public  boolean sendHtmlMailCopy(MailSenderInfo mailInfo) {MailAuthenticator authenticator = null;Properties pro = mailInfo.getProperties();pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");if (mailInfo.isValidate()) {authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());}Session sendMailSession = Session.getDefaultInstance(pro, authenticator);try {Message mailMessage = new MimeMessage(sendMailSession);Address from = new InternetAddress(mailInfo.getFromAddress());mailMessage.setFrom(from);String[] var9;int var8 = (var9 = mailInfo.getToAddress().split(";")).length;for(int var7 = 0; var7 < var8; ++var7) {String one = var9[var7];mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(one));}mailMessage.setSubject(mailInfo.getSubject());mailMessage.setSentDate(new Date());Multipart mainPart = new MimeMultipart();BodyPart html = new MimeBodyPart();html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");mainPart.addBodyPart(html);mailMessage.setContent(mainPart);Transport transport = sendMailSession.getTransport("smtp");transport.connect(mailInfo.getMailServerHost(), mailInfo.getUserName(), mailInfo.getPassword());transport.sendMessage(mailMessage, mailMessage.getRecipients(Message.RecipientType.TO));transport.close();return true;} catch (MessagingException var10) {Logger.getLogger(var10.getClass()).error("", var10);throw new RuntimeException(var10);}}//带附件发送public  boolean sendHtmlMailWithFileCopy(MailSenderInfo mailInfo) {MailAuthenticator authenticator = null;Properties pro = mailInfo.getProperties();pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");if (mailInfo.isValidate()) {authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());}Session sendMailSession = Session.getDefaultInstance(pro, authenticator);try {Message mailMessage = new MimeMessage(sendMailSession);Address from = new InternetAddress(mailInfo.getFromAddress());mailMessage.setFrom(from);Address to = new InternetAddress(mailInfo.getToAddress());mailMessage.setRecipient(Message.RecipientType.TO, to);mailMessage.setSubject(mailInfo.getSubject());mailMessage.setSentDate(new Date());Multipart mainPart = new MimeMultipart("related");BodyPart html = new MimeBodyPart();html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");mainPart.addBodyPart(html);String[] var12;int var11 = (var12 = mailInfo.getAttachFileNames()).length;for(int var10 = 0; var10 < var11; ++var10) {String file = var12[var10];html = new MimeBodyPart();DataSource fileDataSource = new FileDataSource(file);html.setDataHandler(new DataHandler(fileDataSource));html.setHeader("Content-ID", (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date()));html.setHeader("Content-Disposition", "attachment;filename=" + getFileName(file));mainPart.addBodyPart(html);}mailMessage.setContent(mainPart);Transport.send(mailMessage);return true;} catch (MessagingException var14) {Logger.getLogger(var14.getClass()).error("", var14);throw new RuntimeException(var14);}}private static String getFileName(String content) {content = content.replace('\\', '/');return content.substring(content.lastIndexOf(47) + 1);}
}

这篇关于Java javamail发送邮件 使用sina 加密端口发送的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1055023

相关文章

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib