1. 程式人生 > >spring 傳送email

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>