其实这个方式和上篇文章《spring发送邮件(无附件情况)》差不多。这里把主要代码粘贴出来,如果有Spring的开发经验,相信大家一看就明白。
1 BaseMailSender.java
/**
*
*/
package gov.emergency.emercore.persistencedb.test.mail;
import org.springframework.mail.javamail.JavaMailSender;
/**
* @author liuzhy
*
*/
public abstract class BaseMailSender {
protected String to;
protected String from;
protected String subject;
protected JavaMailSender sender;
public void setFrom(String from) {
this.from = from;
}
public void setSender(JavaMailSender sender) {
this.sender = sender;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setTo(String to) {
this.to = to;
}
}
2 SimpleHtmlMailSender.java
package gov.emergency.emercore.persistencedb.test.mail;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessageHelper;
public class SimpleHtmlMailSender extends BaseMailSender{
public void sendMessage() throws MessagingException{
MimeMessage msg = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true, "GB2312");
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='#'>郁闷!"
+ "</a></h1></body></html>", true); //HTML邮件内容
// 增加附件
FileSystemResource rarfile = new FileSystemResource(new File("c:/bbb.jpg"));
helper.addAttachment("bb.jpg", rarfile);
sender.send(msg);
}
}
3 MailTest.java
package gov.emergency.emercore.persistencedb.test.mail;
import javax.mail.MessagingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MailTest {
public static void main(String[] args) throws MessagingException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("gov/emergency/emercore/persistencedb/test/mail/mail_attach.xml");
SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx.getBean("testMailSender");
sender.sendMessage();
}
}
4 mail_attach.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
<beans>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.163.com</value>
</property>
<property name="javaMailProperties">
<props>
<!-- 如果要使用用户名和密码验证,这一步需要 -->
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
<property name="username">
<value>邮箱用户名</value>
</property>
<property name="password">
<value>邮箱密码</value>
</property>
</bean>
<!-- 测试发送的类 -->
<bean id="testMailSender" class="gov.emergency.emercore.persistencedb.test.mail.SimpleHtmlMailSender">
<property name="sender">
<ref bean="mailSender"/>
</property>
<property name="to">
<value>
[email protected]</value>
</property>
<property name="from">
<value>
[email protected]</value>
</property>
<property name="subject">
<value>Html Mail</value>
</property>
</bean>
</beans>