最近项目用到邮件系统,开始了解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);这里的参数注意第三个不是密码

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

  1. ssh: connect to host gitlab.alpha.com port 22: Network is unreachable

    在这里只说明我遇到的问题和解决方法,可能并不能解决你遇到的问题: git clone git@gitlab.alpha.com:ipcam/ambarella.gitCloning into 'amb ...

  2. $ ssh -T git@github.com ssh: connect to host ssh.github.com port 22: Connection timed out

    在C:/用户/用户名/.ssh中添加几个文件 之前的电脑生成都是四个文件,分别是 id_rsa  id_rsa.pub  config known_hosts 不知道为什么在另一台电脑上却生成两个文件 ...

  3. Could not connect to SMTP host: localhost, port: 25;

    1.错误描写叙述 DEBUG: setDebug: JavaMail version 1.3.3 DEBUG: getProvider() returning javax.mail.Provider[ ...

  4. selenium在Eclipse中打开fireFox浏览器是报报错connect to host 127.0.0.1 on port 7055

    1.相信很多同学刚接触selenium时,在Eclipse中打开fireFox浏览器是报报错: org.openqa.selenium.firefox.NotConnectedException: U ...

  5. Selenium2学习-038-firefox、webdriver版本不对称问题解决:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055

    今天有个朋友在群里问,为何脚本运行不通过,其脚本操作步骤简单描述如下: 1.启动火狐浏览器 2.打开百度 3.查询框输入关键字 4.点击按钮[百度一下] 脚本挺简单的,其给出的应用报错信息如下所示: ...

  6. Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "124.202.213.70" failed: [10054] Connection reset by peer [MsgId: MERR-27780]

    解决方案一: 备注: 此方案如果请求响应时间太长,勾选"WinInet replay instead of Sockets(Windows only)"将会导致如下错误:

  7. 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation

    解决方法为将selenium-server-standalone-2.37.0.jar升级至selenium-server-standalone-2.41.0.jar即可. 下载地址:http://s ...

  8. WebDriver:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms

    今天尝试最新的webDriver与fireFox搭配: 运行代码时出现如下的问题,但是浏览器却可以打开: org.openqa.selenium.firefox.NotConnectedExcepti ...

  9. svn :Can't connect to host *.*.*.*': 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

    Can't connect to host *.*.*.*': 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. -------------------------------- ...

随机推荐

  1. css边框的一些属性

    边框样式值如下:none : 无边框.与任何指定的border-width值无关hidden : 隐藏边框.IE不支持dotted : 在MAC平台上IE4+与WINDOWS和UNIX平台上IE5.5 ...

  2. ==和equals的比较方式

    关系操作符生成的是一个boolean结果,它们计算的是操作数的值之间的关系.——摘自<Java编程思想> 多读几遍之后你会发现——这不就是句废话么233333.(开个玩笑不要打我) 先说 ...

  3. 执行Runtime.exec()需要注意的陷阱

    作为Java语言的一部分.java.lang包被隐藏的导入到每一个Java程序.这个包的表面陷阱,经常影响到大多数程序员.这个月,我将讨论运行时exec()方法时的潜伏陷阱. 陷阱4:当运行exec( ...

  4. Mysql时间差计算

    Mysql如何计算两个时间字段的差值?可用函数 TIMESTAMPDIFF() ----------------------------- TIMESTAMPDIFF函数,有参数设置,可以精确到天(D ...

  5. 我的coding地址

    https://dev.tencent.com/u/dtid_d6b0780bdefc3f9c/follower#1

  6. Django ORM中常用字段和参数

    一些说明: 表myapp_person的名称是自动生成的,如果你要自定义表名,需要在model的Meta类中指定 db_table 参数,强烈建议使用小写表名,特别是使用MySQL作为后端数据库时. ...

  7. list.sort和内置方法sorted的区别

    1.list.sort会直接修改list,不会把原来的列表复制一份,直接修改list,所以list.sort的返回值为None; 2.sorted(list)的返回值就修改之后的list,原来的lis ...

  8. Cmake实践(Cmake Practice)第一部分

    参考资料地址:https://github.com/Akagi201/learning-cmake/blob/master/docs/cmake-practice.pdf 一.初识cmake 1. C ...

  9. 安卓出现错误: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

    Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...

  10. ASP.NET结合Redis实现分布式缓存

    最近一个项目ASP.NET+MySQL 有的网页打开初始化的查询需要10秒甚至更久,用户体验极差,而且并发量变大的时候网站容易崩溃 后来想了两种解决方案都不是太满意 1.数据库里建一张缓存表,后台作业 ...