锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产日韩a在线播放,91亚洲国产成人久久精品网站,亚洲日韩中文字幕在线播放http://m.tkk7.com/purecoffee/archive/2005/11/24/21237.html娓呭挅娓呭挅Thu, 24 Nov 2005 02:37:00 GMThttp://m.tkk7.com/purecoffee/archive/2005/11/24/21237.htmlhttp://m.tkk7.com/purecoffee/comments/21237.htmlhttp://m.tkk7.com/purecoffee/archive/2005/11/24/21237.html#Feedback0http://m.tkk7.com/purecoffee/comments/commentRss/21237.htmlhttp://m.tkk7.com/purecoffee/services/trackbacks/21237.html Chapter 21. Sending Email with Spring mail abstraction layer Prev     Next

Chapter 21. Sending Email with Spring mail abstraction layer

21.1. Introduction

Spring provides a higher level of abstraction for sending electronic mail which shields the user from the specifics of underlying mailing system and is responsible for a low level resource handling on behalf of the client.

21.2. Spring mail abstraction structure

The main package of Spring mail abstraction layer is org.springframework.mail package. It contains central interface for sending emails called MailSender and the value object which encapsulates properties of a simple mail such as from, to, cc, subject, text called SimpleMailMessage. This package also contains a hierarchy of checked exceptions which provide a higher level of abstraction over the lower level mail system exceptions with the root exception being MailException.Please refer to JavaDocs for more information on mail exception hierarchy.

Spring also provides a sub-interface of MailSender for specialized JavaMail features such as MIME messages, namely org.springframework.mail.javamail.JavaMailSender It also provides a callback interface for preparation of JavaMail MIME messages, namely org.springframework.mail.javamail.MimeMessagePreparator

MailSender:

public interface MailSender {

    /**
     * Send the given simple mail message.
     * @param simpleMessage message to send
     * @throws MailException in case of message, authentication, or send errors
     */
    public void send(SimpleMailMessage simpleMessage) throws MailException;

    /**
     * Send the given array of simple mail messages in batch.
     * @param simpleMessages messages to send
     * @throws MailException in case of message, authentication, or send errors
     */
    public void send(SimpleMailMessage[] simpleMessages) throws MailException;

}

JavaMailSender:

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[])
     */
    public MimeMessage createMimeMessage();

    /**
     * Send the given JavaMail MIME message.
     * The message needs to have been created with createMimeMessage.
     * @param mimeMessage message to send
     * @throws MailException in case of message, authentication, or send errors
     * @see #createMimeMessage
     */
    public void send(MimeMessage mimeMessage) throws MailException;

    /**
     * Send the given array of JavaMail MIME messages in batch.
     * The messages need to have been created with createMimeMessage.
     * @param mimeMessages messages to send
     * @throws MailException in case of message, authentication, or send errors
     * @see #createMimeMessage
     */
    public void send(MimeMessage[] mimeMessages) throws MailException;

    /**
     * Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
     * Alternative way to prepare MimeMessage instances, instead of createMimeMessage
     * and send(MimeMessage) calls. Takes care of proper exception conversion.
     * @param mimeMessagePreparator the preparator to use
     * @throws MailException in case of message, authentication, or send errors
     */
    public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException;

    /**
     * Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.
     * Alternative way to prepare MimeMessage instances, instead of createMimeMessage
     * and send(MimeMessage[]) calls. Takes care of proper exception conversion.
     * @param mimeMessagePreparators the preparator to use
     * @throws MailException in case of message, authentication, or send errors
     */
    public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException;

}

MimeMessagePreparator:

public interface MimeMessagePreparator {

    /**
     * Prepare the given new MimeMessage instance.
     * @param mimeMessage the message to prepare
     * @throws MessagingException passing any exceptions thrown by MimeMessage
     * methods through for automatic conversion to the MailException hierarchy
     */
    void prepare(MimeMessage mimeMessage) throws MessagingException;

}

