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

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

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

    隨筆-126  評論-247  文章-5  trackbacks-0
    前言:說過了 Spring 如何發送 Email 之后,接著來說一下,怎么樣用 Spring 來發送帶有附件的郵件,其實實現這個也很簡單,
    Spring 的官方文檔也有給出例子,下面來說下我的實現。

    環境:
    JDK        1.6
      Spring     2.5.6 
    Javamail  1.4.4 
    Maven     3.0.4 
    Myeclipse 8.6.1

    項目結構:


    spring-smtp-mail-attachment.xml
    <beans xmlns=http://www.springframework.org/schema/beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation
    ="http://www.springframework.org/schema/beans  >
      
        
    <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
           
    <!-- 服務器 -->
            
    <property name="host" value="smtp.yeah.net" />
            
    <!-- 端口號 -->
            
    <property name="port" value="25" />
            
    <!-- 用戶名 -->
            
    <property name="username" value="fancydeepin@yeah.net" />
            
    <!--  密碼   -->
            
    <property name="password" value="*********" />
            
    <!-- SMTP服務器驗證 -->
            
    <property name="javaMailProperties">
                
    <props>
                   
    <!-- 驗證身份 -->
                   
    <prop key="mail.smtp.auth">true</prop>
               
    </props>
            
    </property>
        
    </bean>
        
    <!-- 
           目前我用過的EMAIL賬號都是網易的,下面列出網易的SMTP服務器名和端口號:
           網易郵箱          SMTP服務器     SMTP端口    POP3服務器      POP3端口
           @126.com     smtp.126.com      25         pop3.126.com      110
           @163.com     smtp.163.com      25         pop3.163.com      110
           @yeah.net    smtp.yeah.net      25         pop3.yeah.net     110
       
    -->
        
        
    <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
            
    <!-- 發件人email -->
            
    <property name="from" value="fancydeepin@yeah.net" />
            
    <!-- 
             收件人email
            <property name="to" value="to@yeah.net" />
            email主題(標題)
            <property name="subject" value="Subject" />
            email主題內容
            <property name="text">
              <value>ContentText</value>
            </property>
            
    -->
        
    </bean>
        
        
    <bean id="simpleMail" class="com.fancy.util.Email">
            
    <property name="javaMailSender" ref="javaMailSender" />
            
    <property name="simpleMailMessage" ref="simpleMailMessage" />
        
    </bean>
        
    </beans>

    Email.java
    package com.fancy.util;

    import javax.mail.internet.MimeMessage;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    /**
     * -----------------------------------------
     * @文件: Email.java
     * @作者: fancy
     * @郵箱: fancydeepin@yeah.net
     * @時間: 2012-6-12
     * @描述: 發送Email工具類
     * -----------------------------------------
     
    */

    public class Email {
        
        
    private JavaMailSender javaMailSender;
        
    private SimpleMailMessage simpleMailMessage;
        
      
    /**
         * @方法名: sendMail 
         * @參數名:
    @param subject 郵件主題
         * @參數名:
    @param content 郵件主題內容
         * @參數名:
    @param to        收件人Email地址
         * @描述語:  發送郵件
         
    */

        
    public void sendMail(String subject, String content, String to) {
            
            
    try {
                MimeMessage mimeMessage 
    = javaMailSender.createMimeMessage();
              
    /**
                 * new MimeMessageHelper(mimeMessage,true)之true個人見解:
                 * 關于true參數,官方文檔是這樣解釋的:
                 * use the true flag to indicate you need a multipart message
                 * 翻譯過來就是:使用true,以表明你需要多個消息
                 * 再去翻一下MimeMessageHelper的API,找到這樣一句話:
                 * supporting alternative texts, inline elements and attachments
                 * 也就是說,如果要支持內聯元素和附件就必須給定第二個參數為true
                 * 否則拋出 java.lang.IllegalStateException 異常
                 
    */

                MimeMessageHelper messageHelper 
    = new MimeMessageHelper(mimeMessage,true);
                messageHelper.setFrom(simpleMailMessage.getFrom()); 
    //設置發件人Email
                messageHelper.setSubject(subject); //設置郵件主題
                messageHelper.setText(content);   //設置郵件主題內容
                messageHelper.setTo(to);          //設定收件人Email
              /**
                 * ClassPathResource:很明顯就是類路徑資源,我這里的附件是在項目里的,所以需要用ClassPathResource
                 * 如果是系統文件資源就不能用ClassPathResource,而要用FileSystemResource,例:
                 * FileSystemResource file = new FileSystemResource(new File("D:/Readme.txt"));
                 
    */

                ClassPathResource file 
    = new ClassPathResource("attachment/Readme.txt");
              
    /**
                 * MimeMessageHelper的addAttachment方法:
                 * addAttachment(String attachmentFilename, InputStreamSource inputStreamSource)
                 * InputStreamSource是一個接口,ClassPathResource和FileSystemResource都實現了這個接口
                 
    */

                messageHelper.addAttachment(file.getFilename(), file); 
    //添加附件
                javaMailSender.send(mimeMessage);    //發送附件郵件
                
            }
     catch (Exception e) {System.out.println("異常信息:" + e);}
        }

         
    //Spring 依賴注入
        public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
            
    this.simpleMailMessage = simpleMailMessage;
        }

         
    //Spring 依賴注入
        public void setJavaMailSender(JavaMailSender javaMailSender) {
            
    this.javaMailSender = javaMailSender;
        }

    }


    Junit Test:EmailTest.java
    package com.fancy.test;

    import junit.framework.TestCase;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.fancy.util.Email;
    /**
     * -----------------------------------------
     * @文件: EmailTest.java
     * @作者: fancy
     * @郵箱: fancydeepin@yeah.net
     * @時間: 2012-6-12
     * @描述: Junit測試,運行將發送一封email
     * -----------------------------------------
     
    */

    public class EmailTest extends TestCase {

        
    public void testSendMail() {
            ApplicationContext context 
    = new ClassPathXmlApplicationContext("spring-smtp-mail-attachment.xml");
            Email mail 
    = (Email)context.getBean("simpleMail");
            mail.sendMail("Spring SMTP Mail With Attachment Subject", "Spring SMTP Mail With Attachment Text Content", "
    fancyzero@yeah.net");
            //mail.sendMail("標題", "內容", "收件人郵箱");
        }

    }

    pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation
    ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      
    <modelVersion>4.0.0</modelVersion>
      
    <groupId>com.fancy</groupId>
      
    <artifactId>spring-mail-attachment-example</artifactId>
      
    <version>1.0</version>
      
    <packaging>jar</packaging>
      
    <name>spring-mail-attachment-example</name>
      
    <url>http://maven.apache.org</url>
      
    <properties>
        
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      
    </properties>

      
    <dependencies>
        
    <!-- Spring framework -->
        
    <dependency>
          
    <groupId>org.springframework</groupId>
          
    <artifactId>spring</artifactId>
          
    <version>2.5.6</version>
        
    </dependency>
        
    <!-- Javamail API -->
        
    <dependency>
          
    <groupId>javax.mail</groupId>
          
    <artifactId>mail</artifactId>
          
    <version>1.4.4</version>
        
    </dependency>
        
    <!-- Junit -->
        
    <dependency>
          
    <groupId>junit</groupId>
          
    <artifactId>junit</artifactId>
          
    <version>4.1</version>
          
    <scope>test</scope>
        
    </dependency>
      
    </dependencies>
      
    </project>

    運行一下 Junit 的 EmailTest.java 收到郵件:











    測試完成,英文、中文、壓縮包 附件都能正常發送和下載。


    下面附上 Spring 官方文檔對Attachment的示例:

    24.3.1.1 Attachments

    The following example shows you how to use the MimeMessageHelper to send an email along with a single JPEG image attachment.

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost(
    "mail.host.com");

    MimeMessage message 
    = sender.createMimeMessage();

    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(
    "test@host.com");

    helper.setText(
    "Check out this image!");

    // let's attach the infamous windows Sample file (this time copied to c:/)
    FileSystemResource file = new FileSystemResource(new File("c:/Sample.jpg"));
    helper.addAttachment(
    "CoolImage.jpg", file);

    sender.send(message);



     



      
    posted on 2012-06-12 00:29 fancydeepin 閱讀(4983) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 免费A级毛片无码专区| 久久青青成人亚洲精品| 久久精品国产免费观看| 一级大黄美女免费播放| 亚洲国产精品日韩av不卡在线| 亚洲国产第一页www| 国产亚洲色婷婷久久99精品91| 日本一区免费电影| 国产在线a免费观看| 99久久综合精品免费| a级黄色毛片免费播放视频| 国产成人亚洲午夜电影| 亚洲色大成网站www永久网站| 久久亚洲精品成人无码网站| 国产AV无码专区亚洲AV男同| 国产亚洲一区二区三区在线不卡| 深夜国产福利99亚洲视频| 国产精品久久免费视频| 午夜私人影院免费体验区| 国产桃色在线成免费视频| 国产人成免费视频网站| 精品无码无人网站免费视频| 三年片在线观看免费大全电影| 在线涩涩免费观看国产精品| a级毛片免费高清毛片视频| 一级毛片完整版免费播放一区| 深夜A级毛片视频免费| 美女视频黄a视频全免费网站色 | 国产免费区在线观看十分钟| 丁香六月婷婷精品免费观看| 特黄特色大片免费| 一级做a爱片特黄在线观看免费看| 一区二区三区免费视频网站 | 又粗又大又猛又爽免费视频 | 亚洲丰满熟女一区二区哦| 亚洲欧美日韩综合久久久久| 亚洲精品无码专区| 亚洲精品无码你懂的| 怡红院亚洲红怡院在线观看| 乱淫片免费影院观看| 国产猛男猛女超爽免费视频|