How To Send E-Mail Using JSP Code..

                                   How To Send Mail Using JSP Code.

Hi Guys,
               Below I have written the code snippet for sending the email using Java Server Pages. Many of the developers have facing the probelms regarding this so use this code in yours pages and run your application smoothly.


String host = "smtp.gmail.com";
String to=emid2;
String from = "Techsupport@gmail.com";
String cc = "Techsupport@gmail.com";
String subject = "TechSupport";

String pass = "pass";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "465");//Gmail smtp port is 465.
props.put("mail.smtp.auth", "true");

Session session1 = Session.getInstance(props, new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication() {    

//Please Set your Email ID and Passowrd.

return new PasswordAuthentication("Techsupport@gmail.com","yourpassword");
}                                                                  
});                                                                  
MimeMessage message = new MimeMessage(session1);
Address fromAddress = new InternetAddress(from);
Address toAddress = new InternetAddress(to);
Address ccAddress = new InternetAddress(cc);
//Address bccAddress = new InternetAddress(bcc);

message.setFrom(fromAddress);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.setRecipient(Message.RecipientType.CC, ccAddress);
//message.setRecipient(Message.RecipientType.BCC, bccAddress);

message.setSubject("Tech Support");//This is subject of email you can change it according to you.
message.setText("Hi,\n\t"Testing is Done"\n Regards,\n\ Techsupport " );//This is email body Content which you want to write.
Transport transport = session1.getTransport("smtp");
transport.connect(host, from, pass);
message.saveChanges();
Transport.send(message);
transport.close();

Thanks,

Comments

  1. i am getting following error.....pls help me....

    org.apache.jasper.JasperException: An exception occurred processing JSP page /sendmail2.jsp at line 49

    46: message.setSubject("Tech Support");//This is subject of email you can change it according to you.
    47: message.setText("Hi");
    48: Transport transport = session1.getTransport("smtp");
    49: transport.connect(host,from,pass);
    50: message.saveChanges();
    51: Transport.send(message);
    52: transport.close();


    thank you
    in advance

    ReplyDelete
  2. Hi dileep,
    Can you show me what packages are you using in code.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Read Excel File in VC++....

Window could not start the Apache tomcat 6 on Local Computer.For more information, review the System Events Logs.If this is a non-Microsoft service,contact service vendor, and refer to service-specific error code 0.

How to Bind Multiple SMPP Acounts Using Kannel SMS Gateway.