21.3. Using Spring mail abstraction

Let's assume there is a business interface called OrderManager

public interface OrderManager {

    void placeOrder(Order order);
}

and there is a use case that says that an email message with order number would need to be generated and sent to a customer placing that order. So for this purpose we want to use MailSender and SimpleMailMessage

Please note that as usual, we work with interfaces in the business code and let Spring IoC container take care of wiring of all the collaborators for us.

Here is the implementation of OrderManager

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class OrderManagerImpl implements OrderManager {

    private MailSender mailSender;
    private SimpleMailMessage message;

    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void setMessage(SimpleMailMessage message) {
        this.message = message;
    }

    public void placeOrder(Order order) {

        //... * Do the business calculations....
        //... * Call the collaborators to persist the order

        //Create a thread safe "sandbox" of the message
        SimpleMailMessage msg = new SimpleMailMessage(this.message);
        msg.setTo(order.getCustomer().getEmailAddress());
        msg.setText(
            "Dear "
                + order.getCustomer().getFirstName()
                + order.getCustomer().getLastName()
                + ", thank you for placing order. Your order number is "
                + order.getOrderNumber());
        try{
            mailSender.send(msg);
        }
        catch(MailException ex) {
            //log it and go on
            System.err.println(ex.getMessage());            
        }
    }
}

Here is what the bean definitions for the code above would look like:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host"><value>mail.mycompany.com</value></property>
</bean>

<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
  <property name="from"><value>customerservice@mycompany.com</value></property>
  <property name="subject"><value>Your order</value></property>
</bean>

<bean id="orderManager" class="com.mycompany.businessapp.support.OrderManagerImpl">
  <property name="mailSender"><ref bean="mailSender"/></property>
  <property name="message"><ref bean="mailMessage"/></property>
</bean>

Here is the implementation of OrderManager using MimeMessagePreparator callback interface. Please note that the mailSender property is of type JavaMailSender in this case in order to be able to use JavaMail MimeMessage:

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;

public class OrderManagerImpl implements OrderManager {

    private JavaMailSender mailSender;
    
    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void placeOrder(final Order order) {

        //... * Do the business calculations....
        //... * Call the collaborators to persist the order
        
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws MessagingException {
                mimeMessage.setRecipient(Message.RecipientType.TO, 
                        new InternetAddress(order.getCustomer().getEmailAddress()));
                mimeMessage.setFrom(new InternetAddress("mail@mycompany.com"));
                mimeMessage.setText(
                    "Dear "
                        + order.getCustomer().getFirstName()
                        + order.getCustomer().getLastName()
                        + ", thank you for placing order. Your order number is "
                        + order.getOrderNumber());
            }
        };
        try{
            mailSender.send(preparator);
        }
        catch (MailException ex) {
            //log it and go on
            System.err.println(ex.getMessage());            
        }
    }
}

If you want to use JavaMail MimeMessage to the full power, the MimeMessagePreparator is available at your fingertips.

Please note that the mail code is a crosscutting concern and is a perfect candidate for refactoring into a custom Spring AOP advice, which then could easily be applied to OrderManager target. Please see the AOP chapter.

21.3.1. Pluggable MailSender implementations

Spring comes with two MailSender implementations out of the box - the JavaMail implementation and the implementation on top of Jason Hunter's MailMessage class that's included in http://servlets.com/cos (com.oreilly.servlet). Please refer to JavaDocs for more information.

21.4. Using the JavaMail MimeMessageHelper

One of the components that comes in pretty handy when dealing with JavaMail messages is the org.springframework.mail.javamail.MimeMessageHelper. It prevents you from having to use the nasty APIs the the javax.mail.internet classes. A couple of possible scenarios:

21.4.1. Creating a simple MimeMessage and sending it

Using the MimeMessageHelper it's pretty easy to setup and send a MimeMessage:

// of course you would setup the mail sender using 
// DI in any real-world cases
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");

