码迷,mamicode.com
首页 > 编程语言 > 详细

spring 发送email

时间:2018-11-13 23:57:34      阅读:408      评论:0      收藏:0      [点我收藏+]

标签:原创   标题   lin   mail   ima   inter   main   factory   学习   

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

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>

 

spring 发送email

标签:原创   标题   lin   mail   ima   inter   main   factory   学习   

原文地址:https://www.cnblogs.com/leavesss/p/9955433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!