ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javaMail的使用以及trying to connect to host "1xxx@163.com", port 25, isSSL false异常

2019-04-12 18:39:39  阅读:267  来源: 互联网

标签:25 false MimeMessage session static import msg mail isSSL


最近项目用到邮件系统,开始了解javaMail。。。话不多说先上代码:

pom依赖:

    <!--    邮件  https://mvnrepository.com/artifact/javax.mail/mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>

工具类:

 import java.util.Date;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtils {
    //发件人地址
    private static String senderAddress = "1234567891@163.com";
    //收件人地址
    private static String recipientAddress  = "123456789@qq.com";
    //发件人账户
    public static String senderAccount = "1234567891@163.com";
    //发件人授权码
    public static String auth = "xxxxxxx";
    //邮件主题
    public static String subject = "xxxxxx通知";
    //邮件正文
    public static String content = "您有一则xxxxx!";
    
    public static void main(String [] args) throws MessagingException {
        //1.连接邮件服务器的配置参数
        Properties properties = new Properties();
        //设置用户认证方式
        properties.setProperty("mail.smtp.auth", "true");
        //设置传输协议
        properties.setProperty("mail.transport.protocol", "smtp");
        //设置发件人的SMTP服务器地址
        properties.setProperty("mail.smtp.host", "smtp.163.com");
        //properties.setProperty("mail.smtp.port", "465");
        
        //2.创建定义整个应用程序所需的环境信息的Session对象
        Session session = Session.getInstance(properties);
        session.setDebug(true);
        //3.创建邮件的实例对象
        MimeMessage msg = getMimeMessage(session);
        //4.根据session对象获取邮件传输对象
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.163.com", senderAccount, auth);
        //发送邮件,并发送到所有收件人地址,message.getAllRecipients()获取到创建又见对象时添加的所有收件人抄送人密送人
        transport.sendMessage(msg, msg.getAllRecipients());
        //如果只想发送给指定的人,可以如下写法
        //transport.sendMessage(msg, new Address[]{new InternetAddress("xxx@qq.com")});
        //5.关闭邮件链接
        transport.close();
        
    }
    
        /**
         * 獲得創建一封郵件的實例對象
         * @param session
         * @return
         * @throws MessagingException
         * @throws AddressException
         */
        public static MimeMessage getMimeMessage(Session session) throws MessagingException {
            //創建郵件的實例對象
            MimeMessage msg = new MimeMessage(session);
            //设置发件人地址
            msg.setFrom(new InternetAddress(senderAddress));
            
            /*
             * 设置收件人地址(可以增加多个收件人,抄送人,密送)
             * MimeMessage.RecipientType.TO 发送
             * MimeMessage.RecipientType.CC 抄送
             * MimeMessage.RecipientType.BCC 密送
             */
            msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recipientAddress));
            //设置邮件主题
            msg.setSubject(subject, "UTF-8");
            //设置邮件正文
            msg.setContent(content, "text/html;charset=UTF-8");
            //设置邮件的发送时间默认立即发送
            msg.setSentDate(new Date());
            return msg;
        }
}

需要注意首先发件人邮箱需要设置开启POP3/SMTP/IMAP协议

 transport.connect("smtp.163.com", senderAccount, auth);这里的参数注意第三个不是密码

标签:25,false,MimeMessage,session,static,import,msg,mail,isSSL
来源: https://www.cnblogs.com/qq642193463/p/10691516.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有