邊城愚人

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

            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 閱讀(2701) 評論(1)  編輯  收藏 所屬分類: Framework

          評論

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

          主站蜘蛛池模板: 涟源市| 宣恩县| 芜湖县| 广东省| 宾阳县| 盘山县| 长汀县| 大荔县| 沾益县| 旺苍县| 黄平县| 泸水县| 邹平县| 沂南县| 南昌县| 故城县| 左贡县| 阿拉善盟| 林甸县| 曲麻莱县| 桂平市| 昌宁县| 西城区| 安达市| 永州市| 英吉沙县| 顺昌县| 中宁县| 阿城市| 祁阳县| 饶平县| 富平县| 鲜城| 怀仁县| 抚远县| 玉树县| 澄江县| 铜梁县| 黄骅市| 白水县| 新郑市|