CATEGORIES
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)
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();
Most Popular Articles
- How to Configure 404 Error or Page not Found on Tomcat
- Sending Email from JSP Pages Using smtp.gmail.com
- JSP Web Services Example - XML, XSLT in JSP - XML Web Services in Java
- How to Email from JSP Page?
- How to Change the Image onMouseOver
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: Maxim Vasilkov at gmail
Works perfect!
Name: Vladimir
Works for me. Thank you.
Name: Muthu
Hi it’s working fine for me but use the following code
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
String host="", user="", pass="";
host ="smtp.gmail.com"; //"smtp.gmail.com";
user =""; //"YourEmailId@gmail.com" // email id to send the emails
pass ="password"; //Your gmail password
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to =""; // out going email id
String from =""; //Email id of the recipient
String subject ="welcome god";
String messageText ="welcome god";
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());
out.println("message successfully sended"); // assume it was sent
}
catch (Exception err) {
out.println("message not successfully sended"); // assume it’s a fail
}
transport.close();
%>
Name: charlyn
very helpful. it works!
Name: Ahmed Abd El-Rasoul
thanks ,it’s works :)
Name: neelam
thanks ,it’s works :)
Name: priya
thanx it works:)
Name: Santosh
Thank u very Much this code helped me a lot
Name: Arundev
This is not work for me.An error showing "Session cannot be resolved".What is the reason?
Name: mpoulhs
Muthu thanks a lot! :)
Name: hao
thanks u
Name: Ramesh.M
It is very useful for me. i have more example written but it is not working. This coding execute successfully.....