Spring 发送普通邮件、夹带附件的邮件、嵌入网页或图片的邮件 使用方法

在项目应用中,时常存在发送邮件的业务需求。比如:用户注册时的邮件确认;系统出错时的邮件提醒;用户系统邮件订阅;系统的广告邮件定时推广等等。本文根据spring开发文档,列举普通文件邮件,夹带附件的邮件、嵌入图片的邮件三种形式的邮件的使用方式。
实现过程中定义BaseMailDefined类,用于描述邮件发送过程中需要的参数:发送者、接受者、邮件标题、邮件文本内容。
定义MailSenderFactory类,用于对普通文件邮件,夹带附件的邮件、嵌入图片的邮件三种形式的邮件的发送。其中JavaMailSender 采用spring注入的其实现类org.springframework.mail.javamail.JavaMailSenderImpl。
[img]http://dl.iteye.com/upload/picture/pic/130269/f32fee90-2bd4-3da4-95d7-fef8d13a4e89.png[/img]

public class MailSenderFactory {

private JavaMailSender javaMailSender;

public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}


/**
* 简单邮件发送
* @param bmd
*/
public void senderSimpleMail(BaseMailDefined bmd){
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(bmd.getFrom());
msg.setText(bmd.getText());
msg.setTo(bmd.getTo());
msg.setSubject(bmd.getSubject());
javaMailSender.send(msg);
}

/**
* 发送添加附件的邮件
* @param bmd
* @throws MessagingException
*/
public void senderMimeMail(BaseMailDefined bmd) throws MessagingException{
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setTo(bmd.getTo());
helper.setText(bmd.getText());
helper.setFrom(bmd.getFrom());
helper.setSubject(bmd.getSubject());
helper.setSentDate(new Date());
//夹带附件
FileSystemResource file = new FileSystemResource(new File("E:/task/ip2.rar"));
helper.addAttachment("ip2.rar", file);
javaMailSender.send(msg);
}

/**
* 发送内嵌图片或html的邮件
* @param bmd
* @throws MessagingException
*/
public void senderHtmlMail(BaseMailDefined bmd) throws MessagingException{
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setTo(bmd.getTo());
helper.setText(bmd.getText());
helper.setFrom(bmd.getFrom());
helper.setSubject(bmd.getSubject());
helper.setSentDate(new Date());
//内嵌式图片或html样式
helper.setText("<html><body><img src='cid:identifier1235'></body></html>", true);
FileSystemResource res = new FileSystemResource(new File("E:/task/ip2.png"));
helper.addInline("identifier1235", res);
javaMailSender.send(msg);
}
}

public class BaseMailDefined {
protected String to;
protected String from;
protected String subject;
protected String text;

public String getText() {
return text;
}

public String getTo() {
return to;
}

public String getFrom() {
return from;
}

public String getSubject() {
return subject;
}

public void setText(String text) {
this.text = text;
}

public void setTo(String to) {
this.to = to;
}

public void setFrom(String from) {
this.from = from;
}

public void setSubject(String subject) {
this.subject = subject;
}

}
测试方法:
public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { "ApplicationContext.xml" });

MailSenderFactory factory = (MailSenderFactory) ctx.getBean("mailFactory");
BaseMailDefined bmd = (BaseMailDefined) ctx.getBean("baseMail");

factory.senderSimpleMail(bmd);
factory.senderMimeMail(bmd);
factory.senderHtmlMail(bmd);

}
XML配置:
<bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.sina.com</value>
</property>
<property name="username">
<value>handouName</value>
</property>
<property name="password">
<value>handouPassword</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>

<bean id="mailFactory" class="com.handou.mail.MailSenderFactory">
<property name="javaMailSender">
<ref bean="sender" />
</property>
</bean>

<bean id="baseMail" class="com.handou.mail.BaseMailDefined">
<property name="to">
<value>handouName@sina.com</value>
</property>
<property name="from">
<value>handouName@sina.com</value>
</property>
<property name="subject">
<value>test</value>
</property>
<property name="text">
<value>this is a test mail !!</value>
</property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值