admin管理员组文章数量:1559746
背景:
由于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/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");
//端口
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", "shengjk1123"));
// session.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(session);
try {
//发件人地址
mimeMessage.setFrom(new InternetAddress("shengjk1@qq",""));
//收件人的地址
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("shengjk1@qq"));
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>
版权声明:本文标题:腾讯企业邮发送邮件java版 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727422880a1113778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论