Sending Mail Through Mail JSP page

by guruprasad 2014-02-15 10:01:29

To send mail through JSP page,follow the below steps,

Steps:

1.Import the necessary packages,

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

2.Configure(here,assign) the basic mailing informations like to_address,from_address,etc.,

String to_address="abc@gmail.com"; //sender's mail id
String from_address="def@gmail.com"; //recipient's mail id
String host_id=""; //your host id

3.Configure the properties object like host,port,authentication and so on..

Properties properties = System.getProperties(); //Getting system properties.
properties.setProperty("mail.smtp.host", host); //setting host id.
properties.put("mail.smtp.port", "25"); //configuring port number.
properties.setProperty("mail.smtp.auth", "true"); //setting authentication property.
properties.put("mail.transport.protocol", "smtp");


Session mailSession = Session.getDefaultInstance(properties); // Get the default Session object.


4. Sending mail process.

try
{

MimeMessage msg = new MimeMessage(mailSession); //setting mime object for sending(message).

msg.setFrom(new InternetAddress(from)); //setting from address in mail's header.

msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); //setting msg content to recipient's address.

msg.setSubject("Sample Message Text"); //setting subject content.

msg.setText("This is actual message"); //setting msg content.

Transport.send(msg); // sending message.
out.println("Mail Sent Successfully");
}
catch (Exception e)
{
out.println(e);

}

578
like
0
dislike
0
mail
flag

You must LOGIN to add comments