码迷,mamicode.com
首页 > 其他好文 > 详细

邮件服务实现

时间:2015-02-13 14:49:35      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:java   spring   邮箱   邮件   乱码   

pom.xml

 <!-- 邮件发送 -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.1</version>
        </dependency>

EmailService

package net.deniro.email;

/**
 * 邮件服务
 *
 * @author deniro
 *         15-2-5下午3:11
 */
public interface EmailService {
    /**
     * 发送html格式的邮件
     *
     * @param to       接收地址
     * @param subject  邮件主题
     * @param htmlText 邮件内容
     * @throws EmailException
     */
    void sendMail(String to, String subject, String htmlText) throws EmailException;
}

EmailServiceImpl

package net.deniro.email;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

import static javax.mail.Message.RecipientType.TO;

/**
 * @author deniro
 *         15-2-5下午3:16
 */
public class EmailServiceImpl implements EmailService {

    private JavaMailSender javaMailSender;
    /**
     * 系统邮箱
     */
    private String systemEmail;

    public static void main(String[] args) {
        try {
            String host = "smtp.sina.com";
            final String from = "lisq047@sina.com";
            String to = "lisq047@163.com";

            Properties props = System.getProperties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");

            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, "deniro");
                }
            };

            Session session = Session.getDefaultInstance(props, authenticator);

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipients(TO, String.valueOf(new InternetAddress(to)));

            message.setSubject("测试");

            message.setText("测试内容");

            message.saveChanges();

            Transport.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    @Override
    public void sendMail(String to, String subject, String htmlText) throws EmailException {
        try {
            MimeMessage msg = javaMailSender.createMimeMessage();
            MimeMessageHelper msgHelper = new MimeMessageHelper(msg, "utf-8");//解决中文乱码

            msgHelper.setFrom(systemEmail);
            msgHelper.setTo(to);
            msgHelper.setSubject(subject);
            msgHelper.setText(htmlText, true);//html格式


            javaMailSender.send(msg);
        } catch (MessagingException e) {
            throw new EmailException("邮件发送失败。", e);
        }
    }

    public JavaMailSender getJavaMailSender() {
        return javaMailSender;
    }

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

    public String getSystemEmail() {
        return systemEmail;
    }

    public void setSystemEmail(String systemEmail) {
        this.systemEmail = systemEmail;
    }
}


email.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--载入属性文件-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="location" value="classpath:service.properties"></property>
    </bean>

    <!--邮件发送-->
    <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="protocol" value="${email.protocol}"/>
        <property name="host" value="${email.host}"/>
        <property name="port" value="${email.port}"/>
        <property name="username" value="${email.username}"/>
        <property name="password" value="${email.password}"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.${email.protocol}.auth">${email.auth}</prop>
            </props>
        </property>
    </bean>

    <!--邮件服务-->
    <bean id="emailService" class="net.deniro.email.EmailServiceImpl">
        <property name="javaMailSender" ref="javaMailSender"/>
        <property name="systemEmail" value="${email.systemEmail}"/>
    </bean>


</beans>


service.properties


email.protocol=smtp
email.host=smtp.sina.com
email.port=25
email.username=lisq047
email.password=******
email.auth=true
email.systemEmail=lisq047@sina.com

邮件发送结果:

技术分享

技术分享


调用前,记得先开启系统邮箱的SMTP服务:


技术分享


技术分享

邮件服务实现

标签:java   spring   邮箱   邮件   乱码   

原文地址:http://blog.csdn.net/lisq037/article/details/43794021

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!