Spring的框架下发送带有内嵌图片和附件的邮件的java程序的小例子。
Spring的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/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>[email protected]</value> </property> <!-- 邮件服务密码 --> <property name="password"> <value>password</value> </property> </bean> </beans>
java的代码如下:
package cn.com.sl; import java.io.File; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; public class SendMail1 { public static void main(String[] args) throws Exception { ApplicationContext ctx = new FileSystemXmlApplicationContext( "src/applicationContext.xml"); JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender"); SendMail1 springMail = new SendMail1(); // 测试发送只有文本信息的简单测试 // springMail.sendTextMail(sender); // 测试发送html // springMail.sendHtml(sender); // 测试发送带附件的邮件 springMail.sendMimeMessage(sender); } /** */ /** * 测试发送只有文本信息的简单测试 * * @param sender * 邮件发送器 * @throws Exception */ private void sendTextMail(JavaMailSender sender) throws Exception { SimpleMailMessage mail = new SimpleMailMessage(); mail.setTo("[email protected]"); mail.setFrom("[email protected]"); mail.setSubject("test by amigo"); mail.setText("spring Mail的简单测试"); sender.send(mail); System.out.println("成功发送文本文件!"); } /** */ /** * 测试发送有html的简单测试 * * @param sender * 邮件发送器 * @throws Exception */ private void sendHtml(JavaMailSender sender) throws Exception { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); // 设置收件人,寄件人 messageHelper.setTo("[email protected]"); messageHelper.setFrom("[email protected]"); messageHelper.setSubject("测试HTML邮件!"); // true 表示启动HTML格式的邮件 messageHelper .setText( "<html><head></head><body><h1>hello!!chao.wang</h1></body></html>", true); // 发送邮件 sender.send(mailMessage); System.out.println("html发送成功....."); } /** */ /** * 测试发送有html和附件的简单测试 * * @param sender * 邮件发送器 * @throws Exception */ private void sendMimeMessage(JavaMailSender sender) throws Exception { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); MimeMessage mailMessage = senderImpl.createMimeMessage(); // 如果是增加附件mimeMessageHelper后一个参数要为true MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true ,"GB18030"); // 设置收件人,寄件人 messageHelper.setTo("[email protected]"); messageHelper.setFrom("[email protected]"); messageHelper.setSubject("测试附件邮件!"); // true 表示启动HTML格式的邮件 messageHelper.setText( "<html><head></head><body><h1>hello!!spring image html mail</h1><h2>大家好,这是Spring的邮件发送模块</h2>" + "<img src=\"cid:aaa\"/></body></html>", true); //添加内嵌图片 FileSystemResource img = new FileSystemResource(new File("e:/小海豹.jpg")); messageHelper.addInline("aaa",img); //添加附件 FileSystemResource file = new FileSystemResource(new File("e:/小海豹.rar")); //用于解决邮件显示附件名中含有中文 ClassPathResource fileName =new ClassPathResource("小海豹.rar"); //这里的方法调用和插入图片是不同的。 messageHelper.addAttachment(MimeUtility.encodeWord(fileName.getFilename()),file); // 发送邮件 sender.send(mailMessage); System.out.println("附件发送成功....."); } }
相关参考资料有:http://www.blogjava.net/tangzurui/archive/2008/12/08/244953.html;
http://yefriendly.iteye.com/blog/404341;
http://blog.csdn.net/matrixjava/article/details/3237772。