zoukankan      html  css  js  c++  java
  • 阿里云服务器 发送邮箱 STMP 25端口 465端口问题 Javamail 25被禁用

    我们传统使用的比较简单的是 STMP 25端口收发邮件

    今天发现刚购买的阿里云服务器不能作为客户端通过STMP 25端口发送邮件

    开始在网上有说发现是JDK1.8的原因,然后自己也把JDK1.8换到了JDK1.7 还是不行,所以这里排除了JDK的原因。

    那么问题来了,是否25端口不能连接适用喃?

    然后在终端输入命令行 ,可测试25端口是否可连接。

    telnet smtp.163.com 25 

    返回成功 说明你的服务器 是没有封掉25端口的。上面的图是我自己的电脑,当然能连通。

    当换到服务器测试的时候 就会出现一只连接不成功 

    找了很久的问题,突然在阿里云客服社区发现一个帖子,当中有客服的解释,当然我不是第一个遇到这个问题的人。

    那么客服的解释一下就说明的问题:好像是将25端口封掉了 时间是2016年9月底以后新购买的服务器。

    那么问题找到了 那就需要将25端口换到465端口 采用SSL协议传输邮件。

    好了贴代码时间到:

    下面这段代码是采用SSL协议发送邮件,可向多人单人发送邮件

    import java.io.File;
    import java.io.UnsupportedEncodingException;
    import java.security.Security;
    import java.util.Date;
    import java.util.Map;
    import java.util.Properties;
    
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Authenticator;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    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;
    import javax.mail.internet.MimeUtility;
    
    
    public class MailUtil {
    
        private static final String FROM_MAIL_SMTP = "smtp.exmail.qq.com";
        private static final String FROM_MAIL_NAME = "aaaaaa@qq.com";
        private static final String FROM_MAIL_PASS = "xxxxxxx";
    
        
        /**
         * 发送邮件(灵活度高,通用版)
         * @param from 发件人
         * @param to 收件人, 多个Email以英文逗号分隔
         * @param cc 抄送, 多个Email以英文逗号分隔
         * @param subject 主题
         * @param content 内容
         * @param fileList 附件列表
         * @return
         */
        public static boolean sendMail(String to, String cc, String subject, String content, String[] fileList){
             try {
                 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                 final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
                 final Properties p = System.getProperties() ;
                 p.setProperty("mail.smtp.host", FROM_MAIL_SMTP);
                 p.setProperty("mail.smtp.auth", "true");
                 p.setProperty("mail.smtp.user", FROM_MAIL_NAME);
                 p.setProperty("mail.smtp.pass", FROM_MAIL_PASS);
                 
                 p.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
                 p.setProperty("mail.smtp.socketFactory.fallback", "false");
                 //邮箱发送服务器端口,这里设置为465端口
                 p.setProperty("mail.smtp.port", "465");
                 p.setProperty("mail.smtp.socketFactory.port", "465");
        
                // 根据邮件会话属性和密码验证器构造一个发送邮件的session
                Session session = Session.getInstance(p, new Authenticator(){
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
                    }
                });
                session.setDebug(true);
                Message message = new MimeMessage(session);
                //消息发送的主题
                message.setSubject(subject);
                //接受消息的人
                message.setReplyTo(InternetAddress.parse(FROM_MAIL_NAME));
                //消息的发送者
                message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"测"));
                // 创建邮件的接收者地址,并设置到邮件消息中
                String[] split = to.split(",");
                InternetAddress []tos = new InternetAddress[split.length];
                for (int i = 0; i < split.length; i++) {
                    tos[i]=new InternetAddress(split[i]);
                }
                // 设置抄送人
                if (cc != null && cc.length() > 0) {
                    message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); 
                }
                message.setRecipients(Message.RecipientType.TO, tos);
                // 消息发送的时间
                message.setSentDate(new Date());
                Multipart mainPart = new MimeMultipart();
                // 创建一个包含HTML内容的MimeBodyPart
                BodyPart html = new MimeBodyPart();
                // 设置HTML内容
                html.setContent(content, "text/html; charset=utf-8");
                mainPart.addBodyPart(html);
                // 将MiniMultipart对象设置为邮件内容
                message.setContent(mainPart);
                // 设置附件
                if (fileList != null && fileList.length > 0) {
                    for (int i = 0; i < fileList.length; i++) {
                        html = new MimeBodyPart();
                        FileDataSource fds = new FileDataSource(fileList[i]); 
                        html.setDataHandler(new DataHandler(fds)); 
                        html.setFileName(MimeUtility.encodeText(fds.getName(), "UTF-8", "B"));
                        mainPart.addBodyPart(html); 
                    }
                }
                message.setContent(mainPart); 
                message.saveChanges(); 
                Transport.send(message);
             } catch (MessagingException e) {
                 e.printStackTrace();
                 return false;
             } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }

    如果是用Springboot配合文件,则需要加入以下配置:

    properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
    spring.mail.properties.mail.smtp.port=465
    spring.mail.properties.mail.socketFactory.port=465

    OK当我们使用465端口SSL协议的时候,顺利的在服务器上发出邮件了。

  • 相关阅读:
    从零开始——PowerShell应用入门(全例子入门讲解)
    详解C# Tuple VS ValueTuple(元组类 VS 值元组)
    How To Configure VMware fencing using fence_vmware_soap in RHEL High Availability Add On——RHEL Pacemaker中配置STONITH
    DB太大?一键帮你收缩所有DB文件大小(Shrink Files for All Databases in SQL Server)
    SQL Server on Red Hat Enterprise Linux——RHEL上的SQL Server(全截图)
    SQL Server on Ubuntu——Ubuntu上的SQL Server(全截图)
    微软SQL Server认证最新信息(17年5月22日更新),感兴趣的进来看看哟
    Configure Always On Availability Group for SQL Server on RHEL——Red Hat Enterprise Linux上配置SQL Server Always On Availability Group
    3分钟带你了解PowerShell发展历程——PowerShell各版本资料整理
    由Find All References引发的思考。,
  • 原文地址:https://www.cnblogs.com/xk920/p/12008717.html
Copyright © 2011-2022 走看看