spark 邮件报错
在集群上跑代码,如果有问题,希望能发邮件到自己的邮箱。
想使用如下代码(环境是在win10下的intelliJ idea里,平时用sbt compile和sbt package,打成jar包后粘贴到集群spark里跑)
import java.lang.Exception; import java.lang.RuntimeException; import java.lang.String; import java.lang.System; import java.security.Security; import java.util.Calendar; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Part; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; //import com.hypers.commons.PropertyResources; public class SendMailSSL {Log log = LogFactory.getLog(getClass()); Properties props; String host; String port; String username; String password; final String DEFAULT_ENCODE = "UTF-8"; public SendMailSSL() {this.host = "";//PropertyResources.getProperty("smtp.server"); this.port = "";//PropertyResources.getProperty("smtp.port"); this.username = "";//PropertyResources.getProperty("smtp.username"); this.password = "";//PropertyResources.getProperty("smtp.password"); //String timeout = PropertyResources.getProperty("mail.smtp.timeout","10000"); //String conntimeout = PropertyResources.getProperty("mail.smtp.connectiontimeout", "10000"); props = System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", port); //props.put("mail.smtp.timeout", timeout); //props.put("mail.smtp.connectiontimeout", conntimeout); // STARTTLS // props.put("mail.smtp.starttls.enable", "true"); // SSL props.put("mail.smtp.socketFactory.port", port); props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); // props.put("javax.net.ssl.trustStore", truststore); Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); // props.put("mail.transport.protocol", "smtp"); }MimeMessage getMimeMessage(String to, String subject, String content, String[] attachments) throws Exception {// JavaMail representation of the message Session session = Session.getInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password); }}); MimeMessage msg = new MimeMessage(session); // Sender and recipient InternetAddress ia = new InternetAddress(); if (username.contains("@")) {ia.setAddress(username); ia.setPersonal(username.substring(0, username.indexOf("@"))); } else {ia.setAddress(username + "@" + host); ia.setPersonal(username); }msg.setFrom(ia); msg.setSender(ia); msg.setSentDate(Calendar.getInstance().getTime()); for (String recipient : to.split(",")) {msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); }// Subject msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B")); msg.setContentLanguage(new String[] { "en-us", "zh-cn" }); msg.setHeader("Content-Transfer-Encoding", "base64"); // Add a MIME part to the message MimeMultipart mmp = new MimeMultipart("mixed"); Document doc = Jsoup.parse(content); BodyPart plainPart = new MimeBodyPart(); plainPart.setContent(subject, "text/plain;charset=" + DEFAULT_ENCODE); mmp.addBodyPart(plainPart); BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(doc.html(), "text/html;charset=" + DEFAULT_ENCODE); mmp.addBodyPart(htmlPart); if (attachments != null) {for (String attachment : attachments) {BodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDisposition(Part.ATTACHMENT); FileDataSource fds = new FileDataSource(attachment); attachmentPart.setDataHandler(new DataHandler(fds)); attachmentPart.setFileName(MimeUtility.encodeWord(fds.getName())); mmp.addBodyPart(attachmentPart); }}msg.setContent(mmp); msg.saveChanges(); return msg; }MimeMessage getMimeMessage(String to, String subject, String content)throws Exception {log.info("email to " + to); return getMimeMessage(to, subject, content, null); }/*public void sendDefault(String subject, String content) { try { Transport.send(getMimeMessage( PropertyResources.getProperty("notify.list.test"), subject, content)); } catch (Exception e) { throw new RuntimeException(e); } }*/ public void send(String to, String subject, String content) {try {Transport.send(getMimeMessage(to, subject, content)); } catch (Exception e) {throw new RuntimeException(e); }}public void send(String to, String subject, String content, String... attachments) {try {if (StringUtils.isNotBlank(to)) {log.info(String.format("send %s to %s", subject, to)); Transport.send(getMimeMessage(to, subject, content, attachments)); }} catch (Exception e) {throw new RuntimeException(e); }}}
1:刚开始使用的时候装在的是jdk1.8,出现
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
解决方法是:
引用来自“刘正阳”的评论
你这个是jdk导致的,jdk里面有一个jce的包,安全性机制导致的访问https会报错,官网上有替代的jar包,换掉就好了
引用来自“嘉树”的评论
牛逼啊 包在这里 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
spark 邮件报错相关推荐
- 关于发邮件报错535 Error:authentication failed解决方法
关于发邮件报错535 Error:authentication failed解决方法 参考文章: (1)关于发邮件报错535 Error:authentication failed解决方法 (2)ht ...
- 记录一次服务器登录后提示邮件报错550
记录一次服务器登录后提示邮件报错550 `smtp-server: 550 邮箱:user not exist "/root/dead.letter" 11/417 . . . m ...
- python邮件发送csv附件_Python2.7 smtplib发送带附件邮件报错STARTTLS解决方法
最近在SIOMP系统中添加一项功能时,即自动发送带附件邮件到指定邮箱时,发现在发送时报错:错误信息:STARTTLS extension not supported by server.很是奇怪,但发 ...
- outlook客户端接收邮件报错0x80040600
今天一user QQ联系我说她的outlook无法接收邮件了,总是在报错,我让她截图给我,发现报告的是错误0x80040600,查看错误信息,我问其是不是非正常关闭了outlook客户端,她弱弱的说是 ...
- spark:报错com.esotericsoftware.kryo.KryoException: Buffer underflow.
场景 spark-sql跑一个较大的任务,数据落盘时报错: com.esotericsoftware.kryo.KryoException: Buffer underflow. Caused by: ...
- 利用jmail qq邮箱发邮件 报错 解决方法
利用java的jmail,登录qq邮箱发邮件时遇到下面几个问题,分享一下解决方法. 1. qq需要使用ssl 解决方法:使用有ssl安全认证的代码 <span style="font ...
- Python发邮件报错 554, b‘HL:ITC 0,163
利用Python脚本发邮件,发送失败 利用方法 set_debuglevel(1) 可以打印错误信息 运行脚本,错误信息显示: (554, b'HL:ITC 0,163 smtp9,DcCowAD3_ ...
- Spark 启动报错时报错 java.lang.NoSuchMethodError: scala.collection.immutable.HashSet$.empty()Lscala/collect
今天在Spark测试时异常 报错 一直 提示 找不到 该方法 因为 本身代码量很小 而且 没有用到 错误提示的类 找了许久 才发现是 版本 对应问题 /Library/Java/JavaVirtu ...
- cdo收取邮件_利用CDO发邮件 报错怎么处理
import win.ui; import com; //引用com库 /*DSG{{*/ mainForm = win.form(text="aardio工程11";right= ...
最新文章
- 使用Depth Texture
- 资料库保存树结构的三种方法
- python趣味编程入门 迈克 桑德斯_Python趣味编程入门
- leetcode-283-移动零
- 防止properties乱码配置---SpringBoot
- 用于用户C语言标识符,下列可用于C语言用户标识符的一组是( )
- Spring中AOP注解实现
- Virtual Serial Port Driver虚拟串口vspd v7.2 下载及破解方法
- 保研面试 算法题_面试挂在了一道 LRU 缓存算法设计题
- kubeflow kfserving 详解
- 【模板】左偏树(可并堆)
- soc设计方法与实现_冷轧钢板局部激光热处理实现汽车轻量化设计新方法
- (源码)群体智能优化算法之正余弦优化算法(Sine Cosine Algorithm,SCA)
- MySQL免安装版 图文教程【5.7版本,纯净版win7安装】
- ukey网络连接异常_Ukey的各种操作,你知道吗?
- java基于ssm+vue的高校会议预约系统 elementui
- **alon_MM DMA Interface for PCIe使用详解
- android棒棒糖,Android L正式定名Lollipop(棒棒糖)
- Uniapp中使用Echarts
- java实训题:随机点名器