Spring整合JavaMail

package com.xiajin.javamail;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class JavaMail {  public static void main(String[] args) throws Exception {
	 JavaMailSenderImpl javaMail = new JavaMailSenderImpl();   
     javaMail.setHost("smtp.163.com");   
     javaMail.setPassword("xxxxxxxxxx");   
     javaMail.setUsername("[email protected]");   
     Properties prop = new Properties();   
     prop.setProperty("mail.smtp.auth", "true");        
     javaMail.setJavaMailProperties(prop);   
     MimeMessage message = javaMail.createMimeMessage(); //建立邮件消息  
     MimeMessageHelper messageHelp = new MimeMessageHelper(message,true,"GBK");   
		//表示启用multipart模式
     messageHelp.setFrom("[email protected]");   
     messageHelp.setTo("[email protected]");   
     messageHelp.setSubject("邮件测试");   
     String body = "<html><head><META http-equiv=Content-Type content='text/html; charset=GBK'></HEAD><title>test</title></head><body>dear guy \n ";   
     body+="<red>帅哥!</red> pic <img src='cid:a'></img><br>hello</body></html>";   
     messageHelp.setText(body, true); //true表示启用HTML格式的邮件  
     messageHelp.addInline("a", new File("E:/a.jpg"));   
     File file=new File("E:/Favorites中文文件.rar");        
     try {   
         messageHelp.addAttachment(MimeUtility.encodeWord(file.getName()), file);   
     } catch (UnsupportedEncodingException e) {             
         e.printStackTrace();   
     }   
     javaMail.send(message);   
     System.out.println("ok");
 }   

}

你可能感兴趣的:(java,spring,html)