Step 1

Please make sure that you have necessary java libraries being used in this java source.

Step 2

Please change the following in this source:

  • sender@example.com: the sender's email address.
  • recipient@example.com: the recipient's email address.
  • USERNAME: your SMTP2GO username.
  • PASSWORD: your SMTP2GO password.
package smtp2go;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class Smtp2go {
    public static void main(String[] args) {
		final String username = "USERNAME";
		final String password = "PASSWORD"; 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "mail.smtp2go.com");
		props.put("mail.smtp.port", "2525"); // 8025, 587 and 25 can also be used. 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  }); 
		try { 
			Message message = new MimeMessage(session);
			Multipart mp = new MimeMultipart("alternative");
			BodyPart textmessage = new MimeBodyPart();
			textmessage.setText("It is a text message n");
			BodyPart htmlmessage = new MimeBodyPart();
			htmlmessage.setContent("It is a html message.", "text/html");
			mp.addBodyPart(textmessage);
			mp.addBodyPart(htmlmessage);
			message.setFrom(new InternetAddress("sender@example.com"));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse("recipient@example.com"));
			message.setSubject("Java Mail using SMTP2GO");
			message.setContent(mp); 
			Transport.send(message); 
			System.out.println("Done"); 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
    }
}

Ready to get started?

Try SMTP2GO free for as long as you like:

Try SMTP2GO Free → Paid plans available for over 1,000 emails/month.

Log In