I wrote a program to distribute some material via email. It has been working until recently. After uninstalling RJE 8.0 (I don't need it) and upgrading from JDK 15.0.1 to JDK 16.0.1, I tried to use it and it fails with a "SSLHandshakeException".
From what I can gather, I have a problem with a certificate. This has taken me out of my league.
I have included the code below for my email class. The libraries I have used are: activation-1.1.1.jar javax.mail.jar
Since this is for personal use use only, I don't want to have to purchase a certificate. Can you help me get around this issue?
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";
}
}