javaMail

javaMail发送邮件

jar包:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.4</version>
</dependency>

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

工具类

package com.ufclub.util.mail;

import com.ufclub.DTO.mail.FoxmailPropertiesDTO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送邮件
 *
 * @author zhoushixia
 * @date 2017-08-10
 */
public class SendMailUtil {

    private static final Logger logger = LoggerFactory.getLogger(SendMailUtil.class);

    private static  boolean isInit = false;

    private static  Properties prop;

    public static  void setProperty(FoxmailPropertiesDTO foxmailPropertiesDTO){
        if(!isInit){
            logger.info("{0}邮箱初始化开始,发送者为{1},接受者为{2}",foxmailPropertiesDTO.getMailCode(),foxmailPropertiesDTO.getSenderAddress(),foxmailPropertiesDTO.getReceiveAddress());

            prop = new Properties();
            prop.setProperty("mail.host",foxmailPropertiesDTO.getMailHost());
            prop.setProperty("mail.transport.protocol", "smtp");
            prop.setProperty("mail.smtp.auth", "true");
            isInit = true;

            logger.info("{0}邮箱初始化成功,发送者为{1},接受者为{2}",foxmailPropertiesDTO.getMailCode(),foxmailPropertiesDTO.getSenderAddress(),foxmailPropertiesDTO.getReceiveAddress());

        }
    }

    public static void sendMail(FoxmailPropertiesDTO foxmailPropertiesDTO){
        if(StringUtils.isBlank(foxmailPropertiesDTO.getReceiveAddress())){
            logger.info("{0}发送邮件,邮件无接收者",foxmailPropertiesDTO.getMailCode());
            return;
        }
        try{
            setProperty(foxmailPropertiesDTO);
            //创建session
            Session session = Session.getInstance(prop);
            //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
            session.setDebug(true);
            //通过session得到transport对象
            Transport ts = session.getTransport();
            //使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
            ts.connect(foxmailPropertiesDTO.getMailHost(), foxmailPropertiesDTO.getSenderAddress(), foxmailPropertiesDTO.getSenderPassword());
            //创建邮件
            Message message = createSimpleMail(session,foxmailPropertiesDTO);
            //发送邮件
            ts.sendMessage(message, message.getAllRecipients());
            ts.close();
        }catch (Exception e) {
            logger.error("发送邮件异常");
            e.printStackTrace();
        }

    }

    public static MimeMessage createSimpleMail(Session session,FoxmailPropertiesDTO foxmailPropertiesDTO)
            throws Exception {
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //指明邮件的发件人
        message.setFrom(new InternetAddress(foxmailPropertiesDTO.getSenderAddress()));
        InternetAddress[] internetAddressTo = new InternetAddress().parse(foxmailPropertiesDTO.getReceiveAddress());
        message.addRecipients(Message.RecipientType.TO, internetAddressTo);
        //邮件的文本内容
        message.setContent(foxmailPropertiesDTO.getSendContent(), "text/html;charset=UTF-8");
        //返回创建好的邮件对象
        return message;
    }

}
 
数据库截图
调用截图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值