Sending Attachment Mail Through java

Monday, July 13, 2009

The Google GMail service offers Attachments also

For this application you need two jar files those are
You need 2 jars : mail.jar and activation.jar.


The following java class will Explains Attachments in javamail
GoogleMailSenderWithAttachment.java


/**
* File Name : GoogleMailSenderWithAttachment.java
* Created By : Nagarajuv
* Created Date : Jun 19, 2009
* Purpose :
* Tables (for selecting) :
* Tables (by inserting/updating) :
* :::Change History :::
* Add comment
*/
package com.raj.mailservice;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import java.security.Security;
import java.util.Properties;

/**
* @author nagarajuv
*
*/
public class GoogleMailSenderWithAttachment {

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_PORT = "465";
private static final String emailMsgTxt = "

Hi! From HtmlJavaMail

";
private static final String emailSubjectTxt = "Hi raju accept my wishes";
private static final String emailFromAddress = "raju.veeranala@gmail.com";
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = {"raju.veeranala@gmail.com", "nagaraju.veeranala@valuemomentum.biz"};

public void sendSSLMessage(String recipients[], String subject,
String messageTxt, String from)throws MessagingException {

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("yourmail@gmail.com", "Password");
}
});

Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
System.out.println("recipient "+i+"-->"+recipients[i]);
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageTxt);

MimeMultipart multipart=new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("File Path");//Ex: D:\\vncclient.jar

messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("vncclient.jar");
multipart.addBodyPart(messageBodyPart);

msg.setSubject(subject);

msg.setContent(multipart);

Transport.send(msg);
}//end of sendSSLMessage

public static void main(String args[]) throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
new GoogleMailSenderWithAttachment().sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}//end main
}//end class

Enjoy with this is example
Image Hosted by ImageShack.us
Image Hosted by ImageShack.us

0 comments: