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

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

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

    邊城愚人

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

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

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

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

    ??? ??? 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)類(lèi) 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 ,需要依賴(lài) mail.jar activation.jar 兩個(gè)包。利用 Spring IOC 優(yōu)勢(shì),可以通過(guò)配置文件來(lái)配置郵件發(fā)送信息。對(duì)于 MailSender Spring 提供了實(shí)現(xiàn)類(lèi) 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 ,可以通過(guò) 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 也可以通過(guò)配置文件配置使用到的屬性。 SimpleMailMessage 不能發(fā)送帶有附件的郵件,這時(shí)就需要 JavaMailSender (它的實(shí)現(xiàn)類(lèi)也是 JavaMailSenderImpl ), MimeMessagePreparator 的實(shí)現(xiàn)類(lèi)和部分 JavaMail API mailSender 的配置不變,添加一個(gè)實(shí)現(xiàn) MimeMessagePreparator 的類(lèi) 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 閱讀(2700) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): Framework

    評(píng)論

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

    主站蜘蛛池模板: 亚洲av成人无码久久精品| 亚洲精品国产摄像头| 视频免费1区二区三区| 成人黄色免费网站| 亚洲αv久久久噜噜噜噜噜| 亚洲国产AV无码一区二区三区| 无码人妻一区二区三区免费n鬼沢 无码人妻一区二区三区免费看 | 午夜男人一级毛片免费| 亚洲AV成人无码久久精品老人| 黄色网址在线免费观看| 毛片免费观看的视频在线| 亚洲AV人无码激艳猛片| 一区视频免费观看| 国产婷婷高清在线观看免费| 亚洲Av高清一区二区三区| 日本一道本不卡免费 | 亚洲国产成AV人天堂无码| 国产一级片免费看| 激情综合色五月丁香六月亚洲| 亚洲avav天堂av在线网毛片| 成人在线免费看片| 337p日本欧洲亚洲大胆艺术| 国产在线观看免费视频软件| 亚洲人妻av伦理| 香蕉97碰碰视频免费| 国产成人涩涩涩视频在线观看免费| 久久精品国产亚洲AV忘忧草18| 亚洲国产精品免费在线观看| 亚洲国产精品VA在线看黑人| 国产男女爽爽爽免费视频| 亚洲国产精品专区在线观看| 亚洲AV无码国产剧情| 在线观看成人免费视频| 色在线亚洲视频www| 又黄又爽又成人免费视频| 一区二区三区亚洲| 暖暖在线视频免费视频| 亚洲第一AV网站| 成在人线av无码免费高潮喷水| 中文字幕亚洲图片| 中文在线观看国语高清免费|