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

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: , , ,

Feb 20

When using Javamail to send mail the Message-ID gets set when the message is actually sent. If you want to provide your own Message-ID then you need to subclass MimeMessage. You can then use that subclass to create your message.

public class CustomMimeMessage extends MimeMessage {

	public CustomMimeMessage(Session session) {
		super(session);
	}

	@Override
	protected void updateMessageID() throws MessagingException {
		setHeader("Message-ID", "objects-message-id");
	}	
}

written by objects \\ tags: , ,