美文网首页
邮件发送

邮件发送

作者: 荒天帝886 | 来源:发表于2019-05-22 17:31 被阅读0次

https://blog.csdn.net/qq_16055765/article/details/79740685
https://www.jianshu.com/writer#/notebooks/36543162/notes/49730902/preview

pom.xml

<dependency>
   <groupId>com.sun.mail</groupId>
   <artifactId>javax.mail</artifactId>
   <version>1.5.5</version>
</dependency>

application.yml

mail:
  account: 853671516@qq.com
  password: uxnsnlrujufobfdg
  transport:
    protocol: smtp
  smtp:
    auth: true
    port: 465
    host: smtp.qq.com
    socketFactory:
      class: javax.net.ssl.SSLSocketFactory
      fallback: false

SendSMS.java

package com.vegetables.web.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

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;

@Slf4j
@Service
public class SendSMS {
    //发件人邮箱
    @Value("${mail.account}")
    private String ACCOUNT;
    //发件人密钥
    @Value("${mail.password}")
    private String PASSWORD;

    @Value("${mail.smtp.port}")
    private String PORT;

    @Value("${mail.smtp.host}")
    private String HOST;

    @Value("${mail.smtp.auth}")
    private String AUTH;

    @Value("${mail.transport.protocol}")
    private String PROTOCOL;

    @Value("${mail.smtp.socketFactory.class}")
    private String CLASS;

    @Value("${mail.smtp.socketFactory.fallback}")
    private String FALLBACK;

    //异步方法,另起线程
    @Async
    public void senEmail(String emailAddress, String emailMessage) throws Exception {
        log.info("Thread - {}", Thread.currentThread().getName());
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", PROTOCOL);
        props.setProperty("mail.smtp.host", HOST);
        props.setProperty("mail.smtp.auth", AUTH);
        props.setProperty("mail.smtp.port", PORT);
        props.setProperty("mail.smtp.socketFactory.class", CLASS);
        props.setProperty("mail.smtp.socketFactory.fallback", FALLBACK);
        props.setProperty("mail.smtp.socketFactory.port", PORT);

        log.info("prop - {}", props);

        Session session = Session.getInstance(props);
        session.setDebug(true);
        MimeMessage message = createMimeMessage(session, ACCOUNT, emailAddress, emailMessage);
        Transport transport = session.getTransport();
        transport.connect(ACCOUNT, PASSWORD);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    private static MimeMessage createMimeMessage(Session session, String account, String mailAddress, String emailMessage) throws Exception {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(account, "来自星星的亲切问候", "UTF-8"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailAddress, "我的测试邮件_收件人昵称", "UTF-8"));

        MimeBodyPart text = new MimeBodyPart();
        text.setContent(emailMessage, "text/html;charset=UTF-8");
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text);
        message.setContent(mm);
        message.setSubject("通知邮件", "UTF-8");
        return message;
    }
}

相关文章

  • spring boot 邮件发送基础详解(4种邮件超级详细)

    本文将介绍spring boot邮件发送将介绍以下几个方面: 邮件使用场景 邮件发送原理 邮件发送流程 邮件发送步...

  • SKPSMTPMessage

    SKPSMTPMessage 可以自己实现邮件发送,采用系统的邮件发送会弹出邮件发送框,如果要求静默发送邮件可参照...

  • SpringBoot 发送邮件

    # 依赖 # 配置 # 发送简单文本邮件 # 发送html邮件 # 使用邮件模板发送邮件 添加依赖 配置 新建ht...

  • 2018-10-11

    文本邮件的发送 1.邮件发送流程 ​ 邮件的发送是主动行为:主要通过 MUA/邮件客户端软件,将邮件内容发送给对应...

  • python自动发送邮件

    python自动发送邮件 在说python发送邮件之前,需要了解一下简单的邮件发送知识,邮件发送一般通过SMTP协...

  • python -- Email , send(smtp), re

    python Email功能: 发送普通文本邮件 发送带有html格式的邮件 发送带有附件的邮件 发送插入图片到正...

  • 2018-07-02

    发送邮件 //发送邮件 @ResponseBody @RequestMapping("email") public...

  • Golang使用SMTP发送邮件

    使用SMTP发送邮件 发送邮件测试 邮箱如下

  • python学习(21)smtp发送邮件

    本文介绍python发送邮件模块smtplib以及相关MIME模块。smtplib用于生成邮件发送的代理,发送邮件...

  • email

    1. django 发送邮件 settings.py中配置发送邮件邮箱 使用send_mail方法发送邮件

网友评论

      本文标题:邮件发送

      本文链接:https://www.haomeiwen.com/subject/lxouzqtx.html