Javamail发送邮件

正文

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

一、 使用JavaMail建立邮件和发送邮件前端

可能遇到的问题:一、由于端口号问题致使的错误:java

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/questions/31535863/error-when-sending-email-via-java-mail-apipython

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

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

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个步骤
        //一、建立session
        Session mailSession = Session.getInstance(props);
        //开启Session的debug模式,这样就能够查看到程序发送Email的运行状态
        mailSession.setDebug(true);
        //二、经过session获得transport对象
        Transport transport = mailSession.getTransport();

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

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("HTML mail Hello");
        message.setFrom(new InternetAddress("你的邮箱地址"));
        //四、建立邮件
        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("接收人邮箱地址"));

        //五、发送邮件
//        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个步骤
        //一、建立session
        Session mailSession = Session.getInstance(props);
        //开启Session的debug模式,这样就能够查看到程序发送Email的运行状态
        mailSession.setDebug(true);
        //二、经过session获得transport对象
        Transport transport = mailSession.getTransport();

        //三、使用邮箱的用户名和密码连上邮件服务器,这里有多个构造器,可传入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("接收人邮箱地址"));

        //四、建立邮件
        //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);
        //五、发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}复制代码

SpringBoot发送邮件须要加依赖:api

<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相关资源。session

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

image

相关文章
相关标签/搜索