RSS feed

Sending Email from JSP Pages Using smtp.gmail.com

Sample Codes for Sending Emails from JSP Page Using Gmail

Published by: San (9/4/2007)

SEP 4 2007

How can I send emails from a jsp page? I have a page where i have a form and it sends out emails on submition. Please let me know the code and classes to include in jsp page to send emails.

This works for me. Try on your jsp page and if it doesn't work then post a comment here. Make sure to use the correct smtp server, user id and password. It can send text and html type of content so better comment and uncomment the codes on your wish.

String host="", user="", pass="";
host = smtp_server; //"smtp.gmail.com"; user = jsp_email; //"YourEmailId@gmail.com" // email id to send the emails
pass = jsp_email_pw; //Your gmail password
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to = email_to_address; // out going email id
String from = email_from_address; //Email id of the recipient
String subject = email_subject;
String messageText = email_body;
boolean sessionDebug = true;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html"); // use setText if you want to send text
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
try {
transport.sendMessage(msg, msg.getAllRecipients());
WasEmailSent = true; // assume it was sent
}
catch (Exception err) {
WasEmailSent = false; // assume it's a fail
}
transport.close();

 

Javascript Change Image onMouseOver >>

<< JSP Web Services Example - XML, XSLT in JSP - XML Web Services in Java

 

COMMENT

Name: Pankaj

Hi I tried for code but it is giving an error

javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.AuthenticationFailedException


please help me out................

PLease try to mail me ur suggestions at pvpatni@gmail.com

Name: goody

Thanks for that code.

Name: prem

how to know about the smtp server info?

Name: Anshul

its awesome...it worked soo easily..thnx..it solved my BIGGGGGG prob of triggering mail...

thanx...

Name: abdul

plz give proper step to use this code..i mean m nt getting were to pest this code..and how to use it...

Name: Vincent Lai

Pure Awesome, simple and sweet !

Name: Dumazzink

Where would be the most suitable place to put this code? In a Servlet?<br>
<br>
@prem - get the SMTP settings from the mailing server .. you must ask for it or see if it’s listed on their website. Some free email providers may not give you this information.

Name: Dumazzink

I’m getting an "javax.mail.AuthenticationFailedException" error - any help?

Name: Imran

Gr8 Thaks alot It works!

Name: Satheesh

hi i have been showing error at Session mailSession..

Name: kamini chaudhari

its very useful to me
thank u very much

Name: Sachin

Thank you very much, it helped me a lot.

Name: Chandan

thanks for the code .it helped me a lot.thanks

Name: Ravi

Hi there,

Thank you very much for this code. I was looking for this code for my project. i spent nearly 5 hrs on internet to find this code. once again thank you so much for this code

*Name:
*Comment:
You can use HTML tags here.
*Code: Please enter the sum of 5+2