• spring 发送email


    此篇非原创,代码忘了从哪篇帖子下学习的了,时间太久了,写这篇主要是为了存一下

    package com.cn.test.mailConfig;
    import java.security.GeneralSecurityException;  
    import java.util.Properties;  
      
    import javax.mail.MessagingException;  
    import javax.mail.internet.MimeMessage;  
      
    import org.springframework.core.io.FileSystemResource;  
    import org.springframework.mail.SimpleMailMessage;  
    import org.springframework.mail.javamail.JavaMailSenderImpl;  
    import org.springframework.mail.javamail.MimeMessageHelper;  
      
    import com.sun.mail.util.MailSSLSocketFactory;  
    public class Mail {
    public static void sendSimpleMail() throws GeneralSecurityException{  
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();    
            senderImpl.setHost("smtp.163.com");    
            senderImpl.setUsername("**@163.com");  
            senderImpl.setPassword("**");  
            Properties prop = new Properties();    
            MailSSLSocketFactory sf = new MailSSLSocketFactory();  
            sf.setTrustAllHosts(true);  
            prop.put("mail.smtp.ssl.enable", "true");  
            prop.put("mail.smtp.ssl.socketFactory", sf);  
            prop.put("mail.smtp.auth", "true");  
            prop.put("mail.smtp.timeout", "20000");  
            senderImpl.setJavaMailProperties(prop);  
            SimpleMailMessage mail = new SimpleMailMessage();    
            mail.setFrom("**@163.com");  
            mail.setTo("**@163.com");  
            mail.setSubject("测试标题");  
            mail.setText("测试");  
            senderImpl.send(mail);  
            System.out.println("SIMPLEMAIL SENDED");  
        }  
        public static void sendMimeMail() throws GeneralSecurityException, MessagingException{  
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  
            senderImpl.setHost("smtp.163.com");    
            senderImpl.setUsername("**@163.com");  
            senderImpl.setPassword("**"); 
            Properties prop = new Properties();  
            MailSSLSocketFactory sf = new MailSSLSocketFactory();  
            sf.setTrustAllHosts(true);  
            prop.put("mail.smtp.ssl.enable", "true");  
            prop.put("mail.smtp.ssl.socketFactory", sf);  
            prop.put("mail.smtp.auth", "true");  
            prop.put("mail.smtp.timeout", "20000");   
            senderImpl.setJavaMailProperties(prop);  
            MimeMessage mail = senderImpl.createMimeMessage();  
            MimeMessageHelper helper = new MimeMessageHelper(mail,true);  
            helper.setFrom("**@163.com");  
            helper.setTo("**@163.com");  
            helper.setSubject("测试标题MIME");  
            helper.setText("测试MIME");  
            FileSystemResource mailImage = new FileSystemResource("src/main/webapp/files/images/0.jpg");  
            helper.addAttachment("mail.png", mailImage);  
            senderImpl.send(mail);  
            System.out.println("MIMEMAIL SENDED");  
        }  
        public static void sendHTMLMail() throws GeneralSecurityException, MessagingException{  
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  
            senderImpl.setHost("smtp.163.com");    
            senderImpl.setUsername("**@163.com");  
            senderImpl.setPassword("**");
            senderImpl.setDefaultEncoding("UTF-8");  
            Properties prop = new Properties();  
            MailSSLSocketFactory sf = new MailSSLSocketFactory();  
            sf.setTrustAllHosts(true);  
            prop.put("mail.smtp.ssl.enable", "true");  
            prop.put("mail.smtp.ssl.socketFactory", sf);  
            prop.put("mail.smtp.auth", "true");  
            prop.put("mail.smtp.timeout", "20000");   
            senderImpl.setJavaMailProperties(prop);  
            MimeMessage mail = senderImpl.createMimeMessage();  
            MimeMessageHelper helper = new MimeMessageHelper(mail,true);  
            helper.setFrom("**@163.com");  
            helper.setTo("**@163.com");  
            helper.setSubject("测试标题HTML");  
            StringBuilder html = new StringBuilder();  
            html.append("<html>")  
                .append("<body>")  
                .append("<h2>Goser,你好</h2>")  
                .append("<p>这是一个测试。</p>")  
                .append("<img src='cid:mailImage'/>")  
                .append("<p>THANKS</p>")  
                .append("</body>")  
                .append("</html>");  
            helper.setText(html.toString(),true);  
            FileSystemResource mailImage = new FileSystemResource("src/main/webapp/files/images/0.jpg");  
            helper.addInline("mailImage", mailImage);  
            senderImpl.send(mail);  
            System.out.println("HTMLMAIL SENDED");  
        }  
        public static void main(String[] args) throws GeneralSecurityException, MessagingException{  
            //sendHTMLMail();  
            //sendSimpleMail();  
            sendMimeMail();  
        }  
    }

    <dependency>
             <groupId>javax.mail</groupId>
             <artifactId>mail</artifactId>
             <version>1.4.7</version>
        </dependency>

  • 相关阅读:
    文件编程---库函数
    linux文件编程----系统调用
    makefile工程管理
    快速上手日期插件laydate
    js判断浏览器类型以及语言
    谷歌支付服务端详细讲解(PHP)
    php中的date和strtotime函数妙用
    本地搭建GitLab
    mysql查询语句常用字段操作函数
    php中签名公钥、私钥(SHA1withRSA签名)以及AES(AES/ECB/PKCS5Padding)加密解密详解
  • 原文地址:https://www.cnblogs.com/leavesss/p/9955433.html
Copyright © 2020-2023  润新知