Javamail发送邮件

AlgoStrider
• 阅读 1068

正文

这几篇文章写的就挺好了,传送过去看看吧:

1、 使用JavaMail创建邮件和发送邮件

可能遇到的问题:
1、因为端口号问题导致的错误:

javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2210)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
        at javax.mail.Service.connect(Service.java:317)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)

问题和解决这里可以看到,把port configuration from 465 to 587(把端口从465改成587)
https://stackoverflow.com/que...

2、使用javamail发送内嵌图片的html格式邮件

要在邮件中包含图片简单办法是使用image标签,src指向服务器上图片的位置。

 package com.example.emaildemo;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @program: email-demo
 * @description:
 * @author: smallsoup
 * @create: 2019-01-27 16:44
 **/

public class SendEmailUtil {
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.exmail.qq.com");
        props.setProperty("mail.smtp.auth", "true");

        //使用JavaMail发送邮件的5个步骤
        //1、创建session
        Session mailSession = Session.getInstance(props);
        //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
        mailSession.setDebug(true);
        //2、通过session得到transport对象
        Transport transport = mailSession.getTransport();

        //3、使用邮箱的用户名和密码连上邮件服务器,这里有多个构造器,可传入host、端口、user、password
        transport.connect( "你的邮箱地址", "你的邮箱AUTH密码,不是登陆密码哦,在邮箱的设置里单独开启和设置");

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("HTML mail Hello");
        message.setFrom(new InternetAddress("你的邮箱地址"));
        //4、创建邮件
        message.setContent("<h1>This is a test</h1>" + "<img src=\"http://www.rgagnon.com/images/jht.gif\">",
                "text/html");
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("接收人邮箱地址"));

        //5、发送邮件
//        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}

上面发送带图片邮件的方法很简单,但是有些邮件客户端会把是否包含有服务器端图片作为垃圾邮件的判断机制。我们可以将图片内嵌到邮件中,然后用cid加content-id引用内嵌的图片。

package com.example.emaildemo;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
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 java.util.Properties;

/**
 * @program: email-demo
 * @description:
 * @author: smallsoup
 * @create: 2019-01-27 16:44
 **/

public class SendEmailUtil {
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.exmail.qq.com");
        props.setProperty("mail.smtp.auth", "true");

        //使用JavaMail发送邮件的5个步骤
        //1、创建session
        Session mailSession = Session.getInstance(props);
        //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
        mailSession.setDebug(true);
        //2、通过session得到transport对象
        Transport transport = mailSession.getTransport();

