java2db

How to send email in java ,jsp and servlet



To send email in java , use javax.mail API . Most of the java web applications , sends email  to the users , using mail application programming interface .

Required Jar files :
send email in java mail.jar (Download mail.jar )

send email in java stand alone :

send email in java  To send email in java stand alone application, add the mail.jar file in the class path.
send email in java  compile and run the below example code, using command prompt or IDE .

 Send email to more than one email ID’s: Do the below two changes (in line number 25 and 45 ) .

Send email in jsp :

send email in java Create new jsp file  in the web project  ,  copy the below code.

send email in java Add the mail.jar file to the libraries .

send email in java Run the jsp file from the web browser .

 

Send email in Servlet:

send email in java Create a web project in any IDE .
send email in java Add the mail.jar file to the libraries .
send email in java create .jsp file , servlet , web.xml files .
send email in java Deploy the web application in server.
send email in java Run the .jsp file from the web browser.

input.jsp  file :

sendEmailApp.java  file

web.xml

Same way we can send email in java web applications like struts , spring and any other .

In the above sample code , email sends from yahoo .
Change the SMTP server host(mail.smtp.host) and port(mail.smtp.port) , to send email from any other email service (like Gmail,Rocketmail) .

Some of the common SMTP servers :

 Mail Service  Server Host  Port
Gmail smtp.gmail.com 587
Yahoo Mail smtp.mail.yahoo.com 587
Hotmail smtp.live.com 25
Rocketmail smtp.mobile.mail.yahoo.com 25



Related Posts :
How to Connect to Oracle from Liferay portlet ( Java Liferay and Oracle integration )
How to Connect Liferay 7 to PostgreSQL ( Java Liferay portlet and PostgreSQL integration )
How to call C# .Net WCF web service in java SOAP client ( Visual Studio 2013 )
How to call PHP RESTful web service in java client
How to call java SOAP web service in php ( php web service client )
Solution to SSLHandshakeException (skip the certificate for Java and LDAP bind)
Java RESTful web service example in maven using JAX-RS
RESTful web service example in java using JAX-RS
   [will not be published]



^ <