首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >腾讯企业邮发送邮件 java版

腾讯企业邮发送邮件 java版

作者头像
shengjk1
发布2018-10-24 15:27:35
1.6K0
发布2018-10-24 15:27:35
举报
文章被收录于专栏:码字搬砖码字搬砖

背景: 由于spark streaming程序需要保证 1*24 小时,不间断运行的,为了第一时间知道程序是否出错,所以采用了出错发送邮件这一策略,而公司恰恰又使用了腾讯企业邮,网上也有一些关于腾讯企业邮的例子,但不那么尽人意,s所以特地在此整理一下!

各位看官,让我们一起畅游在代码的海洋里吧!嘎嘎嘎。。。

package test.util;

import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.log4j.Logger;

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

 /**
 * Created by shengjk1 on 2016/11/4.
 * Blog Address:http://blog.csdn.net/jsjsjs1789
 */
public class SendEmailUtils {
    private static Logger logger = Logger.getLogger("SendEmailUtils.class");

    //用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication
    static class MyAuthenricator extends Authenticator{
        String u = null;
        String p = null;
        public MyAuthenricator(String u,String p){
            this.u=u;
            this.p=p;
        }
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(u,p);
        }
    }

    public static  void send(String title,String context){
        Properties prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", "smtps");
        //服务器
        prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
        //端口
        prop.setProperty("mail.smtp.port", "25");
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        //使用SSL,企业邮箱必需!
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
//        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        //发件人,进行权限认证
        Session session = Session.getDefaultInstance(prop, new MyAuthenricator("shengjk1@qq.cn", "shengjk1123"));
//        session.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(session);
        try {
        //发件人地址
            mimeMessage.setFrom(new InternetAddress("shengjk1@qq.cn",""));
            //收件人的地址
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("shengjk1@qq.cn"));
            mimeMessage.setSubject(title);
            mimeMessage.setSentDate(new Date());
            mimeMessage.setText(context);
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
        } catch (Exception e) {
            logger.error("scan 邮件异常 " +e);

        }
    }
    public static void main(String[] args) {
        SendEmailUtils.send("scanError","hahahahhaha");


}

pom.xml

<dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>mailapi</artifactId>
      <version>1.5.5</version>
    </dependency>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年11月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯企业邮
腾讯企业邮(Tencent Exmail)是腾讯基于多年海量用户邮件系统研发和运营经验,为企业量身订造的一套办公用邮箱系统。稳定、简洁和快速,整合了丰富的企业应用,涵盖资源共享、消息发布、组织管理等方面。腾讯企业邮和微信生态深度整合,为您提供便捷高效的移动邮件解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档