阿里云服务器不能发送smtp协议邮件问题

1 篇文章 0 订阅
1 篇文章 0 订阅

遇到一个大大的坑。。。
之前在一个阿里云的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;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值