﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-你们不要倒闭了啊！</title><link>http://www.blogjava.net/purecoffee/</link><description /><language>zh-cn</language><lastBuildDate>Sun, 05 Apr 2026 17:37:36 GMT</lastBuildDate><pubDate>Sun, 05 Apr 2026 17:37:36 GMT</pubDate><ttl>60</ttl><item><title>java 的 Des加密和解密</title><link>http://www.blogjava.net/purecoffee/archive/2005/11/28/21677.html</link><dc:creator>清咖</dc:creator><author>清咖</author><pubDate>Mon, 28 Nov 2005 07:08:00 GMT</pubDate><guid>http://www.blogjava.net/purecoffee/archive/2005/11/28/21677.html</guid><wfw:comment>http://www.blogjava.net/purecoffee/comments/21677.html</wfw:comment><comments>http://www.blogjava.net/purecoffee/archive/2005/11/28/21677.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/purecoffee/comments/commentRss/21677.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/purecoffee/services/trackbacks/21677.html</trackback:ping><description><![CDATA[<P>import java.security.Key;<BR>import java.security.SecureRandom;<BR>import javax.crypto.Cipher;<BR>import javax.crypto.KeyGenerator;<BR>import sun.misc.BASE64Decoder;<BR>import sun.misc.BASE64Encoder;</P>
<P>/**<BR>&nbsp;*<BR>&nbsp;* 使用DES加密与解密,可对byte[],String类型进行加密与解密<BR>&nbsp;* 密文可使用String,byte[]存储.<BR>&nbsp;*<BR>&nbsp;* 方法:<BR>&nbsp;* void getKey(String strKey)从strKey的字条生成一个Key<BR>&nbsp;*<BR>&nbsp;* String getEncString(String strMing)对strMing进行加密,返回String密文<BR>&nbsp;* String getDesString(String strMi)对strMin进行解密,返回String明文<BR>&nbsp;*<BR>&nbsp;*byte[] getEncCode(byte[] byteS)byte[]型的加密<BR>&nbsp;*byte[] getDesCode(byte[] byteD)byte[]型的解密<BR>&nbsp;*/</P>
<P>public class DesEncrypt {<BR>&nbsp; Key key;</P>
<P>&nbsp; /**<BR>&nbsp;&nbsp; * 根据参数生成KEY<BR>&nbsp;&nbsp; * @param strKey<BR>&nbsp;&nbsp; */<BR>&nbsp;&nbsp; public void getKey(String strKey) {<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; KeyGenerator _generator = KeyGenerator.getInstance("DES");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _generator.init(new SecureRandom(strKey.getBytes()));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.key = _generator.generateKey();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _generator = null;<BR>&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</P>
<P>&nbsp; /**<BR>&nbsp;&nbsp; * 加密String明文输入,String密文输出<BR>&nbsp;&nbsp; * @param strMing<BR>&nbsp;&nbsp; * @return<BR>&nbsp;&nbsp; */<BR>&nbsp; public String getEncString(String strMing) {<BR>&nbsp;&nbsp;&nbsp; byte[] byteMi = null;<BR>&nbsp;&nbsp;&nbsp; byte[] byteMing = null;<BR>&nbsp;&nbsp;&nbsp; String strMi = "";<BR>&nbsp;&nbsp;&nbsp; BASE64Encoder base64en = new BASE64Encoder();<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteMing = strMing.getBytes("UTF8");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteMi = this.getEncCode(byteMing);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strMi = base64en.encode(byteMi);<BR>&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; } finally {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; base64en = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteMing = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteMi = null;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; return strMi;<BR>&nbsp; }</P>
<P>&nbsp; /**<BR>&nbsp;&nbsp; * 解密 以String密文输入,String明文输出<BR>&nbsp;&nbsp; * @param strMi<BR>&nbsp;&nbsp; * @return<BR>&nbsp;&nbsp; */<BR>&nbsp; public String getDesString(String strMi) {<BR>&nbsp;&nbsp;&nbsp; BASE64Decoder base64De = new BASE64Decoder();<BR>&nbsp;&nbsp;&nbsp; byte[] byteMing = null;<BR>&nbsp;&nbsp;&nbsp; byte[] byteMi = null;<BR>&nbsp;&nbsp;&nbsp; String strMing = "";<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteMi = base64De.decodeBuffer(strMi);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteMing = this.getDesCode(byteMi);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strMing = new String(byteMing, "UTF8");<BR>&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; } finally {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; base64De = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteMing = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteMi = null;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; return strMing;<BR>&nbsp; }</P>
<P>&nbsp; /**<BR>&nbsp;&nbsp; * 加密以byte[]明文输入,byte[]密文输出<BR>&nbsp;&nbsp; * @param byteS<BR>&nbsp;&nbsp; * @return<BR>&nbsp;&nbsp; */<BR>&nbsp; private byte[] getEncCode(byte[] byteS) {<BR>&nbsp;&nbsp;&nbsp; byte[] byteFina = null;<BR>&nbsp;&nbsp;&nbsp; Cipher cipher;<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cipher = Cipher.getInstance("DES");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cipher.init(Cipher.ENCRYPT_MODE, key);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteFina = cipher.doFinal(byteS);<BR>&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; } finally {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cipher = null;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; return byteFina;<BR>&nbsp; }</P>
<P>&nbsp; /**<BR>&nbsp;&nbsp; * 解密以byte[]密文输入,以byte[]明文输出<BR>&nbsp;&nbsp; * @param byteD<BR>&nbsp;&nbsp; * @return<BR>&nbsp;&nbsp; */<BR>&nbsp; private byte[] getDesCode(byte[] byteD) {<BR>&nbsp;&nbsp;&nbsp; Cipher cipher;<BR>&nbsp;&nbsp;&nbsp; byte[] byteFina = null;<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cipher = Cipher.getInstance("DES");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cipher.init(Cipher.DECRYPT_MODE, key);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteFina = cipher.doFinal(byteD);<BR>&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp; } finally {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cipher = null;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; return byteFina;<BR>&nbsp; }</P>
<P>&nbsp; public static void main(String args[]) {<BR>&nbsp;&nbsp;&nbsp; DesEncrypt des=new DesEncrypt();//实例化一个对像<BR>&nbsp;&nbsp;&nbsp; des.getKey("aadd");//生成密匙</P>
<P>&nbsp;&nbsp;&nbsp; String strEnc = des.getEncString("明文");//加密字符串,返回String的密文<BR>&nbsp;&nbsp;&nbsp; System.out.println("加密文:"+strEnc);</P>
<P>&nbsp;&nbsp;&nbsp; String strDes = des.getDesString(strEnc);//把String 类型的密文解密<BR>&nbsp;&nbsp;&nbsp; System.out.println("解密文:"+strDes);<BR>&nbsp; }<BR>}<BR></P><img src ="http://www.blogjava.net/purecoffee/aggbug/21677.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/purecoffee/" target="_blank">清咖</a> 2005-11-28 15:08 <a href="http://www.blogjava.net/purecoffee/archive/2005/11/28/21677.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Sending Email with Spring mail abstraction layer</title><link>http://www.blogjava.net/purecoffee/archive/2005/11/24/21237.html</link><dc:creator>清咖</dc:creator><author>清咖</author><pubDate>Thu, 24 Nov 2005 02:37:00 GMT</pubDate><guid>http://www.blogjava.net/purecoffee/archive/2005/11/24/21237.html</guid><wfw:comment>http://www.blogjava.net/purecoffee/comments/21237.html</wfw:comment><comments>http://www.blogjava.net/purecoffee/archive/2005/11/24/21237.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/purecoffee/comments/commentRss/21237.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/purecoffee/services/trackbacks/21237.html</trackback:ping><description><![CDATA[<TABLE width="100%" summary="Navigation header">
<TBODY>
<TR>
<TH align=middle colSpan=3>Chapter&nbsp;21.&nbsp;Sending Email with Spring mail abstraction layer</TH></TR>
<TR>
<TD align=left width="20%"><A accessKey=p href="http://www.springframework.org/docs/reference/cci.html">Prev</A>&nbsp;</TD>
<TH align=middle width="60%">&nbsp;</TH>
<TD align=right width="20%">&nbsp;<A accessKey=n href="http://www.springframework.org/docs/reference/scheduling.html">Next</A></TD></TR></TBODY></TABLE>
<HR>

<DIV class=chapter lang=en>
<DIV class=titlepage>
<DIV>
<DIV>
<H2 class=title><A name=mail></A>Chapter&nbsp;21.&nbsp;Sending Email with Spring mail abstraction layer</H2></DIV></DIV>
<DIV></DIV></DIV>
<DIV class=sect1 lang=en>
<DIV class=titlepage>
<DIV>
<DIV>
<H2 class=title style="CLEAR: both"><A name=mail-introduction></A>21.1.&nbsp;Introduction</H2></DIV></DIV>
<DIV></DIV></DIV>
<P>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.</P></DIV>
<DIV class=sect1 lang=en>
<DIV class=titlepage>
<DIV>
<DIV>
<H2 class=title style="CLEAR: both"><A name=mail-structure></A>21.2.&nbsp;Spring mail abstraction structure</H2></DIV></DIV>
<DIV></DIV></DIV>
<P>The main package of Spring mail abstraction layer is <TT class=literal>org.springframework.mail</TT> package. It contains central interface for sending emails called <TT class=literal>MailSender</TT> and the <SPAN class=emphasis><EM>value object</EM></SPAN> which encapsulates properties of a simple mail such as <SPAN class=emphasis><EM>from</EM></SPAN>, <SPAN class=emphasis><EM>to</EM></SPAN>, <SPAN class=emphasis><EM>cc</EM></SPAN>, <SPAN class=emphasis><EM>subject</EM></SPAN>, <SPAN class=emphasis><EM>text</EM></SPAN> called <TT class=literal>SimpleMailMessage</TT>. 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 <TT class=literal>MailException.</TT>Please refer to JavaDocs for more information on mail exception hierarchy.</P>
<P>Spring also provides a sub-interface of <TT class=literal>MailSender</TT> for specialized <SPAN class=emphasis><EM>JavaMail</EM></SPAN> features such as MIME messages, namely <TT class=literal>org.springframework.mail.javamail.JavaMailSender</TT> It also provides a callback interface for preparation of JavaMail MIME messages, namely <TT class=literal>org.springframework.mail.javamail.MimeMessagePreparator</TT></P>
<P>MailSender: </P><PRE class=programlisting>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;

}</PRE>
<P>JavaMailSender: </P><PRE class=programlisting>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;

}</PRE>
<P>MimeMessagePreparator: </P><PRE class=programlisting>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;

}</PRE></DIV>
<DIV class=sect1 lang=en>
<DIV class=titlepage>
<DIV>
<DIV>
<H2 class=title style="CLEAR: both"><A name=mail-usage></A>21.3.&nbsp;Using Spring mail abstraction</H2></DIV></DIV>
<DIV></DIV></DIV>
<P>Let's assume there is a business interface called <TT class=literal>OrderManager</TT></P><PRE class=programlisting>public interface OrderManager {

    void placeOrder(Order order);
}</PRE>
<P>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 <TT class=literal>MailSender</TT> and <TT class=literal>SimpleMailMessage</TT></P>
<P><SPAN class=emphasis><EM>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.</EM></SPAN></P>
<P>Here is the implementation of <TT class=literal>OrderManager</TT> </P><PRE class=programlisting>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());            
        }
    }
}</PRE>
<P>Here is what the bean definitions for the code above would look like:</P><PRE class=programlisting>&lt;bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt;
  &lt;property name="host"&gt;&lt;value&gt;mail.mycompany.com&lt;/value&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"&gt;
  &lt;property name="from"&gt;&lt;value&gt;customerservice@mycompany.com&lt;/value&gt;&lt;/property&gt;
  &lt;property name="subject"&gt;&lt;value&gt;Your order&lt;/value&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;bean id="orderManager" class="com.mycompany.businessapp.support.OrderManagerImpl"&gt;
  &lt;property name="mailSender"&gt;&lt;ref bean="mailSender"/&gt;&lt;/property&gt;
  &lt;property name="message"&gt;&lt;ref bean="mailMessage"/&gt;&lt;/property&gt;
