This java code is a working example.
Code: Select all
public static void testEmailRelay(){
// Recipient's email ID needs to be mentioned.
String[] to = {"someone@gmail.com"};
// Sender's email ID needs to be mentioned
String from = "no-reply@our.domain";
// Assuming you are sending email from through gmails smtp
String host = "smtp-relay.gmail.com";
// Get system properties
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", "false");
// Get the Session object.// and pass username and password
Session session = Session.getInstance(properties);
// Used to debug SMTP issues
session.setDebug(true);
String messageBody = "Test email body";
for (int i = 0; i < to.length; i++) {
sendEmail(to[i], from, session, messageBody);
}
}
private static void sendEmail(String to, String from, Session session, String messageBody) {
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: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("Test email subject");
// Now set the actual message
message.setText(messageBody);
System.out.println("sending...");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
The attached image show a config I tested to no prevail.