How to send an HTML email in Java (Using Google SMTP Server)
Join the DZone community and get the full member experience.
Join For FreeFor example :
- Confirming a user registration
- Password reset via emails
- Using javax.mail.jar directly
- Using Apache commons email jar which wraps javax.mail
Using javax.mail
try { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "false"); props.put("mail.smtp.ssl.enable", "true"); Session session = Session.getInstance(props, new EmailAuth()); Message msg = new MimeMessage(session); InternetAddress from = new InternetAddress("sendersEmailAddress", "Sender's name"); msg.setFrom(from); InternetAddress toAddress = new InternetAddress("Receiver's email"); msg.setRecipient(Message.RecipientType.TO, toAddress); msg.setSubject("Test"); msg.setContent(msg.setContent("<html>\n" + "<body>\n" + "\n" + "<a href=\"http://pushpalankajaya.blogspot.com\">\n" + "This is a link</a>\n" + "\n" + "</body>\n" + "</html>", "text/html");, "text/html"); Transport.send(msg); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } catch (MessagingException ex) { ex.printStackTrace(); } } static class EmailAuth extends Authenticator { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("sendersEmailAddress", "password"); } }
Using Apache commons e-mail
HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.gmail.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("sendersEmailAddress", "password")); email.setSSLOnConnect(true); email.setFrom("Senders' email"); email.setSubject("TestMail- Alternative message"); email.setHtmlMsg("<html>\n" + "<body>\n" + "\n" + "<a href=\"http://pushpalankajaya.blogspot.com\">\n" + "This is a link</a>\n" + "\n" + "</body>\n" + "</html>"); // set the alternative message email.setTextMsg("This is a link: http://pushpalankajaya.blogspot.com"); email.addTo("lanka@wso2.com"); email.send();
Here with setTextMsg method we can set a plain text message to be shown
at receiver's end if receiver is not supporting HTML content in emails.
If the gmail account used to send the email is 2-Step verification
enabled and you used your usual password to send the email in code, you
will get a similar error to the following.
javax.mail.AuthenticationFailedException: 534-5.7.9 Application-specific password required. Learn more at 534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833
The solution is to go to Account>Security>App passwords>Settings and generate a password for the app we are to run.
Reference
[1] - http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/javamail/javamail.html
[2] - http://commons.apache.org/proper/commons-email/userguide.html
Published at DZone with permission of Pushpalanka Jayawardhana, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
-
Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
-
Microservices Decoded: Unraveling the Benefits, Challenges, and Best Practices for APIs
-
Observability Architecture: Financial Payments Introduction
Comments