&lt;/bean&gt;</PRE>
<P>Here is the implementation of <TT class=literal>OrderManager</TT> using <TT class=literal>MimeMessagePreparator</TT> callback interface. Please note that the mailSender property is of type <TT class=literal>JavaMailSender</TT> in this case in order to be able to use JavaMail MimeMessage: </P><PRE class=programlisting>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());            
        }
    }
}</PRE>
<P>If you want to use JavaMail MimeMessage to the full power, the <TT class=literal>MimeMessagePreparator</TT> is available at your fingertips.</P>
<P><SPAN class=emphasis><EM>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.</EM></SPAN></P>
<DIV class=sect2 lang=en>
<DIV class=titlepage>
<DIV>
<DIV>
<H3 class=title><A name=d0e15382></A>21.3.1.&nbsp;Pluggable MailSender implementations</H3></DIV></DIV>
<DIV></DIV></DIV>
<P>Spring comes with two MailSender implementations out of the box - the JavaMail implementation and the implementation on top of Jason Hunter's <SPAN class=emphasis><EM>MailMessage</EM></SPAN> class that's included in <A href="http://servlets.com/cos" target=_top>http://servlets.com/cos</A> (com.oreilly.servlet). Please refer to JavaDocs for more information.</P></DIV></DIV>
<DIV class=sect1 lang=en>
<DIV class=titlepage>
<DIV>
<DIV>
<H2 class=title style="CLEAR: both"><A name=d0e15393></A>21.4.&nbsp;Using the JavaMail MimeMessageHelper</H2></DIV></DIV>
<DIV></DIV></DIV>
<P>One of the components that comes in pretty handy when dealing with JavaMail messages is the <TT class=literal>org.springframework.mail.javamail.MimeMessageHelper</TT>. It prevents you from having to use the nasty APIs the the <TT class=literal>javax.mail.internet</TT> classes. A couple of possible scenarios: </P>
<DIV class=sect2 lang=en>
<DIV class=titlepage>
<DIV>
<DIV>
<H3 class=title><A name=d0e15404></A>21.4.1.&nbsp;Creating a simple MimeMessage and sending it</H3></DIV></DIV>
<DIV></DIV></DIV>
<P>Using the MimeMessageHelper it's pretty easy to setup and send a MimeMessage: </P><PRE class=programlisting>// 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);
			</PRE>
