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="xxxx@gmail.com" />
<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("xxx@gmail.com");

mmhepler.setFrom("xxxx@gmail.com");
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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值