首页 > 解决方案 > 电子邮件程序失败

问题描述

我编写了一个程序来通过电子邮件分发一些材料。它一直工作到最近。卸载 RJE 8.0(我不需要它)并从 JDK 15.0.1 升级到 JDK 16.0.1 后,我尝试使用它,但它失败并出现“SSLHandshakeException”。

据我所知,我的证书有问题。这让我脱离了我的联盟。

我已经为我的电子邮件课程包含了下面的代码。我使用的库是:activation-1.1.1.jar javax.mail.jar

由于这仅供个人使用,我不想购买证书。你能帮我解决这个问题吗?

package PkgMailerPkg;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

//----------------------------------------------------------------------------------------
// To enable GMail to accept email from a less secure app . . .
//    Go to www.gmail.com
//    Select LSWeb0247 account
//    Left click on image
//    Select "Manage your Google Account" (centered Under email address)
//    Click on "Security" (on left side of screen)
//    Scroll down to "Less secure app access"
//    Ensure it is set to "ON"3
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
// Send email class.
//----------------------------------------------------------------------------------------
public class MailManager
{
   private final static String HOST = "smtp.gmail.com";
   private final String user;
   private final String password;
   private Properties properties;
   private final Session session;

   //-------------------------------------------------------------------------------------
   // Constructor:
   //
   //   user = sender email address
   //   password = sender account password
   //-------------------------------------------------------------------------------------
   public MailManager(String user, String password)
   {
      this.user = user;
      this.password = password;

      //----------------------------------------------------------------------------------
      // Get system properties
      //----------------------------------------------------------------------------------
      properties = System.getProperties();

      //----------------------------------------------------------------------------------
      // Setup mail server
      //----------------------------------------------------------------------------------
      properties.put("mail.smtp.host", HOST);
      properties.put("mail.smtp.port", "465");
      properties.put("mail.smtp.ssl.enable", "true");
      properties.put("mail.smtp.auth", "true");

      //----------------------------------------------------------------------------------
      // Get the Session object using GMail account
      //----------------------------------------------------------------------------------
      session = Session.getInstance(properties, new javax.mail.Authenticator()
      {
         @Override
         protected PasswordAuthentication getPasswordAuthentication()
         {
            return new PasswordAuthentication(user, password);
         }
      });
   }

   //-------------------------------------------------------------------------------------
   // Parameters:
   //    subject: EMail subject line
   //    to:      ArrayList of primary recipients
   //    cc:      ArrayList of CC recipients
   //    bcc:     ArrayList of BCC recipients
   //    from:    Sender address
   //    body:    Email body text (HTML allowed)
   //    attachments: ArrayList of files to attach
   //
   // Returns:
   //    "OK" - No problem
   //    Error - Something happened
   //-------------------------------------------------------------------------------------
   public String sendMail(
           String subject,
           ArrayList<String> to,
           ArrayList<String> cc,
           ArrayList<String> bcc,
           String from,
           String body,
           ArrayList<String> attachments
   )
   {
      try
      {
         //-------------------------------------------------------------------------------
         // Create a default MimeMessage object.
         //-------------------------------------------------------------------------------
         MimeMessage message = new MimeMessage(session);

         //-------------------------------------------------------------------------------
         // Set From: header field of the header.
         //-------------------------------------------------------------------------------
         message.setFrom(new InternetAddress(from));

         //-------------------------------------------------------------------------------
         // Set "TO" recipients
         //-------------------------------------------------------------------------------
         for (String sendTo : to)
         {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(sendTo));
         }

         //-------------------------------------------------------------------------------
         // Set "CC" recipients
         //-------------------------------------------------------------------------------
         if (cc != null)
         {
            for (String ccTo : cc)
            {
               message.addRecipient(Message.RecipientType.CC, new InternetAddress(ccTo));
            }
         }

         //-------------------------------------------------------------------------------
         // Set "BCC" recipients
         //-------------------------------------------------------------------------------
         if (bcc != null)
         {
            for (String bccTo : bcc)
            {
               message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bccTo));
            }
         }

         //-------------------------------------------------------------------------------
         // Set "SUBJECT"
         //-------------------------------------------------------------------------------
         message.setSubject(subject);

         //-------------------------------------------------------------------------------
         // Add attachments
         //-------------------------------------------------------------------------------
         Multipart multipart = new MimeMultipart();

         for (String attachment : attachments)
         {
            MimeBodyPart attachmentPart = new MimeBodyPart();
            File f = new File(attachment);
            attachmentPart.attachFile(f);
            multipart.addBodyPart(attachmentPart);
         }

         //-------------------------------------------------------------------------------
         // Add attachments
         //-------------------------------------------------------------------------------
         MimeBodyPart messageBodyPart = new MimeBodyPart();
         messageBodyPart.setContent(body, "text/html");
         multipart.addBodyPart(messageBodyPart);

         message.setContent(multipart);

         //-------------------------------------------------------------------------------
         // Send message
         //-------------------------------------------------------------------------------
         Transport.send(message);
      }
      catch (MessagingException | IOException ex)
      {
         return "MailManager Error: " + ex.getMessage();
      }

      return "OK";
   }
}

标签: javaemailcertificate

解决方案


推荐阅读