spring mail 例子

  <!-- Send Email Helper -->
  <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="protocol" value="smtps" />
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="465" />
    <property name="username" value="[email protected]" />
    <property name="password" value="xxxxx" />
    <property name="javaMailProperties">
      <props>
        <prop key="mail.smtps.auth">true</prop>
      </props>
    </property>
  </bean>
  <bean id="mailService" class="com.spring3.MailServiceImp">  
        <property name="javaMailSender" ref="javaMailSender"/>  
    </bean>  

<bean id="taskExecutor"
	class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
	<property name="corePoolSize" value="4" />
	<!-- 并发线程数,想达到真正的并发效果,最好对应CPU的线程数及核心数 -->
	<property name="maxPoolSize" value="10" />
	<!-- 最大线程池容量 -->
	<property name="queueCapacity" value="500" />
	<!-- 超过最大线程池容量后,允许的线程队列数 -->
</bean>





package com.spring3;
package com.spring3;

public interface MailService {
	public void sendEmail();
}




import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailServiceImp implements MailService {
	
	private JavaMailSenderImpl javaMailSender;
	
	public void sendEmail() {
		try {
			MimeMessage  mailMessage = javaMailSender.createMimeMessage(); 
			MimeMessageHelper mmhepler = new MimeMessageHelper(mailMessage,true,"UTF-8");
			
			mmhepler.setTo("[email protected]");  
	       
			mmhepler.setFrom("[email protected]");
			mmhepler.setSubject("Spring Mail 测试成功!");  
			mmhepler.setText("<html><body color='red'>" +  
	                "<font size='4' color='red'> 我的邮件测试</font>" +  
	                "<a href='www.google.com'>谷歌Google</a> <img src=\"cid:aaa\"/>"+  
	                "</body></html>",true);  
			
			FileSystemResource img = new FileSystemResource(new File("E:/Test62a.jpg")); 
			
			mmhepler.addInline("aaa", img);  
			mmhepler.addAttachment("Test62a.jpg", img); 
			
			javaMailSender.send(mailMessage);  
	          
	        System.out.println("发送成功!");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public JavaMailSenderImpl getJavaMailSender() {
		return javaMailSender;
	}

	public void setJavaMailSender(JavaMailSenderImpl javaMailSender) {
		this.javaMailSender = javaMailSender;
	}
	@Test
	public void test(){
		String path="service-context.xml";  
        ApplicationContext factory=new ClassPathXmlApplicationContext(path);  
        MailService mailService = (MailService) factory.getBean("mailService");  
        try {  
            mailService.sendEmail();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}
	
	
	
}


package com.helper;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;

import com..service.CommonService;

public class SendEmailThread implements Runnable{
	private final Logger log = LoggerFactory.getLogger(SendEmailThread.class);
	private String email;
	private int templateId;
	private String title;
	private Object data ;
	private Map<String, FileSystemResource> imageMap;
	
	private CommonService commonService;
	
	@SuppressWarnings("unused")
	private SendEmailThread(){
	}
	
		
	
	public SendEmailThread(String email, int templateId, String title,
			Object data, Map<String, FileSystemResource> imageMap,CommonService commonService) {
		super();
		this.email = email;
		this.templateId = templateId;
		this.title = title;
		this.data = data;
		this.imageMap = imageMap;
		this.commonService=commonService;
	}



	public void run() {
		try {
			System.out.println("发送Email="+email+".........");
			//Thread.sleep(1000);
			commonService.sendMail(email, templateId, title, data, imageMap);
		} catch (Exception e) {
			log.error("send email "+email+" \\n error:"+e);
		}

		
	}
	
	

}


SendEmailThread sendEmailThread=new SendEmailThread(email, 1, "啊啊啊", data, null,commonService);
			taskExecutor.execute(sendEmailThread);




你可能感兴趣的:(spring)