Thursday, March 5th, 2009
| Apache Commons has many built-in utils. One of the most used util is Commons-Email. Couple of week back had a situation need to send a mail to the users from my application. So i ended here. |
| The implementation is very simple. Commons-Email is built on top of the Java Mail API. |
Some of the mail classes that are provided are as follows:
- SimpleEmail-This class is used to send basic text based emails.
- MultiPartEmail-This class is used to send multipart messages.
- HtmlEmail-This class is used to send HTML formatted emails.
- EmailAttachment-This is a simple container class to allow for easy handling of attachments.
|
| The following piece of code will send an mail by using Commons-Email |
package com.techmaddy;
import org.apache.commons.mail.HtmlEmail;
public class MailMan {
public static void main(String[] args) {
try {
HtmlEmail email = new HtmlEmail();
email.setHostName("mysmtp.server.com");
email.addTo("test@test.com", "KMK");
email.setFrom("admin@ttext", "Admin");
email.setSubject("WARNING::Be Aware of this");
email.setHtmlMsg("<b>Read this from techmaddy.com</b>");
email.send();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
|
| If you SMTP Server needs authentication |
email.setAuthentication("username", "password");
|
| For more ref Commons-Email |
Tags: Apache Commons, Core Java, Email, Mail
Posted in General, Java | No Comments »