I've been working on this JAVA code that is supposed to send an email. Here's the code:
package sendemail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MAIN {
private String SMTP_HOST = "smtp.gmail.com";
private String FROM_ADDRESS = "sender@gmail.com";
private String PASSWORD = "pass";
private String FROM_NAME = "user";
public boolean sendMail(String[] recipients, String[] bccRecipients, String subject, String message) {
try {
Properties props = (Properties)System.getProperties().clone();
//Bypass the SSL authentication
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.starttls.required", true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.auth", true);
props.put("mail.debug", "false");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, new SocialAuth());
Message msg = new MimeMessage(session);
InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME);
msg.setFrom(from);
InternetAddress[] toAddresses = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
toAddresses[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, toAddresses);
InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length];
for (int j = 0; j < bccRecipients.length; j++) {
bccAddresses[j] = new InternetAddress(bccRecipients[j]);
}
msg.setRecipients(Message.RecipientType.BCC, bccAddresses);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
return true;
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(MAIN.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (MessagingException ex) {
Logger.getLogger(MAIN.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
class SocialAuth extends Authenticator {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM_ADDRESS, PASSWORD);
}
}
}
package sendemail;
import sendemail.MAIN;
public class BIGMAIN {
public static void main(String[] args) {
String[] recipients = new String[]{"rec@gmail.com"};
String[] bccRecipients = new String[]{"rec@gmail.com"};
String subject = "Hi this is test Mail";
String messageBody = "Test Mail from codesstore.blogspot.com";
new MAIN().sendMail(recipients, bccRecipients, subject, messageBody);
}
}
But whenever I run it I get this error:
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. h7sm10262673wmf.9 - gsmtp
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at sendemail.send.main(send.java:39)
I've searched for this error and found out that adding the following line to the code should solve the problem, but the problem is still here and sometimes I get a message that says the server is null:
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.starttls.required", true);