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

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

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

    邊城愚人

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

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      31 隨筆 :: 0 文章 :: 96 評(píng)論 :: 0 Trackbacks

    ??? ??? 本文主要介紹如何使用簡(jiǎn)單的Spring郵件抽象層來實(shí)現(xiàn)郵件發(fā)送功能,對(duì)于JavaMail中的API并不做介紹。通過對(duì)比JavaMailAPISpring的郵件抽象層,我覺得,Spring的郵件抽象層優(yōu)點(diǎn)就是簡(jiǎn)化了代碼量,并能充分利用IOC功能;缺點(diǎn)就是要使用部分Spring API,使程序與第三方框架耦合。關(guān)于這方面的內(nèi)容,可以參考Spring的參考手冊(cè)。

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

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

    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 )一個(gè)簡(jiǎn)單郵件信息的實(shí)現(xiàn)類 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 的回調(diào)接口,代碼如下:

    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 兩個(gè)包。利用 Spring IOC 優(yōu)勢(shì),可以通過配置文件來配置郵件發(fā)送信息。對(duì)于 MailSender Spring 提供了實(shí)現(xiàn)類 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 中調(diào)用 mailSender ,可以通過 IOC mailSender 注入到 Service 中。這里給出一個(gè)簡(jiǎn)單的使用演示:

    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 不能發(fā)送帶有附件的郵件,這時(shí)就需要 JavaMailSender (它的實(shí)現(xiàn)類也是 JavaMailSenderImpl ), MimeMessagePreparator 的實(shí)現(xiàn)類和部分 JavaMail API mailSender 的配置不變,添加一個(gè)實(shí)現(xiàn) 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/ 常用基礎(chǔ)算法 .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 ,其中添加了兩個(gè)附件。下面就是簡(jiǎn)單的使用方法。

    ?

    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) 評(píng)論(1)  編輯  收藏 所屬分類: Framework

    評(píng)論

    # re: 利用Spring發(fā)送郵件[未登錄] 2008-07-26 22:41 Gavin
    不錯(cuò)。看一下。  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 777爽死你无码免费看一二区| 国产成人精品亚洲| 久久国产精品一区免费下载| 免费人成视频在线观看不卡| 亚洲色欲啪啪久久WWW综合网| 亚洲免费视频播放| 日韩亚洲国产二区| 亚洲国产AV无码一区二区三区| 中文字幕无码成人免费视频| 亚洲午夜精品第一区二区8050| 深夜a级毛片免费无码| 亚洲乱亚洲乱少妇无码| eeuss免费影院| 亚洲91av视频| 亚洲大片免费观看| 亚洲国产区男人本色在线观看| a毛片基地免费全部视频| 亚洲成av人片天堂网无码】| 免费一级毛片在级播放| 精品国产免费人成网站| 夜夜亚洲天天久久| 亚洲AV无码AV吞精久久| 91精品国产免费久久国语麻豆| 亚洲精品人成电影网| 德国女人一级毛片免费| 美女被免费视频网站a| 亚洲色WWW成人永久网址| 日韩中文字幕免费视频| 亚洲日韩国产精品乱-久| 亚洲 综合 国产 欧洲 丝袜| 永久免费av无码网站yy| 亚洲AV成人影视在线观看| 免费不卡中文字幕在线| 七色永久性tv网站免费看| 亚洲免费人成视频观看| 18级成人毛片免费观看| 亚洲人成欧美中文字幕| 中文亚洲AV片在线观看不卡| 久久久久久夜精品精品免费啦| 久久亚洲精品无码av| 久久亚洲AV无码精品色午夜麻|