posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          spring mail發(fā)送

          Posted on 2007-05-11 17:58 semovy 閱讀(1182) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): spring

          package com.action;

          import java.util.ArrayList;
          import java.util.Date;
          import java.util.Iterator;
          import java.util.List;

          import javax.activation.DataHandler;
          import javax.activation.FileDataSource;
          import javax.mail.Message;
          import javax.mail.MessagingException;
          import javax.mail.Multipart;
          import javax.mail.internet.InternetAddress;
          import javax.mail.internet.MimeBodyPart;
          import javax.mail.internet.MimeMessage;
          import javax.mail.internet.MimeMultipart;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.FileSystemXmlApplicationContext;
          import org.springframework.mail.SimpleMailMessage;
          import org.springframework.mail.javamail.JavaMailSender;
          import org.springframework.mail.javamail.MimeMessagePreparator;

           

          public class SpringMail {
           public static void main(String[] args) throws Exception {
                  ApplicationContext ctx = new FileSystemXmlApplicationContext(
                          "applicationContext.xml");
                  JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
                  SpringMail springMail = new SpringMail();
                 
                  //測(cè)試發(fā)送只有文本信息的簡(jiǎn)單測(cè)試
                  springMail.sendTextMail(sender);
                 
                  //測(cè)試發(fā)送帶附件的郵件
                  springMail.sendMimeMessage(sender);
              }
             
              /** *//**
               * 測(cè)試發(fā)送只有文本信息的簡(jiǎn)單測(cè)試
               * @param sender 郵件發(fā)送器
               * @throws Exception
               */
              private void sendTextMail(JavaMailSender sender) throws Exception {
                  SimpleMailMessage mail = new SimpleMailMessage();
                  mail.setTo("superman_wshm@126.com");
                  mail.setFrom("superman_wshm@126.com");
                  mail.setSubject("test by amigo");
                  mail.setText("spring Mail的簡(jiǎn)單測(cè)試");
                  sender.send(mail);
                 
                  System.out.println("成功發(fā)送文本文件!");
              }
             
              /** *//**
               * 發(fā)送帶附件的郵件
               * @param sender 郵件發(fā)送器
               * @throws Exception
               */
              private void sendMimeMessage(final JavaMailSender sender) throws Exception {
                  //附件文件集合
                  final List files = new ArrayList();
                  MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
                      public void prepare(MimeMessage mimeMessage) throws MessagingException {
                          mimeMessage.setRecipient(Message.RecipientType.TO,
                                  new InternetAddress("superman_wshm@126.com"));
                          mimeMessage.setFrom(new InternetAddress("superman_wshm@126.com"));
                          mimeMessage.setSubject("Spring發(fā)送帶附件的郵件", "gb2312");
                         
                          Multipart mp = new MimeMultipart();
                         
                          //向Multipart添加正文

                          MimeBodyPart content = new MimeBodyPart();
                          content.setText("內(nèi)含spring郵件發(fā)送的例子,請(qǐng)查收!");
                         
                          //向MimeMessage添加(Multipart代表正文)
                          mp.addBodyPart(content);
                          files.add("com/action/SpringMail.java");
                          files.add("applicationContext.xml");
                         
                          //向Multipart添加附件
                          Iterator it = files.iterator();
                          while(it.hasNext()) {
                              MimeBodyPart attachFile = new MimeBodyPart();
                              String filename = it.next().toString();
                              FileDataSource fds = new FileDataSource(filename);
                              attachFile.setDataHandler(new DataHandler(fds));
                              attachFile.setFileName(fds.getName());
                              mp.addBodyPart(attachFile);
                          }
                         
                          files.clear();
                         
                          //向Multipart添加MimeMessage
                          mimeMessage.setContent(mp);
                          mimeMessage.setSentDate(new Date());
                      }
                  };

                  //發(fā)送帶附件的郵件
                  sender.send(mimeMail);
                 
                  System.out.println("成功發(fā)送帶附件郵件!");
              }

          }


          Spring配置文件applicationContext.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "

          <beans>
           <bean id="mailSender"
            class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="host">
             <value>smtp.126.com</value>
            </property>
            <property name="javaMailProperties">
             <props>
              <prop key="mail.smtp.auth">true</prop>
              <prop key="mail.smtp.timeout">25000</prop>
             </props>
            </property>
            <property name="username">
             <value>superman_wshm</value>
            </property>
            <property name="password">
             <value>******</value>
            </property>
           </bean>


          </beans>


           剛才發(fā)現(xiàn)一bug,當(dāng)附件名為中文時(shí),會(huì)出現(xiàn)中文亂碼問(wèn)題,對(duì)sendMimeMessage方法進(jìn)行了部分修改,如下:
             

                         sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
                          files.add(
          "src/SpringMail.java");
                          files.add(
          "src/applicationContext.xml");
                          files.add(
          "src/謝星星.xml");
                          
                          
          //向Multipart添加附件
                          Iterator it = files.iterator();
                       
          while (it.hasNext()) {
                              MimeBodyPart attachFile 
          = new MimeBodyPart();
                              String filename 
          = it.next().toString();
                              FileDataSource fds 
          = new FileDataSource(filename);
                              attachFile.setDataHandler(
          new DataHandler(fds));
                              attachFile.setFileName(
          "=?GBK?B?"+enc.encode(fds.getName().getBytes())+"?=");
                              mp.addBodyPart(attachFile);
                          }
          主站蜘蛛池模板: 江山市| 内江市| 祁门县| 关岭| 大悟县| 贵阳市| 象州县| 舒城县| 鹤壁市| 通化市| 化德县| 嘉兴市| 佛教| 武功县| 来凤县| 台南县| 鄂托克前旗| 柏乡县| 襄城县| 微博| 西丰县| 连平县| 谷城县| 洛扎县| 阿勒泰市| 和平区| 高平市| 浦东新区| 汶上县| 绥阳县| 开平市| 彭州市| 五华县| 竹山县| 无棣县| 河北区| 达州市| 宁城县| 长武县| 通化市| 乐山市|