美文网首页
邮件发送

邮件发送

作者: 荒天帝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;
        }
    }
    

    相关文章

      网友评论

          本文标题:邮件发送

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