<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0, comments - 77, trackbacks - 0, articles - 356
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    spring mail發送

    Posted on 2007-05-11 17:58 semovy 閱讀(1187) 評論(0)  編輯  收藏 所屬分類: 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();
           
            //測試發送只有文本信息的簡單測試
            springMail.sendTextMail(sender);
           
            //測試發送帶附件的郵件
            springMail.sendMimeMessage(sender);
        }
       
        /** *//**
         * 測試發送只有文本信息的簡單測試
         * @param sender 郵件發送器
         * @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的簡單測試");
            sender.send(mail);
           
            System.out.println("成功發送文本文件!");
        }
       
        /** *//**
         * 發送帶附件的郵件
         * @param sender 郵件發送器
         * @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發送帶附件的郵件", "gb2312");
                   
                    Multipart mp = new MimeMultipart();
                   
                    //向Multipart添加正文

                    MimeBodyPart content = new MimeBodyPart();
                    content.setText("內含spring郵件發送的例子,請查收!");
                   
                    //向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());
                }
            };

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

    }


    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>


     剛才發現一bug,當附件名為中文時,會出現中文亂碼問題,對sendMimeMessage方法進行了部分修改,如下:
       

                   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);
                    }
    主站蜘蛛池模板: 久久精品毛片免费观看| 国产无遮挡无码视频免费软件| 精品无码无人网站免费视频| 中文字幕不卡亚洲 | 2015日韩永久免费视频播放| 国产成A人亚洲精V品无码| 亚洲av无码片区一区二区三区| 国产成人亚洲综合无| 天天摸夜夜摸成人免费视频| 亚洲AV无码国产精品色午友在线| 在线观看片免费人成视频播放| 77777亚洲午夜久久多人| 国产成人无码免费网站| 亚洲精品无码久久久久sm| 免费在线中文日本| 无码人妻一区二区三区免费| 亚洲一线产区二线产区精华| 黄页网站在线观看免费高清| 亚洲欧美日本韩国| 亚洲精品一级无码鲁丝片| 青柠影视在线观看免费| 亚洲精品国产精品乱码不卡| 国产黄色片免费看| 亚洲∧v久久久无码精品| 国产精品免费大片一区二区| 亚洲色偷偷综合亚洲AVYP| 99xxoo视频在线永久免费观看| 亚洲国产精彩中文乱码AV| 无码AV片在线观看免费| 久久精品国产亚洲AV久| 四虎永久免费地址在线观看| 亚洲无人区视频大全| 青青青国产免费一夜七次郎| 特色特黄a毛片高清免费观看| 免费v片视频在线观看视频| 国产午夜精品理论片免费观看 | 中文字幕在线视频免费观看 | 亚洲Av无码精品色午夜| 免费毛片a在线观看67194| 黄色免费在线网址| 97亚洲熟妇自偷自拍另类图片|