Sending email with Java

Sending email with Java

Sending email with Java

You can insert HTML tags when sending email in with Java in text format but that isn’t enough to make the e-mail interpreted as an HTML e-mail in a web mail or an e-mail client program. So, instead of using:

First of all, download the Java Mail jar file (to add to your Classpath) here


Message msg = new MimeMessage(session);
msg.setText(message);

we should invoke the setContent(Object obj, String type) method of the MimeMessage object. The setContent() method specifies the mime type of the content explicitly, and for HTML format, the type parameter must be “text/html”:


Message msg = new MimeMessage(session);
msg.setContent(message, "text/html");

Below is a sample program that sends email in HTML format, the Program uses Gmail SMTP to send, but you can use domain email to do same (although you will need to change SMTP server information)


package anatech.javamail;

import java.util.Date;
import java.util.Properties;

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class HtmlEmailSender {

public void sendHtmlEmail(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message) throws AddressException,
MessagingException {

// sets SMTP server properties
Properties properties = new Properties();
properties.put(“mail.smtp.host”, host);
properties.put(“mail.smtp.port”, port);
properties.put(“mail.smtp.auth”, “true”);
properties.put(“mail.smtp.starttls.enable”, “true”);

// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};

Session session = Session.getInstance(properties, auth);

// creates a new e-mail message
Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// set plain text message
msg.setContent(message, “text/html”);

// sends the e-mail
Transport.send(msg);
}


/**
* Test the send html e-mail method
*
*/
public static void main(String[] args) {
// SMTP server information
String host = "smtp.gmail.com";
String port = "587";
String mailFrom = "Insert your email address";
String password = "Insert your email password";

// outgoing message information
String mailTo = “Recipients email address”;
String subject = “Insert email subject”;

// message contains HTML markups
String message = “<i>Good Day</i>
“;
message += “<b>Wish you a nice day!</b>
“;
message += “<span style=”color: red;”>Anatech</span>”;

HtmlEmailSender mailer = new HtmlEmailSender();

try {
mailer.sendHtmlEmail(host, port, mailFrom, password, mailTo,
subject, message);
System.out.println(“Email sent.”);
} catch (Exception ex) {
System.out.println(“Failed to sent email.”);
ex.printStackTrace();
}
}
}

You can also check if the email address is a valid one using the method below, the methos return true if the email is valid and returns false if otherwise


public static boolean isValidEmailAddress(String email) {
boolean result = true;
try {
InternetAddress emailAddr = new InternetAddress(email);
emailAddr.validate();
}
catch (AddressException ex) {
result = false;
}
return result;
}