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
目录 %JAVA_HOME%\jre\lib\security里的local_policy.jar,US_export_policy.jar

it works!

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2:用sbt打过jar包放入集群里跑,出现错误
Unsupported major.minor version 52.0 
上网查出是jdk1.8的原因。(没有使用最开始的那段代码的时候,是不报这个错误的)
于是在windows10安装jdk1.7,并改了环境变量。并更新了idea里的jdk版本。
但是打包后放入集群中运行依然报这个错误。
于是猜想还是使用了jdk1.8,便把jdk1.8和jre1.8都删除了(仅仅是文件夹的删除,当时是绝对可能打包的时候还是用的1.8)
再打包运行,依然报这个错误。
在cmd下运行java -version,显示系统找不到文件 C:\ProgramData\Oracle\Java\javapath\java.exe。
于是将jdk1.7中的三个exe文件的快捷方式替换C:\ProgramData\Oracle\Java\javapath\java.exe目录下的三个对应文件。
在cmd下,不显示刚才的错误,显示了另一项错误,于是将jdk1.8 jdk1.7均卸载,重新安装了jdk1.7,之后cmd下显示了java version是1.7。
于是用sbt打包,说是找不到什么java。

重启后,将idea里的有关jdk版本的都确认为jdk7。
打包后运行不报之前的错误了。
报找不到org/jsoup/jsoup类的错误。
将windows下ivy2下的对应的jar包粘贴到集群下(运行脚本同级目录下)后,就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值