Monday, August 11, 2008

Java Class to Implement Email

Hi, The java developers can use the following code to send email through code. The developers should have smtp (Simple Mail Transfer Protocol) server, where she/he can authenticate her/him self with the help of her/his credentials. The default port the smtp server normally uses is 25. The developers can check it whether it works or not. Here we are going to use the default mail package of sun. You can download the javax.mail package from the following link: http://java.sun.com/products/javamail/.
The API is also available through the following link:
http://java.sun.com/products/javamail/javadocs/index.html?javax/mail/package-summary.html

The following code is useful for different scenarios. For example there are four constructors where you can define the possible situations of having cc, attachment. The possible combinations are:
SUBJECT, FROM, TO, CC, ATTACHMENT, BODY.
SUBJECT, FROM, TO, ATTACHMENT, BODY.
SUBJECT, FROM, TO, CC, BODY.
SUBJECT, FROM, TO, BODY.

The code does not authenticate the email id. So you can easily mention any non existing email id also.
package mailpackage;


import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Flags;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;


public class ErrorMail {

String subject;
String from;
String[] to;
String[] cc;
String attachmentpath;
String body;

/**
* @param subject SUBJECT
* @param from FROM
* @param to TO
* @param cc CC
* @param attachmentpath ATTACHMENTPATH
* @param body BODY
*/

public ErrorMail(String subject,String from,String[] to,String[] cc,String attachmentpath,String body){
this.subject = subject;
this.from = from;
this.to = to;
this.cc = cc;
this.attachmentpath = attachmentpath;
this.body = body;
}

/ *
* @param subject SUBJECT
* @param from FROM
* @param to TO
* @param attachmentpath ATTACHMENTPATH
* @param body BODY
*/



public ErrorMail(String subject,String from,String[] to,String attachmentpath,String body){
this.subject = subject;
this.to = to;
this.attachmentpath = attachmentpath;
this.body = body;
this.from = from;
}


/**
* @param subject SUBJECT
* @param from FROM
* @param to TO
* @param cc CC
* @param body BODY
*/

public ErrorMail(String subject,String from,String[] to,String cc[],String body){
this.subject = subject;
this.to = to;
this.cc = cc;
this.body = body;
this.from = from;
}




/**
* @param subject SUBJECT
* @param from FROM
* @param to TO
* @param body BODY
*/


public ErrorMail(String subject,String from,String[] to,String body){
this.subject = subject;
this.to = to;
this.body = body;
this.from = from;
}




public void run() {
try{
Session session = getSession();
MimeMessage message = new MimeMessage(session);
session.setDebug(true);
InternetAddress[] addressTO = new InternetAddress[to.length];
for(int i=0;i < to.length;i++){
System.out.println("to is "+to[i]);
addressTO[i] = new InternetAddress(to[i]);
}
message.setRecipients(RecipientType.TO,addressTO);
System.out.println("cc "+cc);
if(cc!=null){
InternetAddress[] addressCC = new InternetAddress[cc.length];
for(int i=0;i < cc.length;i++){
System.out.println("cc is "+cc[i]);
addressCC[i] = new InternetAddress(cc[i]);
}
message.setRecipients(RecipientType.CC,addressCC);
}
System.out.println("from : "+from);
message.addFrom(new InternetAddress[] { new InternetAddress(from) });
message.setSubject(subject);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);

Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
System.out.println("attachmentpath "+attachmentpath);
if(attachmentpath!=null){
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachmentpath);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
}

message.setContent(mp);

Transport.send(message);
}catch(MessagingException me){
me.printStackTrace();
}
}

private Session getSession() {
Authenticator authenticator = new Authenticator();

Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");

properties.setProperty("mail.smtp.host",”myhost.smtpserver”);
properties.setProperty("mail.smtp.port",”myhostport”);

return Session.getInstance(properties, authenticator);
}

private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;

public Authenticator() {
String username =”validuserofthesmtpserver”;
String password =”password”;
authentication = new PasswordAuthentication(username, password);
}

protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}

public static void main(String[] args) throws MessagingException {
ErrorMail mwpa1 = new ErrorMail("SUBJECT","abc@domain.com",new String[] {"xyz @domain.com","efg @domain.com"},"BODY");
mwpa1.run();

ErrorMail mwpa2 = new ErrorMail("SUBJECT","abc@domain.com",new String[] {"pqr@domain.com","efg@domain.com","xyz@domain.com"},"C:\\attachment.txt ","Body");
mwpa2.run();
ErrorMail mwpa3 = new ErrorMail("SUBJECT","abc@domain.com",new String[] {"abc@domain.com","efg@domain.com","pqr@domain.com"},new String[] {"abc@domain.com","pqr@domain.com"}," Body");
mwpa3.run();
ErrorMail mwpa4 = new ErrorMail("SUBJECT","abc@domain.com",new String[] {"abc@domain.com","efg@domain.com","pqr@domain.com","xyz @dsomain.com"},"Body");
mwpa4.run();

}
}

Hope you all will find it useful.

No comments:

Total Pageviews