阿里云服务器不能发送smtp协议邮件问题
来源:互联网 发布:js date对象方法 编辑:程序博客网 时间:2024/05/18 00:40
遇到一个大大的坑。。。
之前在一个阿里云的WindowsServer可以正常发送,本地开发环境也可以,换到阿里云的centos环境下就出问题了,总是提示
一番查找之后,定位问题为:阿里云自去年之后购买的服务器均屏蔽掉了25端口,只能转而使用smtps协议的方式发送邮件。
扔代码
import java.security.Security;import java.util.Date;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.StringUtils;public class EmailService { Logger logger = LoggerFactory.getLogger(this.getClass()); private String auth=null,host=null, user=null; private static String password=null; public EmailService(String auth,String host,String user,String password){ this.auth = auth; this.host = host; this.user = user; this.password = password; } public EmailService(){ } public void sendEmail(String toemail,String subject,String content,String type) throws Exception{ if (!StringUtils.hasText(auth)||!StringUtils.hasText(host)||!StringUtils.hasText(user)||!StringUtils.hasText(password)) { throw new RuntimeException("参数错误!new对象的时候请传入auth、host、user、password参数,或通过set方法设置。"); } sendMailBySSL(host, user, password, toemail, subject,content); } public static boolean sendMailBySSL(String smtp,String username,String password,String tousername,String subject,String content) throws AddressException, MessagingException{ Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // Get a Properties object Properties props = new Properties(); props.setProperty("mail.smtp.host", smtp); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }}); // -- Create a new message -- Message msg = new MimeMessage(session); // -- Set the FROM and TO fields -- msg.setFrom(new InternetAddress(username)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(tousername,false)); msg.setSubject(subject); msg.setText(content);//发送纯文本格式 //msg.setContent(content, "text/html;charset = gbk");//发送网页格式 msg.setSentDate(new Date()); Transport.send(msg); System.out.println("Message sent."); return true; } public static void main(String[] args) throws AddressException, MessagingException { String subject = "11111131111111"; String content = "11111131111111"; String username = "system@ixincheng.com"; password = "********"; String tousername = "yuruixin_china@163.com"; String smtp = "smtp.ixincheng.com"; sendMailBySSL(smtp, username, password, tousername, subject, content); } public String getAuth() { return auth; } public void setAuth(String auth) { this.auth = auth; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
阅读全文
0 0
- 阿里云服务器不能发送smtp协议邮件问题
- 阿里云发送SMTP邮件失败
- SMTP协议发送邮件
- Java调用阿里SMTP服务器实现邮件发送
- 使用SMTP协议发送邮件
- SMTP邮件发送协议分析
- 采用smtp协议发送邮件
- 使用smtp协议发送邮件
- 阿里云服务器用smtp发送邮件返回错误
- C#smtp服务器发送邮件
- 邮件服务器-SMTP命令行发送邮件
- 阿里云服务器 discuz不能发邮件
- Python 利用SMTP协议,基于第三方的邮件服务器发送邮件
- C语言实现SMTP协议发送邮件
- Java采用SMTP协议发送邮件
- smtp协议 VC实现发送邮件
- SMTP协议发送邮件和附件
- System.Net.Mail 发送邮件 SMTP协议
- 数据结构——队列(queue)
- SQL技巧:如何统计博客每天的总点击量和每天的总点击人数
- ECharts 柱状图动态获取json数据
- play1跳转指定其他模板
- spring aop JoinPoint 用法
- 阿里云服务器不能发送smtp协议邮件问题
- 又见回文(串)
- Android Framework--启动流程
- win7-----xampp结合php5.6安装redis扩展
- Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
- Python3 学习札记(三)
- 语音增强简介
- 《挑战程序设计竞赛中》所讲的超大背包问题
- VS 远程调试