Posts Tagged ‘Apache Commons’

Apache Commons-Email

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
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Get Adobe Flash player