java后端发送邮件信息

package com.cn.service.util;

import com.alibaba.fastjson.JSON;
import com.smarthome.service.dto.sendEmail.EmailAuthenticator;
import com.smarthome.service.dto.sendEmail.EmailSendInfo;
import org.apache.log4j.Logger;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * Created by abbott on 2018/9/19.
 */
public class EmailSender {

    private static final Logger LOGGER = Logger.getLogger(EmailSender.class);

    public static boolean sendTextMail(EmailSendInfo mailInfo){
        /**发送状态*/
        boolean sendStatus = false;
        /**判断是否需要身份认证**/
        EmailAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            /**如果需要身份认证,则创建一个密码验证器*/
            authenticator = new EmailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        /**根据邮件会话属性和密码验证器构造一个发送邮件的session**/
        Session sendMailSession = Session.getInstance(pro, authenticator);
        /**【调试时使用】开启Session的debug模式**/
        sendMailSession.setDebug(true);
        try {
            /**根据session创建一个邮件消息**/
            MimeMessage mailMessage = new MimeMessage(sendMailSession);
            /**创建邮件发送者地址**/
            Address from = new InternetAddress(mailInfo.getFromAddress());
            /**设置邮件消息的发送者**/
            mailMessage.setFrom(from);
            /**创建邮件的接收者地址,并设置到邮件消息中**/
            Address to = new InternetAddress(mailInfo.getToAddress());
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            /**设置邮件消息的主题**/
            mailMessage.setSubject(mailInfo.getSubject(), "UTF-8");
            /**设置邮件消息发送的时间**/
            mailMessage.setSentDate(new Date());
            /**设置邮件消息的主要内容**/
            String mailContent = mailInfo.getContent();
            mailMessage.setText(mailContent, "UTF-8");
            LOGGER.info("---------------------");
            Transport.send(mailMessage);
            LOGGER.info("+++++++++++++++++++++");
            sendStatus = true;
        } catch (MessagingException ex) {
            LOGGER.info("发送邮件出现异常");
            ex.printStackTrace();
            return sendStatus;
        }
        return sendStatus;
    }


    public static void main(String[] args) {
        String fromaddr = "QQ邮箱";//发送邮箱
        String toaddr = "邮箱";//要发送的邮箱
        String title = "【申请人信息】";
        String content = "【测试内容】Hello, this is sample for to check send email using JavaMailAPI ";
        String port = "587";
        String host = "smtp.qq.com";
        String userName = "QQ邮箱"; //发送邮箱
        String password = "指的是授权码"; //发送邮件的授权码

        EmailSendInfo mailInfo = new EmailSendInfo();
        mailInfo.setMailServerHost(host);
        mailInfo.setMailServerPort(port);
        mailInfo.setValidate(true);
        mailInfo.setUserName(userName);
        mailInfo.setPassword(password);
        mailInfo.setFromAddress(fromaddr);
        mailInfo.setToAddress(toaddr);
        mailInfo.setSubject(title);
        mailInfo.setContent(content);

        /**发送文体格式邮件**/
       new EmailSender().sendTextMail(mailInfo);
    }
}

QQ邮箱 POP3 和 SMTP 服务器地址设置如下:

邮箱POP3服务器(端口995)SMTP服务器(端口465或587)
qq.compop.qq.comsmtp.qq.com

SMTP服务器需要身份验证。

发送qq邮箱 异常:

   535 Error: ��ʹ����Ȩ���¼�������뿴: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
DEBUG SMTP: AUTH LOGIN failed

原因:是因为还没有去邮箱那里设置开启POP3/SMTP服务

解决方法:

邮箱—>设置—>账户,然后拉下来,找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务

开启服务:POP3/SMTP服务

开启好了POP3/SMTP服务之后,会有一个授权码,这个授权码就是上面的 password

 

阿里云邮箱服务器:

服务器名称地址非SSL协议端口SSL协议端口号
POP pop3.mxhichina.com110995
IMAP imap.mxhichina.com143993
SMTP smtp.mxhichina.com25465

网易163邮箱服务:

服务器名称地址非SSL协议端口SSL协议端口号
POP pop3.163.com110995
IMAP imap.163.com143993
SMTP smtp163.com25465/994
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值