J2EE JavaMail 1.5 : Sending mail using Web Application
JavaMail using Web Application Example |
JavaskoolEmailer.java
package com.javaskool;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaskoolEmailer {
static Properties properties = new Properties();
static {
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.port", "465");
}
public String send(final String from,final String password, String to, String subject, String body) {
String ret = "ERROR";
try {
Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// message.setRecipients(Message.RecipientType.CC,InternetAddress.parse(to));
// message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
ret = "SUCCESS";
} catch (MessagingException e) {
ret = "ERROR";
e.printStackTrace();
}
return ret;
}
public static Properties getProperties() {
return properties;
}
public static void setProperties(Properties properties) {
JavaskoolEmailer.properties = properties;
}
}
index.jsp
<html>
<head>
<title>javamail demo</title>
</head>
<body>
<form action="sendMail.jsp" method="post">
<table border="1">
<tr bgcolor="cyan">
<td colspan="2">Login Section</td>
</tr>
<tr>
<td>Email/UserId</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td>Password<td>
<td><input type="password" name="password" size="30"></td>
</tr>
<tr bgcolor="cyan">
<td colspan="2">Email Compose Section</td>
</tr>
<tr>
<td>To</td>
<td><input type="text" name="to" size="30"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="subject" size="30"></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="message" cols="20" rows="5"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Send"></td>
</tr>
</table>
</form>
</body>
</html>
sendMail.jsp
<%@page import="com.javaskool.JavaskoolEmailer"%>
<%
try
{
JavaskoolEmailer email=new JavaskoolEmailer();
String to=request.getParameter("to");
String subject=request.getParameter("subject");
String message=request.getParameter("message");
String username=request.getParameter("username");
String password=request.getParameter("password");
String status= email.send(username,password,to, subject,message);
//String status= email.send("JAMES@gmail.com", "XXXXX", "smith@yahoo.com", "Testing Mail", "Hi I a james Bond..");
if(status.equals("SUCCESS"))
{
out.println("Mail has been sent Successfully.");
}
else
{
out.println("Mail has not been sent; FAIL.");
}
}
catch(Exception e)
{
out.println(e.toString());
}
%>
Click here to download javax.mail.jar
Recent Comments