Click here to Skip to main content
15,890,579 members
Home / Discussions / Java
   

Java

 
GeneralRe: Beautiful tooltip for TrayIcon [modified] Pin
sharkbc10-Sep-09 18:35
sharkbc10-Sep-09 18:35 
GeneralRe: Beautiful tooltip for TrayIcon Pin
427748011-Sep-09 11:22
427748011-Sep-09 11:22 
GeneralRe: Beautiful tooltip for TrayIcon Pin
sharkbc13-Sep-09 21:52
sharkbc13-Sep-09 21:52 
Questionrdp platform independent Pin
ashishqwerty8-Sep-09 13:40
ashishqwerty8-Sep-09 13:40 
AnswerRe: rdp platform independent Pin
42774808-Sep-09 15:10
42774808-Sep-09 15:10 
Questionj2ee code for sending mail [modified] Pin
sangeeta20098-Sep-09 5:09
sangeeta20098-Sep-09 5:09 
AnswerRe: j2ee code for sending mail Pin
Nagy Vilmos8-Sep-09 10:39
professionalNagy Vilmos8-Sep-09 10:39 
AnswerRe: j2ee code for sending mail Pin
42774808-Sep-09 13:03
42774808-Sep-09 13:03 
Sending an Email in Java (Servlet)

Prerequisites:
1. Java Mail API (mail.jar)
2. activation.jar
3. HTML Form (Optional)
4. Servlet or JSP (to send the email)

Steps:
1. Download and install JDK6U16
2. Download Apache Tomcat
3. Download Eclipse
4. In Eclipse, make sure to add the apache server first
5. Create a dynamic web project
6. Under the WEB-INF create a folder called lib and paste mail.jar and activation.jar
7. Create a jsp and call it index.jsp
8. Create a servlet and call it sendMail
9. Create a class called MyPasswordAuthenticator (will act as authentication or you can use the code inside the sendMail as you like)
10. Create two jsps (success and error)
11. Create a css file called style

index.jsp Code:
<html>
<head>
<title>Sending email</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<center>
<form action="sendMail" method="get">   
<table>
<tr>
<td>From</td>
<td><input type="text" name="from"></td>
</tr>
<tr>
<tr>
<td>To</td>
<td><input type="text" name="to"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="subject"></td>
</tr>    
<tr>
<td>Message</td>
<td><textarea cols="25" rows="8" name="message"></textarea></td>
</tr>
</table>
<br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>


style.css
* { font-size: 12px; font-family: Verdana }
input, textarea { border: 1px solid #ccc }
textarea { text-align:left}
table { margin-top: 10% }
.error { margin-top: 10%; border: 1px dotted #db1f1f; width: 250px }
.msg { margin-top: 10%; border: 1px dotted #ccc; width: 250px }


sendMail Servlet
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class sendMail extends HttpServlet 
{
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
	{
		String username = "your gmail account";
		String password = "your gmail password";
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.starttls.enable", "true");

		Session session = null;
		if (username != null && password != null) 
		{
			props.put("mail.smtp.auth", "true");
			session = Session.getInstance(props, new MyPasswordAuthenticator(username, password));
		} 
		else 
		{
			session = Session.getDefaultInstance(props, null);
		}

		MimeMessage message = new MimeMessage(session);
		try 
		{
			message.setFrom(new InternetAddress(req.getParameter("from")));
			InternetAddress[] address = InternetAddress.parse(req.getParameter("to"), false);
			message.setRecipients(Message.RecipientType.TO, address);
			message.setSubject(req.getParameter("subject"));
			message.setText(req.getParameter("message"));
			Transport.send(message);
			RequestDispatcher d = req.getRequestDispatcher("success.jsp");
			d.forward(req, resp);
		} 
		catch (Exception e) 
		{
			try 
			{
				RequestDispatcher d = req.getRequestDispatcher("error.jsp");
				d.forward(req, resp);
			} 
			catch (Exception e2) {}
		}
	}
}


MyPasswordAuthenticator
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

class MyPasswordAuthenticator extends Authenticator
{
	String user;
	String pw;
	public MyPasswordAuthenticator (String username, String password)
	{
		super();
		this.user = username;
		this.pw = password;
	}
	public PasswordAuthentication getPasswordAuthentication()
	{
		return new PasswordAuthentication(user, pw);
	}
}


success.jsp
<html>
  <head>
    <title>Message</title>
  </head>
  <body>
    <center>
      <div class="msg">
        <h2>Message</h2>
          <p>
               Email sent
          </p>
      </div>
   </center>
  </body>
</html>


error.jsp
<html>
  <head>
    <title>Error</title>
      <link rel="stylesheet" href="style.css" type="text/css">
    </head>
    <body>
      <center>
        <div class="error">
          <h2>Error</h2>
            <p>
               Message: <%= request.getAttribute("ErrorMessage") %>    
            </p>
        </div>
      </center>
    </body>
</html>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SendingEmail</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>sendMail</display-name>
    <servlet-name>sendMail</servlet-name>
    <servlet-class>sendMail</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>sendMail</servlet-name>
    <url-pattern>/sendMail</url-pattern>
  </servlet-mapping>
</web-app>


Make sure to put your gmail username and password in sendMail servlet
Next run index.jsp

Good luck

modified on Tuesday, September 8, 2009 7:35 PM

GeneralRe: j2ee code for sending mail Pin
sangeeta200910-Sep-09 22:51
sangeeta200910-Sep-09 22:51 
GeneralRe: j2ee code for sending mail Pin
427748011-Sep-09 13:27
427748011-Sep-09 13:27 
GeneralRe: j2ee code for sending mail Pin
sangeeta200917-Sep-09 6:21
sangeeta200917-Sep-09 6:21 
GeneralRe: j2ee code for sending mail Pin
427748018-Sep-09 15:25
427748018-Sep-09 15:25 
GeneralRe: j2ee code for sending mail Pin
sangeeta200923-Sep-09 5:16
sangeeta200923-Sep-09 5:16 
GeneralRe: j2ee code for sending mail Pin
427748023-Sep-09 18:25
427748023-Sep-09 18:25 
Questionhow to convert swings to java applet Pin
Member 46403268-Sep-09 3:32
Member 46403268-Sep-09 3:32 
AnswerRe: how to convert swings to java applet Pin
Nagy Vilmos8-Sep-09 10:36
professionalNagy Vilmos8-Sep-09 10:36 
AnswerRe: how to convert swings to java applet Pin
42774808-Sep-09 13:06
42774808-Sep-09 13:06 
Questiondeployment eerrotr 404 error Pin
kirancgi7-Sep-09 21:48
kirancgi7-Sep-09 21:48 
AnswerRe: deployment eerrotr 404 error Pin
thomas.michaud8-Sep-09 3:40
thomas.michaud8-Sep-09 3:40 
AnswerRe: deployment eerrotr 404 error Pin
42774808-Sep-09 13:11
42774808-Sep-09 13:11 
QuestionKey Logger Pin
badrisuper6-Sep-09 21:55
badrisuper6-Sep-09 21:55 
AnswerRe: Key Logger Pin
TorstenH.7-Sep-09 1:35
TorstenH.7-Sep-09 1:35 
AnswerRe: Key Logger Pin
EliottA7-Sep-09 4:44
EliottA7-Sep-09 4:44 
AnswerRe: Key Logger Pin
42774807-Sep-09 8:01
42774807-Sep-09 8:01 
QuestionDigital Signature Pin
vivhari3-Sep-09 20:26
vivhari3-Sep-09 20:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.