`

MimeMessageHelper的基本用法

    博客分类:
  • java
阅读更多

MimeMessageHelper的基本用法

    博客分类: Spring 发邮件

企业应用Spring邮件MimeMessageHelper

org.springframework.mail.javamail.MimeMessageHelper是处理JavaMail比较顺手的组件之一,可以让你摆脱繁复的JavaMail API

接口类:
Java代码  收藏代码

    package com.mail.service; 
     
    import javax.mail.MessagingException; 
     
    /**
     * Created with IntelliJ IDEA.
     * User: Administrator
     * Date: 12-6-12
     * Time: 下午3:37
     * To change this template use File | Settings | File Templates.
     */ 
    public interface OrderManager { 
     
        void placeOrder() throws MessagingException; 
    } 

实现类:
Java代码  收藏代码

    package com.mail.sender; 
     
    import com.mail.service.OrderManager; 
    import org.springframework.mail.MailException; 
    import org.springframework.mail.javamail.JavaMailSender; 
    import org.springframework.mail.javamail.MimeMessageHelper; 
    import org.springframework.mail.javamail.MimeMessagePreparator; 
     
    import javax.mail.Message; 
    import javax.mail.MessagingException; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeMessage; 
    import java.lang.management.MonitorInfo; 
     
    /** MimeMessageHelper的基本用法
     * Created by IntelliJ IDEA.
     * User: Administrator
     * Date: 12-6-13
     * Time: 上午11:19
     * To change this template use File | Settings | File Templates.
     */ 
    public class MimeHelper implements OrderManager { 
     
        private JavaMailSender javaMailSender; 
     
        public void setJavaMailSender(JavaMailSender javaMailSender) { 
            this.javaMailSender = javaMailSender; 
        } 
     
        public void placeOrder() { 
     
            MimeMessage message = javaMailSender.createMimeMessage(); 
            MimeMessageHelper helper = new MimeMessageHelper(message); 
     
            try { 
                helper.setTo("XXXXX@qq.com");//toXXX邮箱 
                helper.setFrom("XXX@qq.com"); //from XXX邮箱 
                helper.setSubject("helper"); 
                helper.setText("hello"); 
                javaMailSender.send(message); 
            } catch (MessagingException e) { 
                e.printStackTrace(); 
            } 
     
        } 
    } 

配置:

注入JavamailSender对象
Xml代码  收藏代码

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
            <property name="host" value="smtp.exmail.qq.com"/> 
            <property name="defaultEncoding" value="UTF-8"/> 
            <property name="username" value="XXXX@qq.com"/> 
            <property name="password" value="XXXX"/> 
            <property name="javaMailProperties"> 
                <value> 
                    mail.smtp.auth=true 
                </value> 
            </property> 
        </bean> 

注入MimeHelper对象
Xml代码  收藏代码

    <bean id="orderManager" class="com.mail.sender.MimeHelper"> 
            <property name="javaMailSender" ref="mailSender"/> 
        </bean> 



测试一下,如果没错就说明成功了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics