Dec 18

If you get the following error and not sure how to fix it then read on.


Caused by: javax.mail.MessagingException: 501 Syntax: HELO hostname
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363)
at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375)
at javax.mail.Service.connect(Service.java:248)

This is typically caused by the system property “mail.smtp.localhost” not being set by your Java VM. This should be already set for you automatically if your runtime environment is configured properly.

From the javadoc:

Local host name used in the SMTP HELO or EHLO command. Defaults to InetAddress.getLocalHost().getHostName(). Should not normally need to be set if your JDK and your name service are configured properly.

Hope this helps.

written by objects \\ tags: , , , , ,

Oct 12

Googles Gmail SMTP server supports the use of TLS (Transport Layer Security). When using a Gmail SMTP server with Javamail we can configure Javamail to use TLS using the session properties.

	    Properties props = new Properties();
	    props.put("mail.transport.protocol", "smtp"); 
	    props.put("mail.smtp.starttls.enable", "true"); 
	    props.put("mail.smtp.host", "smtp.gmail.com"); 
	    props.put("mail.smtp.auth", "true"); 
    // use your gmail account username here
	    props.put("mail.smtp.user", "username@gmail.com");
	    props.put("mail.smtp.port", "465");
	    props.put("mail.mime.charset", "ISO-8859-1");
	    props.put("mail.smtp.socketFactory.port", "465");
	    props.put("mail.smtp.socketFactory.fallback", "false");
	    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

You’ll also need to specify an Authenticator when creating the session.

You can then use the session to send your mail.

Putting it all together we get this


// Create session
    
Properties sessionProperties = System.getProperties();
props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.auth", "true"); 
// use your gmail account username here
props.put("mail.smtp.user", "username@gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.mime.charset", "ISO-8859-1");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = 
   Session.getDefaultInstance(sessionProperties, new PasswordAuthenticator());
    
// Create message
    
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, 
   InternetAddress.parse(to, false));
message.setSubject(subject);
message.setText(body);

// Send the message

Transport.send(message); 

written by objects \\ tags: , , , , , ,

Oct 12

When using an SMTP server that requires authentication with javamail we need a way to pass the username and password. Javamail provides an Authenticator class for this which we need to subclass to use.

For standard usage we just need to override the getPasswordAuthentication() method to return a PasswordAuthentication instance containing the username and password to be used by javamail when authenticating with the SMTP server.

	class PasswordAuthenticator extends javax.mail.Authenticator {
	    public PasswordAuthentication getPasswordAuthentication() {
			String user = "username@gmail.com";
			String pwd = "password";
			return new PasswordAuthentication(user, pwd);
	    }
	}

Once we have our authenticator we pass it (along with standard properties) to create our javamail session.


	    Authenticator authenticator = new PasswordAuthenticator();
	    Session session = Session.getDefaultInstance(props, authenticator);

You can then use the session to send your mail.

written by objects \\ tags: , , ,