spring发送邮件demo

package EMAIL;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

/**
 * Created by jianghuiwen on 17/3/5.
 */
public class SendEmail {

    static ApplicationContext actx = new ClassPathXmlApplicationContext(
            "email.xml");
    static MailSender sender = (MailSender) actx.getBean("mailSender");
    static SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage");

    public static void main(String[] args){
        for(int i =0;i<1;i++){
            mailMessage.setSubject("你好");
            mailMessage.setText("<html><head></head><body><h1>hello!!spring image html mail</h1>\"\n" +
                    "\t\t\t\t\t+\"<a href=http://www.baidu.com>baidu</a>\"\t+ \"<img src='https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png'/></body></html>");
            mailMessage.setTo("15255123122@163.com");
            sender.send(mailMessage);
        }

    }
}
package EMAIL;

import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

/**
 * Created by jianghuiwen on 17/3/5.
 */
public class SendHtmlMail {
    static ApplicationContext actx = new ClassPathXmlApplicationContext(
            "email.xml");
    static MailSender sender = (MailSender) actx.getBean("mailSender");
    static SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage");

    public static void main(String[] args) throws MessagingException, javax.mail.MessagingException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "email.xml");
        JavaMailSenderImpl sender = (JavaMailSenderImpl) ctx
                .getBean("mailSender");
        MimeMessage mailMessage = sender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true);
        messageHelper.setFrom("test@163.com.com");
        messageHelper.setTo("test111@163.com");

        messageHelper.setSubject("测试邮件中嵌套图片!!");
        // true 表示启动HTML格式的邮件
        messageHelper.setText(
                "<html><head></head><body><h1>hello!!spring image html mail</h1>"
                        +"<a href=http://www.baidu.com>baidu</a>"   + "<img src='https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_ca79a146.png'/></body></html>", true);

//        FileSystemResource img = new FileSystemResource(new File("单.png"));
//
//        messageHelper.addInline("image", img);//跟cid一致

        sender.send(mailMessage);
        System.out.println("邮件发送成功...");

    }
}

发送配置

mail.host=smtp.163.com
mail.username=test@163.com
mail.password=123456,
mail.from=test@163.com
mail.port=465

spring配置

<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:mail.properties</value>
            </list>
        </property>
    </bean>
    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
        <property name="from">
            <value>${mail.from}</value>
        </property>
        <!-- 查看SimpleMailMessage源码还可以注入标题,内容等 -->
    </bean>
    <!-- 申明JavaMailSenderImpl对象 -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="defaultEncoding" value="UTF-8" />
        <property name="host" value="${mail.host}" />
        <property name="username" value="${mail.username}" />
        <property name="port" value="${mail.port}"/>
        <property name="password" value="${mail.password}" />
        <property name="javaMailProperties">
            <props>
                <!-- 设置认证开关 -->
                <prop key="mail.smtp.auth">true</prop>
                <!-- 启动调试开关 -->
                <prop key="mail.debug">true</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            <!-- 设置发送延时 -->
                <prop key="mail.smtp.timeout">0</prop>
            </props>
        </property>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值