        //3、使用邮箱的用户名和密码连上邮件服务器,这里有多个构造器,可传入host、端口、user、password
        transport.connect( "你的邮箱地址", "你的邮箱AUTH密码,不是登陆密码哦,在邮箱的设置里单独开启和设置");

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("HTML mail Hello");
        message.setFrom(new InternetAddress("你的邮箱地址"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("接收人邮箱地址"));

        //4、创建邮件
        //This HTML mail have to 2 part, the BODY and the embedded image
        MimeMultipart multipart = new MimeMultipart("related");

        // first part  (the html)
        BodyPart messageBodyPart = new MimeBodyPart();
        String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
        messageBodyPart.setContent(htmlText, "text/html");

        // add it
        multipart.addBodyPart(messageBodyPart);

        // second part (the image)
        messageBodyPart = new MimeBodyPart();
        DataSource fds = new FileDataSource("C:\\images\\jht.gif");
        messageBodyPart.setDataHandler(new DataHandler(fds));
        messageBodyPart.setHeader("Content-ID","image");

        // add it
        multipart.addBodyPart(messageBodyPart);

        // put everything together
        message.setContent(multipart);
        //5、发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}

SpringBoot发送邮件需要加依赖:

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
  </dependency>

具体可参考:
java发送html模板的高逼格邮件




本公众号免费提供csdn下载服务,海量IT学习资源,如果你准备入IT坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于java、go、python、springcloud、elk、嵌入式 、大数据、面试资料、前端 等资源。同时我们组建了一个技术交流群,里面有很多大佬,会不定时分享技术文章,如果你想来一起学习提高,可以公众号后台回复【2】,免费邀请加技术交流群互相学习提高,会不定期分享编程IT相关资源。


扫码关注,精彩内容第一时间推给你

Javamail发送邮件

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
4年前
javamail发送邮件(简单邮件qq邮箱)
/\\<dependency<groupIdcom.sun.mail</groupId<artifactIdjavax.mail</artifactId<version1.5.4</version</dependency\//\上面是maven需要添加的依赖\/p
Easter79 Easter79
4年前
springboot系列九,springboot整合邮件服务、整合定时任务调度
一、整合邮件服务   如果要进行邮件的整合处理,那么你一定需要有一个邮件服务器,实际上java本身提供有一套JavaMail组件以实现邮件服务器的搭建,但是这个搭建的服务器意义不大,因为你现在搭建完成了,向一些大型的站点发送一封邮件,若干小时你就会被拉黑,如果不想拉黑彼此之间就做一个白名单即可。   要发送邮件,首先要知道
Wesley13 Wesley13
4年前
JavaMail发送和接收邮件API(详解)
一、JavaMail概述:    JavaMail是由Sun定义的一套收发电子邮件的API,不同的厂商可以提供自己的实现类。但它并没有包含在JDK中,而是作为JavaEE的一部分。    厂商所提供的JavaMail服务程序可以有选择地实现某些邮件协议,常见的邮件协议包括:SMTP:简单邮件传输
Wesley13 Wesley13
4年前
Java发送邮件报错:Network is unreachable
使用javamail发送邮件时,老是提示NetworkisNetwork:com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163.com, 25; timeout 1;  nested exception is:j
Easter79 Easter79
4年前
SpringBoot入门 (十) 发送邮件
本文记录学习在SpringBoot中发送邮件。一邮件发送过程发送邮件是一个我们在项目中经常会用到的功能,如在用户注册时发送验证码,账户激活等都会用到。完整的一个邮件发送过程主要包含以下几个步骤:1发件人在用户邮件代理上写邮件内容及收件人的邮箱地址;2用户邮件代理根据发件人填写的邮件信息,生成一封符合邮件格式的邮件;
Stella981 Stella981
4年前
SpringBoot使用JavaMailSender发送邮件(2)
为了更容易更规范维护邮件内容,我们推荐使用模板引擎技术。常用的模板引擎有这几种Thymeleaf、FreeMarker、Velocity等。我们在这里就使用前面两种来实现发送邮件。一、使用Thymeleaf模板技术发送邮件Thymeleaf是SpringBoot推荐的官方模板引擎技术,使用非常的简单方便。(1)添加依赖
Easter79 Easter79
4年前
SpringBoot使用JavaMailSender发送邮件(1)
邮件发送是一个非常常见的功能,最初Sun公司提供了JavaMail用来实现邮件发送,但是配置烦琐。后来Spring中提供了JavaMailsender用来简化邮件配置,而SpringBoot则提供了MailSenderAutoConfiguration对邮件的发送做了进一步简化。在开始之前我们需要申请开通POP3/SMTP服
Easter79 Easter79
4年前
SpringBoot使用JavaMailSender发送邮件(2)
为了更容易更规范维护邮件内容,我们推荐使用模板引擎技术。常用的模板引擎有这几种Thymeleaf、FreeMarker、Velocity等。我们在这里就使用前面两种来实现发送邮件。一、使用Thymeleaf模板技术发送邮件Thymeleaf是SpringBoot推荐的官方模板引擎技术,使用非常的简单方便。(1)添加依赖
Stella981 Stella981
4年前
SpringBoot入门 (十) 发送邮件
本文记录学习在SpringBoot中发送邮件。一邮件发送过程发送邮件是一个我们在项目中经常会用到的功能,如在用户注册时发送验证码,账户激活等都会用到。完整的一个邮件发送过程主要包含以下几个步骤:1发件人在用户邮件代理上写邮件内容及收件人的邮箱地址;2用户邮件代理根据发件人填写的邮件信息,生成一封符合邮件格式的邮件;
Stella981 Stella981
4年前
SpringBoot使用JavaMailSender发送邮件(1)
邮件发送是一个非常常见的功能,最初Sun公司提供了JavaMail用来实现邮件发送,但是配置烦琐。后来Spring中提供了JavaMailsender用来简化邮件配置,而SpringBoot则提供了MailSenderAutoConfiguration对邮件的发送做了进一步简化。在开始之前我们需要申请开通POP3/SMTP服
Wesley13 Wesley13
4年前
1 分钟教会你用 Spring Boot 发邮件
!(https://oscimg.oschina.net/oscnet/94147ca606ad46dca8e3d95a3e5ace39.jpg)SpringBoot提供了一个发送邮件的简单抽象,使用的是下面这个接口。org.springframework.mail.javamail.JavaMailSenderSpring
AlgoStrider
AlgoStrider
Lv1
疏影横斜水清浅,暗香浮动月黄昏。
文章
3
粉丝
0
获赞
0