腾讯企业邮发送邮件 java版

背景:
由于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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shengjk1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值