J2EE JavaMail 1.5 : Sending Attachment
JavaMail Sending Attachment Example |
JavaskoolEmailer.java
package com.javaskool;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
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 filename) {
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);
//1. Adding Messaage
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the body message
//message.setText(body);
messageBodyPart.setText(body);
//2. Adding file attachment
// Create a multipart message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);// Adding 1
// Part two is attachment
messageBodyPart = new MimeBodyPart();
//String filename = "myfile.txt";
javax.activation.DataSource source=new FileDataSource(filename);
//DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);// Adding 2
// Send the complete message parts
message.setContent(multipart );
Transport.send(message);
ret = "SUCCESS";
} catch (MessagingException e) {
ret = "ERROR";
e.printStackTrace();
}
catch(Exception e1)
{
ret = "ERROR";
e1.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>Attachment</td>
<td><input type="file" name="attachment" size="30"></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 attachment=request.getParameter("attachment");
String username=request.getParameter("username");
String password=request.getParameter("password");
String status= email.send(username,password,to, subject,message,attachment);
//String status= email.send("JAMES@gmail.com", "XXXXX", "smith@yahoo.com", "Testing Mail", "Hi I a james Bond..","file.txt");
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