Send Mail in JAVA

Send Mail in java…

SendMail.java


package protech;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail
{
	public	String host;
	public	String to ;
	public	String from ="sanjuindia2005@gmail.com";
	public	String subject ;
	public	String messageText;
	public	boolean sessionDebug = false;	
	public  String smtpPort="25";
		
	public void sendMail()
	{
		try
		{
			System.out.println("From :-------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+from);
			System.out.println("\n<----------------1------------------------>\n");
			Properties props = System.getProperties();
			props.put("mail.host", "192.168.100.24");
			props.put("mail.transport.protocol", "smtp");
			props.put("mail.smtp.port", smtpPort);
			System.out.println("\n<----------------2------------------------>\n");
			Session mailSession = Session.getDefaultInstance(props, null);
			System.out.println("\n<----------------3------------------------>\n");
			mailSession.setDebug(sessionDebug);
			System.out.println("\n<----------------4------------------------>\n");
			Message msg = new MimeMessage(mailSession);
			System.out.println("\n<----------------5------------------------>\n");
			msg.setFrom(new InternetAddress("sanjuindia2005@gmail.com"));
			System.out.println("\n<----------------6------------------------>\n");
			InternetAddress[] addressForMail = {new InternetAddress(to)};
			System.out.println("\n<----------------7------------------------>\n");
			msg.setRecipients(Message.RecipientType.TO, addressForMail);
			System.out.println("\n<----------------8------------------------>\n");
			msg.setSubject(subject);
			System.out.println("\n<----------------9------------------------>\n");
			msg.setSentDate(new java.util.Date());
			System.out.println("\n<----------------10------------------------>\n");
			msg.setContent(messageText ,"text/html");
			System.out.println("\n<----------------11------------------------>\n");
			Transport.send(msg);
			System.out.println("\n<----------------12------------------------>\n");
		}
		catch(Exception ex)
		{
			System.out.println("\n Error in Send Mail :" + ex.toString());
		}
	}
}

Advertisements

About Sanju
I am Software Programmer. I am working in JAVA/J2EE Technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: