Spring 发送普通邮件、夹带附件的邮件、嵌入网页或图片的邮件 使用方法

在项目应用中,时常存在发送邮件的业务需求。比如:用户注册时的邮件确认;系统出错时的邮件提醒;用户系统邮件订阅;系统的广告邮件定时推广等等。本文根据spring开发文档,列举普通文件邮件,夹带附件的邮件、嵌入图片的邮件三种形式的邮件的使用方式。
实现过程中定义BaseMailDefined类,用于描述邮件发送过程中需要的参数:发送者、接受者、邮件标题、邮件文本内容。
定义MailSenderFactory类,用于对普通文件邮件,夹带附件的邮件、嵌入图片的邮件三种形式的邮件的发送。其中JavaMailSender 采用spring注入的其实现类org.springframework.mail.javamail.JavaMailSenderImpl。
Spring 发送普通邮件、夹带附件的邮件、嵌入网页或图片的邮件 使用方法_第1张图片

public class MailSenderFactory {

    private JavaMailSender javaMailSender;

    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }


    /**
     * 简单邮件发送
     * @param bmd
     */
    public void senderSimpleMail(BaseMailDefined bmd){
        SimpleMailMessage msg = new SimpleMailMessage();
            msg.setFrom(bmd.getFrom());
            msg.setText(bmd.getText());
            msg.setTo(bmd.getTo());
            msg.setSubject(bmd.getSubject());
            javaMailSender.send(msg);
    }
    
    /**
     * 发送添加附件的邮件
     * @param bmd
     * @throws MessagingException
     */
    public void senderMimeMail(BaseMailDefined bmd) throws MessagingException{
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);
        helper.setTo(bmd.getTo());
        helper.setText(bmd.getText());
        helper.setFrom(bmd.getFrom());
        helper.setSubject(bmd.getSubject());
        helper.setSentDate(new Date());
        //夹带附件
         FileSystemResource file = new FileSystemResource(new File("E:/task/ip2.rar"));
        helper.addAttachment("ip2.rar", file);
        javaMailSender.send(msg);
    }
    
    /**
     * 发送内嵌图片或html的邮件
     * @param bmd
     * @throws MessagingException
     */
    public void senderHtmlMail(BaseMailDefined bmd) throws MessagingException{
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);
        helper.setTo(bmd.getTo());
        helper.setText(bmd.getText());
        helper.setFrom(bmd.getFrom());
        helper.setSubject(bmd.getSubject());
        helper.setSentDate(new Date());
        //内嵌式图片或html样式
        helper.setText("<html><body><img src='cid:identifier1235'></body></html>", true);
        FileSystemResource res = new FileSystemResource(new File("E:/task/ip2.png"));
        helper.addInline("identifier1235", res);
        javaMailSender.send(msg);
    }
}

public class BaseMailDefined {
         protected String to;  
         protected String from;  
         protected String subject;  
         protected String text;  
       
         public String getText() {
            return text;
        }

        public String getTo() {
            return to;
        }

        public String getFrom() {
            return from;
        }

        public String getSubject() {
            return subject;
        }

        public void setText(String text) {
            this.text = text;
        }

        public void setTo(String to) {  
             this.to = to;  
         }  
       
         public void setFrom(String from) {  
             this.from = from;  
         }  
       
         public void setSubject(String subject) {  
             this.subject = subject;  
         }  
       
}
测试方法:
public static void main(String[] args) throws Exception {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[] { "ApplicationContext.xml" });
        
        MailSenderFactory factory = (MailSenderFactory) ctx.getBean("mailFactory");
        BaseMailDefined bmd = (BaseMailDefined) ctx.getBean("baseMail");
        
        factory.senderSimpleMail(bmd);
        factory.senderMimeMail(bmd);
        factory.senderHtmlMail(bmd);
        
    }
XML配置:
<bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <value>smtp.sina.com</value>
        </property>
        <property name="username">
            <value>handouName</value>
        </property>
        <property name="password">
            <value>handouPassword</value>
        </property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
            </props>
        </property>
    </bean>

    <bean id="mailFactory" class="com.handou.mail.MailSenderFactory">
        <property name="javaMailSender">
            <ref bean="sender" />
        </property>
    </bean>

    <bean id="baseMail" class="com.handou.mail.BaseMailDefined">
        <property name="to">
            <value>[email protected]</value>
        </property>
        <property name="from">
            <value>[email protected]</value>
        </property>
        <property name="subject">
            <value>test</value>
        </property>
        <property name="text">
            <value>this is a test mail !!</value>
        </property>
    </bean>

你可能感兴趣的:(spring)