老妖的博客
现实的中没有几个人能够真为对方去死,甚至山盟海誓很快就会在金钱面前变的微不足道,这才是生活。没有永远的爱,除了你的父母对你,当然也就没有永远的恨,更没有永远的痛,时间是最好的治疗大师,它会很快抚平你心灵上累累的伤痕。很多年以后你想起来时,那些在你生命中汹涌来往的人群至多是个模糊的影子或者毫无意义的名字
posts - 105,  comments - 171,  trackbacks - 0

有的朋友问我如何发送带有附件的html的邮件,不要加入contentID,使用addAttachment即可
实现代码


 1 package com.educast.mail;
 2 
 3 import org.springframework.mail.javamail.MimeMessageHelper;
 4 import org.springframework.core.io.FileSystemResource;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.FileSystemXmlApplicationContext;
 7 
 8 import javax.mail.MessagingException;
 9 import javax.mail.internet.MimeMessage;
10 import java.io.File;
11 
12 
13 public class AttachmentMailSender extends BaseMailSender  {
14    public void sendMessage() throws MessagingException {
15         MimeMessage msg = sender.createMimeMessage();
16         MimeMessageHelper helper = new MimeMessageHelper(msg, true"GB2312");
17 
18         helper.setTo(to);
19         helper.setFrom(from);
20         helper.setSubject(subject);
21 
22         helper.setText("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='#'>郁闷!"
23                 + "</body></html>"true);
24 
25 
26 // add the rar file
27        FileSystemResource rarfile = new FileSystemResource(new File(
28                "c:\\a.rar"));
29        helper.addAttachment("a.rar", rarfile);
30 
31 
32         sender.send(msg);
33     }
34 
35     public static void main(String[] args) throws Exception {
36           ApplicationContext ctx = new FileSystemXmlApplicationContext(
37                 new String[] { "D:\\WORK\\JDBC\\mail\\src\\javaMailSender.xml" });
38         AttachmentMailSender sender = (AttachmentMailSender) ctx.getBean("messageSender");
39         sender.sendMessage();
40     }
41 }
42 
43 
配置文件
 1 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 2 <beans>
 3  <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
 4   <property name="host">
 5    <value>localhost</value>
 6   </property>
 7   <property name="username">
 8    <value>webmaster</value>
 9   </property>
10   <property name="password">
11    <value>password</value>
12   </property>
13         <property name="javaMailProperties">
14         <props>
15         <prop key="mail.smtp.auth">true</prop>
16         </props>
17         </property>
18 
19     </bean>
20 
21  <bean id="messageSender" class="com.educast.mail.AttachmentMailSender">
22   <property name="javaMailSender">
23    <ref bean="sender"/>
24   </property>
25   <property name="to">
26    <value>mfc42d@sohu.com</value>
27   </property>
28   <property name="from">
29    <value>webmaster@mymail.cn</value>
30   </property>
31   <property name="subject">
32    <value>HTML Mail</value>
33   </property>
34  </bean>
35 </beans>
36 
37 
posted on 2005-11-03 19:39 老妖 阅读(1624) 评论(0)  编辑  收藏 所属分类: spring

只有注册用户登录后才能发表评论。


网站导航:
 

<2005年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

随笔分类(48)

随笔档案(104)

好友链接

我的豆瓣

积分与排名

  • 积分 - 218728
  • 排名 - 256

最新评论

阅读排行榜