zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    SpringBoot开发中的坑 记录一些
    MongoDB 文档【行】
    MongoDB 集合【表】
    MongoDB 数据库
    MongoDB 基础
    gitlab或github下fork后如何同步源的新更新内容?
    解决:android源码同步repo sync 时出现的fatal:duplicate path错误
    android ROM刷机updater-script单刷补丁包脚本
    一键解包/打包boot.img/recovery.img工具(高通/MTK双版 支持android 5.1以上)
    解决:github上传时出现error: src refspec master does not match any
  • 原文地址:https://www.cnblogs.com/leavesss/p/9955433.html
Copyright © 2011-2022 走看看