Tuesday, November 16, 2010

Sending Mail using JAVA Class

PostMail: This class can be used to send mail using JAVA Class.

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class PostMail {

public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
boolean debug = false;

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.jcom.net");

Session session = Session.getDefaultInstance(props, null);

Message objMessage = new MimeMessage(session);

InternetAddress addressFrom = new InternetAddress(from);

InternetAddress[] objAddressTo = new InternetAddress[recipients.length];
for (int i = 0; i <>
objAddressTo[i] = new InternetAddress(recipients[i]);
objMessage.setRecipients(Message.RecipientType.TO, objAddressTo);

objMessage.addHeader("MyHeaderName", "myHeaderValue");

objMessage.setContent(message, "text/plain");
System.out.println("Message is send properly:");

public static void main(String args[])
PostMail objPostMail = new PostMail();
String[] objStringArray = new String[1];
objStringArray[0] = new String("siddhartha.dhumale@yahoo.com");
objPostMail.postMail(objStringArray, "Hi Subject", "Hi this is siddharatha", "siddharathadhumale@gmail.com");
}catch (Exception e)

No comments: