老妖的博客
          現(xiàn)實(shí)的中沒有幾個(gè)人能夠真為對(duì)方去死,甚至山盟海誓很快就會(huì)在金錢面前變的微不足道,這才是生活。沒有永遠(yuǎn)的愛,除了你的父母對(duì)你,當(dāng)然也就沒有永遠(yuǎn)的恨,更沒有永遠(yuǎn)的痛,時(shí)間是最好的治療大師,它會(huì)很快撫平你心靈上累累的傷痕。很多年以后你想起來時(shí),那些在你生命中洶涌來往的人群至多是個(gè)模糊的影子或者毫無意義的名字
          posts - 105,  comments - 171,  trackbacks - 0

          有的朋友問我如何發(fā)送帶有附件的html的郵件,不要加入contentID,使用addAttachment即可
          實(shí)現(xiàn)代碼


           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 老妖 閱讀(1653) 評(píng)論(0)  編輯  收藏 所屬分類: spring

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

          常用鏈接

          隨筆分類(48)

          隨筆檔案(104)

          好友鏈接

          我的豆瓣

          積分與排名

          • 積分 - 221783
          • 排名 - 257

          最新評(píng)論

          閱讀排行榜

          主站蜘蛛池模板: 韶关市| 阜平县| 工布江达县| 永丰县| 乌拉特前旗| 敦煌市| 嘉定区| 田林县| 宜川县| 普陀区| 乌拉特前旗| 怀远县| 尉氏县| 万全县| 通州市| 双城市| 上虞市| 新河县| 喜德县| 武宁县| 介休市| 凭祥市| 左云县| 保靖县| 客服| 平远县| 临沂市| 聂荣县| 尚志市| 资中县| 宜兰县| 无锡市| 玉溪市| 融水| 莱州市| 永德县| 柞水县| 屏东县| 阿拉尔市| 金湖县| 阜城县|