Java Program for sending Emails
Documents Created by : Bharat Gali
Created date : 17-Feb-2005
Last Revised :17-Feb-2005
*********************************************************************************
Example java program to send emails with attachments.
Input parameters for this program are
1)sender email id (p_from)
2)receipts email id (pto)
3)subject (psubject)
4)Email Body (pbody)
5)attachment (pattach1)
Attachment is location of the attachment. It can be multiple files separated by comas.
Example : c:/email_send/myimage.jpg;c:/email_send/bharatimage.jpg
Step1: Compile the code with Jdeveloper and generate the class file.
Step 2: Copy the class file to Oracle Application server middle tier home\j2ee\\home\default-web-app\WEB-INF\classes
Step 3: Test the program with the sample url
http://bharat.gali.com:7779/j2ee/servlet/send_email_program?&pfrom=bharat@bharatgali.com&pto=rani@bharatgali.com&psubjec t=test&pattach1=c:/email_send/myimage.jpg;c:/email_send/bharatimage.jpg
This program can be called by any third part program which can supply the above parameters.
Code
******************
//
Created by Bharat Gali on Feb-2005
import java.util.*;
import java.io.*;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
public class send_email_program extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException
{
try
{
PrintWriter out = res.getWriter();
String gfrom = req.getParameter("pfrom");
String gto = req.getParameter("pto");
String gsubject = req.getParameter("psubject");
String gbody = req.getParameter("pbody");
String gattach1 = req.getParameter("pattach1");
sendEmail(gfrom,gto,gsubject,gbody,gattach1);
out.println("Message Sent Successfully");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void doPOST(HttpServletRequest req, HttpServletResponse res) throws
ServletException
{
// If You use POST method then uncomment this statement
doGet(req,res);
}
public void sendEmail(String pfrom,String pto,String psubject,String
pbody,String pattach1)
{
try
{
// Change the below parameter accordingly.
String host = "smtp.bharatgali.com";
String to = pto;
String from = pfrom;
String from_send = "bharat@bharatgali.com";
String body = pbody;
String fileAttachment = pattach1;
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from_send));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(from));
message.setSubject(psubject);
MimeBodyPart messageBodyPart = new MimeBodyPart();
body = body.replace('~','\n');
messageBodyPart.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
int startIndex = 0;
int semicolonIndex = 0;
int lastsemicolonIndex = 0;
String attachment;
while ((semicolonIndex = fileAttachment.indexOf(";", startIndex)) != -1)
{
messageBodyPart = new MimeBodyPart();
attachment = fileAttachment.substring(startIndex, semicolonIndex);
DataSource source =
new FileDataSource(attachment);
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName(attachment);
multipart.addBodyPart(messageBodyPart);
startIndex = semicolonIndex + 1;
lastsemicolonIndex = semicolonIndex;
};
message.setContent(multipart);
Transport.send(message);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}