<P></P></DIV>
<DIV class=sect2 lang=en>
<DIV class=titlepage>
<DIV>
<DIV>
<H3 class=title><A name=d0e15412></A>21.4.2.&nbsp;Sending attachments and inline resources</H3></DIV></DIV>
<DIV></DIV></DIV>
<P>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. </P><PRE class=programlisting>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(
  "&lt;html&gt;&lt;body&gt;&lt;img src='cid:identifier1234'&gt;&lt;/body&gt;&lt;/html&gt;"
  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);
			</PRE>
<P><SPAN class=emphasis><EM>Inline resources are added to the mime message using the Content-ID specified as you've seen just now (<TT class=literal>identifier1234</TT> 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!</EM></SPAN> </P></DIV></DIV></DIV><img src ="http://www.blogjava.net/purecoffee/aggbug/21237.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/purecoffee/" target="_blank">清咖</a> 2005-11-24 10:37 <a href="http://www.blogjava.net/purecoffee/archive/2005/11/24/21237.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>这个页面怎么老师刷啊刷的。。。。。。。</title><link>http://www.blogjava.net/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://www.blogjava.net/purecoffee/archive/2005/11/18/20375.html</guid><wfw:comment>http://www.blogjava.net/purecoffee/comments/20375.html</wfw:comment><comments>http://www.blogjava.net/purecoffee/archive/2005/11/18/20375.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/purecoffee/comments/commentRss/20375.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/purecoffee/services/trackbacks/20375.html</trackback:ping><description><![CDATA[这个页面怎么老师刷啊刷的。。。。。。。可怜一下偶的眼睛<img src ="http://www.blogjava.net/purecoffee/aggbug/20375.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/purecoffee/" target="_blank">清咖</a> 2005-11-18 10:22 <a href="http://www.blogjava.net/purecoffee/archive/2005/11/18/20375.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring,FreeMarker</title><link>http://www.blogjava.net/purecoffee/archive/2005/11/18/20373.html</link><dc:creator>清咖</dc:creator><author>清咖</author><pubDate>Fri, 18 Nov 2005 02:21:00 GMT</pubDate><guid>http://www.blogjava.net/purecoffee/archive/2005/11/18/20373.html</guid><wfw:comment>http://www.blogjava.net/purecoffee/comments/20373.html</wfw:comment><comments>http://www.blogjava.net/purecoffee/archive/2005/11/18/20373.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/purecoffee/comments/commentRss/20373.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/purecoffee/services/trackbacks/20373.html</trackback:ping><description><![CDATA[<P>1。spring日期类型要写自己的方法覆盖原有的绑定类型<BR>例如生日域的绑定<BR>&nbsp;protected void initBinder(HttpServletRequest arg0, ServletRequestDataBinder arg1) throws Exception {<BR>&nbsp;&nbsp;arg1.registerCustomEditor(Date.class,"birthday",getCustomDateEditor());<BR>&nbsp;&nbsp;super.initBinder(arg0, arg1);<BR>&nbsp;}<BR><BR>2.trim<BR>The string without leading and trailing white-space. Example: (${"&nbsp; green mouse&nbsp; "?trim})&nbsp;&nbsp; <BR>The output: (green mouse)&nbsp;&nbsp; <BR>&nbsp; 3.split<BR>&nbsp;&nbsp; <BR>&nbsp;&lt;#list "someMOOtestMOOtext"?split("MOO") as x&gt;<BR>- ${x}<BR>&lt;/#list&gt;&nbsp;&nbsp;&nbsp; <BR>willprint :<BR>- some<BR>- test<BR>- text <BR>&nbsp;</P><img src ="http://www.blogjava.net/purecoffee/aggbug/20373.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/purecoffee/" target="_blank">清咖</a> 2005-11-18 10:21 <a href="http://www.blogjava.net/purecoffee/archive/2005/11/18/20373.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>梦想</title><link>http://www.blogjava.net/purecoffee/archive/2005/11/15/19828.html</link><dc:creator>清咖</dc:creator><author>清咖</author><pubDate>Tue, 15 Nov 2005 04:12:00 GMT</pubDate><guid>http://www.blogjava.net/purecoffee/archive/2005/11/15/19828.html</guid><wfw:comment>http://www.blogjava.net/purecoffee/comments/19828.html</wfw:comment><comments>http://www.blogjava.net/purecoffee/archive/2005/11/15/19828.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/purecoffee/comments/commentRss/19828.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/purecoffee/services/trackbacks/19828.html</trackback:ping><description><![CDATA[人因梦想而伟大，因精神而不休。<BR><BR>注，这页面怎么老自动刷新，，，，<img src ="http://www.blogjava.net/purecoffee/aggbug/19828.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/purecoffee/" target="_blank">清咖</a> 2005-11-15 12:12 <a href="http://www.blogjava.net/purecoffee/archive/2005/11/15/19828.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>这个页面不错</title><link>http://www.blogjava.net/purecoffee/archive/2005/11/13/19596.html</link><dc:creator>清咖</dc:creator><author>清咖</author><pubDate>Sun, 13 Nov 2005 07:25:00 GMT</pubDate><guid>http://www.blogjava.net/purecoffee/archive/2005/11/13/19596.html</guid><wfw:comment>http://www.blogjava.net/purecoffee/comments/19596.html</wfw:comment><comments>http://www.blogjava.net/purecoffee/archive/2005/11/13/19596.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/purecoffee/comments/commentRss/19596.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/purecoffee/services/trackbacks/19596.html</trackback:ping><description><![CDATA[不过把用户限制在使用java的用户，是不是会影响人气。<img src ="http://www.blogjava.net/purecoffee/aggbug/19596.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/purecoffee/" target="_blank">清咖</a> 2005-11-13 15:25 <a href="http://www.blogjava.net/purecoffee/archive/2005/11/13/19596.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>这里很快</title><link>http://www.blogjava.net/purecoffee/archive/2005/11/11/19296.html</link><dc:creator>清咖</dc:creator><author>清咖</author><pubDate>Fri, 11 Nov 2005 06:55:00 GMT</pubDate><guid>http://www.blogjava.net/purecoffee/archive/2005/11/11/19296.html</guid><wfw:comment>http://www.blogjava.net/purecoffee/comments/19296.html</wfw:comment><comments>http://www.blogjava.net/purecoffee/archive/2005/11/11/19296.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/purecoffee/comments/commentRss/19296.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/purecoffee/services/trackbacks/19296.html</trackback:ping><description><![CDATA[但是页面好难看<img src ="http://www.blogjava.net/purecoffee/aggbug/19296.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/purecoffee/" target="_blank">清咖</a> 2005-11-11 14:55 <a href="http://www.blogjava.net/purecoffee/archive/2005/11/11/19296.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>