MimeMessage message = sender.createMimeMesage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("test@host.com");
helper.setText("Thank you for ordering!");

sender.send(message);
			

21.4.2. Sending attachments and inline resources

Email allow for attachments, but also for inline resources in multipart messages. Inline resources could for example be images or stylesheet you want to use in your message, but don't want displayed as attachment. The following shows you how to use the MimeMessageHelper to send an email along with an inline image.

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

MimeMessage message = sender.createMimeMesage();

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

// use the true flag to indicate the text included is HTML
helper.setText(
  "<html><body><img src='cid:identifier1234'></body></html>"
  true);

// let's include the infamous windows Sample file (this time copied to c:/)
FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addInline("identifier1234", res);

// if you would need to include the file as an attachment, use
// addAttachment() methods on the MimeMessageHelper

sender.send(message);
			

Inline resources are added to the mime message using the Content-ID specified as you've seen just now (identifier1234 in this case). The order in which you're adding the text and the resource are VERY important. First add the text and after that the resources. If you're doing it the other way around, it won't work!



娓呭挅 2005-11-24 10:37 鍙戣〃璇勮
]]>
榪欎釜欏甸潰鎬庝箞鑰佸笀鍒峰晩鍒風(fēng)殑銆傘傘傘傘傘傘?/title><link>http://m.tkk7.com/purecoffee/archive/2005/11/18/20375.html</link><dc:creator>娓呭挅</dc:creator><author>娓呭挅</author><pubDate>Fri, 18 Nov 2005 02:22:00 GMT</pubDate><guid>http://m.tkk7.com/purecoffee/archive/2005/11/18/20375.html</guid><wfw:comment>http://m.tkk7.com/purecoffee/comments/20375.html</wfw:comment><comments>http://m.tkk7.com/purecoffee/archive/2005/11/18/20375.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/purecoffee/comments/commentRss/20375.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/purecoffee/services/trackbacks/20375.html</trackback:ping><description><![CDATA[榪欎釜欏甸潰鎬庝箞鑰佸笀鍒峰晩鍒風(fēng)殑銆傘傘傘傘傘傘傚彲鎬滀竴涓嬪伓鐨勭溂鐫?img src ="http://m.tkk7.com/purecoffee/aggbug/20375.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/purecoffee/" target="_blank">娓呭挅</a> 2005-11-18 10:22 <a href="http://m.tkk7.com/purecoffee/archive/2005/11/18/20375.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Spring,FreeMarkerhttp://m.tkk7.com/purecoffee/archive/2005/11/18/20373.html娓呭挅娓呭挅Fri, 18 Nov 2005 02:21:00 GMThttp://m.tkk7.com/purecoffee/archive/2005/11/18/20373.htmlhttp://m.tkk7.com/purecoffee/comments/20373.htmlhttp://m.tkk7.com/purecoffee/archive/2005/11/18/20373.html#Feedback0http://m.tkk7.com/purecoffee/comments/commentRss/20373.htmlhttp://m.tkk7.com/purecoffee/services/trackbacks/20373.html1銆俿pring鏃ユ湡綾誨瀷瑕佸啓鑷繁鐨勬柟娉曡鐩栧師鏈夌殑緇戝畾綾誨瀷
渚嬪鐢熸棩鍩熺殑緇戝畾
 protected void initBinder(HttpServletRequest arg0, ServletRequestDataBinder arg1) throws Exception {
  arg1.registerCustomEditor(Date.class,"birthday",getCustomDateEditor());
  super.initBinder(arg0, arg1);
 }

2.trim
The string without leading and trailing white-space. Example: (${"  green mouse  "?trim})  
The output: (green mouse)  
  3.split
  
 <#list "someMOOtestMOOtext"?split("MOO") as x>
- ${x}
</#list>   
willprint :
- some
- test
- text
 



