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

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

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

    邊城愚人

    如果我不在邊城,我一定是在前往邊城的路上。

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      31 隨筆 :: 0 文章 :: 96 評論 :: 0 Trackbacks

    ??? ??? 本文主要介紹如何使用簡單的Spring郵件抽象層來實現郵件發送功能,對于JavaMail中的API并不做介紹。通過對比JavaMailAPISpring的郵件抽象層,我覺得,Spring的郵件抽象層優點就是簡化了代碼量,并能充分利用IOC功能;缺點就是要使用部分Spring API,使程序與第三方框架耦合。關于這方面的內容,可以參考Spring的參考手冊。

    ??? ??? 閑言少敘,現在就說說Spring郵件抽象層。這里將要用的接口和類有:

    ??? ??? 1 MailSender ,它是發送郵件的主要接口,代碼如下:

    public interface MailSender {

    /**

    * Send the given simple mail message.

    * @param simpleMessage the message to send

    * @throws org.springframework.mail.MailParseException

    * in case of failure when parsing the message

    * @throws org.springframework.mail.MailAuthenticationException

    * in case of authentication failure

    * @throws org.springframework.mail.MailSendException

    * in case of failure when sending the message

    */

    void send(SimpleMailMessage simpleMessage) throws MailException;


    /**

    * Send the given array of simple mail messages in batch.

    * @param simpleMessages the messages to send

    * @throws org.springframework.mail.MailParseException

    * in case of failure when parsing a message

    * @throws org.springframework.mail.MailAuthenticationException

    * in case of authentication failure

    * @throws org.springframework.mail.MailSendException

    * in case of failure when sending a message

    */

    void send(SimpleMailMessage[] simpleMessages) throws MailException;

    }

    ??? ??? 2 )一個簡單郵件信息的實現類 SimpleMailMessage 。

    ??? ??? 3 JavaMailSender 接口,提供使用 JavaMail MimeMessage ,代碼如下:

    public interface JavaMailSender extends MailSender {


    /**

    * Create a new JavaMail MimeMessage for the underlying JavaMail Session

    * of this sender. Needs to be called to create MimeMessage instances

    * that can be prepared by the client and passed to send(MimeMessage).

    * @return the new MimeMessage instance

    * @see #send(MimeMessage)

    * @see #send(MimeMessage[])

    */

    MimeMessage createMimeMessage();


    /**

    * Create a new JavaMail MimeMessage for the underlying JavaMail Session

    * of this sender, using the given input stream as the message source.

    * @param contentStream the raw MIME input stream for the message

    * @return the new MimeMessage instance

    * @throws org.springframework.mail.MailParseException

    * in case of message creation failure

    */

    MimeMessage createMimeMessage(InputStream contentStream) throws MailException;


    /**

    * Send the given JavaMail MIME message.

    * The message needs to have been created with {@link #createMimeMessage()} .

    * @param mimeMessage message to send

    * @throws org.springframework.mail.MailAuthenticationException

    * in case of authentication failure

    * @throws org.springframework.mail.MailSendException

    * in case of failure when sending the message

    * @see #createMimeMessage

    */

    void send(MimeMessage mimeMessage) throws MailException;


    /**

    * Send the given array of JavaMail MIME messages in batch.

    * The messages need to have been created with {@link #createMimeMessage()} .

    * @param mimeMessages messages to send

    * @throws org.springframework.mail.MailAuthenticationException

    * in case of authentication failure

    * @throws org.springframework.mail.MailSendException

    * in case of failure when sending a message

    * @see #createMimeMessage

    */

    void send(MimeMessage[] mimeMessages) throws MailException;


    /**

    * Send the JavaMail MIME message prepared by the given MimeMessagePreparator.

    * <p> Alternative way to prepare MimeMessage instances, instead of

    * {@link #createMimeMessage()} and {@link #send(MimeMessage)} calls.

    * Takes care of proper exception conversion.

    * @param mimeMessagePreparator the preparator to use

    * @throws org.springframework.mail.MailPreparationException

    * in case of failure when preparing the message

    * @throws org.springframework.mail.MailParseException

    * in case of failure when parsing the message

    * @throws org.springframework.mail.MailAuthenticationException

    * in case of authentication failure

    * @throws org.springframework.mail.MailSendException

    * in case of failure when sending the message

    */

    void send(MimeMessagePreparator mimeMessagePreparator) throws MailException;


    /**

    * Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.

    * <p> Alternative way to prepare MimeMessage instances, instead of

    * {@link #createMimeMessage()} and {@link #send(MimeMessage[])} calls.

    * Takes care of proper exception conversion.

    * @param mimeMessagePreparators the preparator to use

    * @throws org.springframework.mail.MailPreparationException

    * in case of failure when preparing a message

    * @throws org.springframework.mail.MailParseException

    * in case of failure when parsing a message

    * @throws org.springframework.mail.MailAuthenticationException

    * in case of authentication failure

    * @throws org.springframework.mail.MailSendException

    * in case of failure when sending a message

    */

    void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException;

    }

    ??? ??? 4 MimeMessagePreparator ,支持 MimeMessage 的回調接口,代碼如下:

    public interface MimeMessagePreparator {

    /**

    * Prepare the given new MimeMessage instance.

    * @param mimeMessage the message to prepare

    * @throws javax.mail.MessagingException passing any exceptions thrown by MimeMessage

    * methods through for automatic conversion to the MailException hierarchy

    * @throws java.io.IOException passing any exceptions thrown by MimeMessage methods

    * through for automatic conversion to the MailException hierarchy

    * @throws Exception if mail preparation failed, for example when a

    * Velocity template cannot be rendered for the mail text

    */

    void prepare(MimeMessage mimeMessage) throws Exception;

    }

    ??? ??? 使用 JavaMail ,需要依賴 mail.jar activation.jar 兩個包。利用 Spring IOC 優勢,可以通過配置文件來配置郵件發送信息。對于 MailSender , Spring 提供了實現類 JavaMailSenderImpl ,可以如下配置 MailSender 信息。

    < bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl" >

    < property name = "host" value = "smtp.126.com" ></ property >

    < property name = "javaMailProperties" >

    < props >

    < prop key = "mail.smtp.auth" > true </ prop >

    < prop key = "mail.smtp.timeout" > 20000 </ prop >

    </ props >

    </ property >

    < property name = "username" value = "kafka0102" ></ property >

    < property name = "password" value = "****" ></ property >

    </ bean >

    ??? ??? 可以想到,如果是在 Service 中調用 mailSender ,可以通過 IOC mailSender 注入到 Service 中。這里給出一個簡單的使用演示:

    BeanFactory bf = new ClassPathXmlApplicationContext( "service-applicationContext.xml" );

    MailSender ms = (MailSender)bf.getBean( "mailSender" );

    SimpleMailMessage smm = new SimpleMailMessage();

    smm.setTo( "kafka0102@126.com" );

    smm.setFrom( "kafka0102@126.com" );

    smm.setSubject( "hello" );

    smm.setText( "I am you" );

    ms.send(smm);

    ??? ??? 其中, SimpleMailMessage 也可以通過配置文件配置使用到的屬性。 SimpleMailMessage 不能發送帶有附件的郵件,這時就需要 JavaMailSender (它的實現類也是 JavaMailSenderImpl ), MimeMessagePreparator 的實現類和部分 JavaMail API mailSender 的配置不變,添加一個實現 MimeMessagePreparator 的類 OneMimeMessagePreparator ,代碼如下。

    import java.util.Date;

    import javax.activation.DataHandler;

    import javax.activation.FileDataSource;

    import javax.mail.Message;

    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.mail.javamail.MimeMessagePreparator;

    public class OneMimeMessagePreparator implements MimeMessagePreparator{


    public void prepare(MimeMessage mm) throws Exception {

    mm.setRecipient(Message.RecipientType. TO , new InternetAddress( "kafka0102@126.com" ));

    mm.setFrom( new InternetAddress( "kafka0102@126.com" ));

    mm.setSubject( "I am you" );

    Multipart mp = new MimeMultipart();

    MimeBodyPart mbp = new MimeBodyPart();

    mbp.setText( "hello kafka0102" );

    mp.addBodyPart(mbp);

    String[] files = new String[]{ "/home/kafka0102/Document/ 常用基礎算法 .txt" , "/home/kafka0102/Document/21164.html" };

    for (String f : files){

    MimeBodyPart mbpFile = new MimeBodyPart();

    FileDataSource fds = new FileDataSource(f);

    mbpFile.setDataHandler( new DataHandler(fds));

    mbpFile.setFileName(fds.getName());

    mp.addBodyPart(mbpFile);

    }

    mm.setContent(mp);

    mm.setSentDate( new Date());

    }

    }

    ??? ??? OneMimeMessagePreparator 的功能就是配置 MimeMessage ,其中添加了兩個附件。下面就是簡單的使用方法。

    ?

    BeanFactory bf = new ClassPathXmlApplicationContext("service-applicationContext.xml");

    JavaMailSender ms = (JavaMailSender)bf.getBean("mailSender");

    ms.send(new OneMimeMessagePreparator());

    ??? ?

    posted on 2007-07-15 20:41 kafka0102 閱讀(2711) 評論(1)  編輯  收藏 所屬分類: Framework

    評論

    # re: 利用Spring發送郵件[未登錄] 2008-07-26 22:41 Gavin
    不錯??匆幌?。  回復  更多評論
      

    主站蜘蛛池模板: 一级毛片大全免费播放下载| 国产精品亚洲片在线va| 成人国产网站v片免费观看| 成年女人永久免费观看片| 亚洲男人天堂2018av| 国产福利在线免费| 亚洲精品第一国产综合野| 成人男女网18免费视频| 亚洲AV永久无码精品一福利| 日韩免费视频播放| 亚洲AⅤ男人的天堂在线观看| 永久黄网站色视频免费直播 | 国产精品成人四虎免费视频| 亚洲精品欧美综合四区| 国产美女做a免费视频软件| 一区二区三区视频免费观看| 亚洲人成77777在线播放网站| 四虎国产精品永久免费网址| 亚洲欧洲日产国码www| 在线免费观看a级片| 色多多A级毛片免费看| 亚洲爆乳无码一区二区三区| 91免费福利精品国产| 亚洲日韩av无码中文| 亚洲人成无码网WWW| 亚洲精品免费视频| 亚洲一区二区三区写真| 免费国产成人午夜私人影视| 黄色免费在线网站| 亚洲中文字幕无码一去台湾| 亚洲精品麻豆av| 免费国产黄网站在线观看| 亚洲午夜精品久久久久久app| 四虎影视永久免费观看| 免费看搞黄视频网站| 亚洲色丰满少妇高潮18p| 亚洲日韩精品A∨片无码| av无码免费一区二区三区| 日韩亚洲人成网站| 亚洲色四在线视频观看| 国产成人aaa在线视频免费观看|