娓呭挅 2005-11-18 10:21 鍙戣〃璇勮
]]>
姊︽兂http://m.tkk7.com/purecoffee/archive/2005/11/15/19828.html娓呭挅娓呭挅Tue, 15 Nov 2005 04:12:00 GMThttp://m.tkk7.com/purecoffee/archive/2005/11/15/19828.htmlhttp://m.tkk7.com/purecoffee/comments/19828.htmlhttp://m.tkk7.com/purecoffee/archive/2005/11/15/19828.html#Feedback1http://m.tkk7.com/purecoffee/comments/commentRss/19828.htmlhttp://m.tkk7.com/purecoffee/services/trackbacks/19828.html
娉紝榪欓〉闈㈡庝箞鑰佽嚜鍔ㄥ埛鏂幫紝錛岋紝錛?img src ="http://m.tkk7.com/purecoffee/aggbug/19828.html" width = "1" height = "1" />

娓呭挅 2005-11-15 12:12 鍙戣〃璇勮
]]>
榪欎釜欏甸潰涓嶉敊http://m.tkk7.com/purecoffee/archive/2005/11/13/19596.html娓呭挅娓呭挅Sun, 13 Nov 2005 07:25:00 GMThttp://m.tkk7.com/purecoffee/archive/2005/11/13/19596.htmlhttp://m.tkk7.com/purecoffee/comments/19596.htmlhttp://m.tkk7.com/purecoffee/archive/2005/11/13/19596.html#Feedback1http://m.tkk7.com/purecoffee/comments/commentRss/19596.htmlhttp://m.tkk7.com/purecoffee/services/trackbacks/19596.html

娓呭挅 2005-11-13 15:25 鍙戣〃璇勮
]]>
榪欓噷寰堝揩http://m.tkk7.com/purecoffee/archive/2005/11/11/19296.html娓呭挅娓呭挅Fri, 11 Nov 2005 06:55:00 GMThttp://m.tkk7.com/purecoffee/archive/2005/11/11/19296.htmlhttp://m.tkk7.com/purecoffee/comments/19296.htmlhttp://m.tkk7.com/purecoffee/archive/2005/11/11/19296.html#Feedback3http://m.tkk7.com/purecoffee/comments/commentRss/19296.htmlhttp://m.tkk7.com/purecoffee/services/trackbacks/19296.html

娓呭挅 2005-11-11 14:55 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 国产精品免费网站| 成人亚洲国产va天堂| 国产一级做a爱免费视频| 蜜臀AV免费一区二区三区| 一级做a爰片久久毛片免费看 | 亚洲码欧美码一区二区三区| 亚洲成亚洲乱码一二三四区软件| 四虎1515hm免费国产| 成人A级毛片免费观看AV网站| 日韩电影免费观看| 99re6在线视频精品免费| 免费大片黄在线观看| 亚洲AV无码国产精品永久一区| 亚洲乱码中文论理电影| 久久久久亚洲AV片无码下载蜜桃 | 国产一级高青免费| 一级毛片a免费播放王色电影| 亚洲AV无码一区二区三区性色 | 性生交片免费无码看人| 亚洲一区在线免费观看| 69国产精品视频免费| 99精品视频免费在线观看| 免费人成黄页在线观看日本| 中文字幕免费在线看线人动作大片 | 久久av免费天堂小草播放| 搜日本一区二区三区免费高清视频| 亚洲精品9999久久久久无码| 在线aⅴ亚洲中文字幕| 亚洲国产成a人v在线观看| 亚洲午夜精品在线| 亚洲国产成a人v在线观看| 亚洲日韩AV一区二区三区中文| 国产91在线|亚洲| 亚洲综合激情五月色一区| 亚洲欧美日韩综合久久久| 亚洲精品无码久久久久YW| 亚洲国产精品无码中文lv| 亚洲乱码无人区卡1卡2卡3| 九九精品国产亚洲AV日韩| 激情吃奶吻胸免费视频xxxx| 全部一级一级毛片免费看|