Friday, April 23, 2010

Sending mail with attachment from java code

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package success;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail
{
String mailhost = "smtp.gmail.com"; //it is googles mail host use proper mail host for proper server
String senderid = "senderid@gmail.com"; ///authentication mail id
String password = ""; //password
String smtpport = "587";

String messagetype = "text/plain";
SendMail( String to, String message,String subject,String Username)
{

Properties props = new Properties();
props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.port", smtpport);
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.localhost", "gamil.com");



Session s = Session.getInstance(props, null);
s.setDebug(true);

MimeMessage message1 = new MimeMessage(s);
try
{
InternetAddress from1 = new InternetAddress(senderid, Username);
InternetAddress to1 = new InternetAddress(to);

message1.setSentDate( new Date() );
message1.setFrom( from1 );
message1.addRecipient(javax.mail.Message.RecipientType.TO, to1);

message1.setSubject(subject);
message1.setContent(message, messagetype);

Transport tr = s.getTransport("smtp");
tr.connect(mailhost, senderid, password);
message1.saveChanges();
tr.sendMessage(message1, message1.getAllRecipients());
tr.close();

}
catch (Exception e)
{
System.out.println(e.toString());
}

}

SendMail( String to, String message,String subject,String Username, String[] attachments)
{

Properties props = new Properties();
props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.port", smtpport);
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.localhost", "gamil.com");



Session s = Session.getInstance(props, null);
s.setDebug(true);

MimeMessage message1 = new MimeMessage(s);
try
{
InternetAddress from1 = new InternetAddress(senderid, Username);
InternetAddress to1 = new InternetAddress(to);

message1.setSentDate( new Date() );
message1.setFrom( from1 );
message1.addRecipient(javax.mail.Message.RecipientType.TO, to1);

message1.setSubject(subject);

// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);

//use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();

//add the message body to the mime message
multipart.addBodyPart(messageBodyPart);

// add any file attachments to the message
addAtachments(attachments, multipart);

// Put all message parts in the message
message1.setContent(multipart);

// Send the message
Transport tr = s.getTransport("smtp");
tr.connect(mailhost, senderid, password);
message1.saveChanges();
tr.sendMessage(message1, message1.getAllRecipients());
tr.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}

}

protected void addAtachments(String[] attachments, Multipart multipart)
throws MessagingException, AddressException
{
for (int i = 0; i <= attachments.length - 1; i++) {
String filename = attachments[i];
MimeBodyPart attachmentBodyPart = new MimeBodyPart();

//use a JAF FileDataSource as it does MIME type detection
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));

//assume that the filename you want to send is the same as the
//actual file name - could alter this to remove the file path
attachmentBodyPart.setFileName(filename);

//add the attachment
multipart.addBodyPart(attachmentBodyPart);
}
}



public static void main(String[] args) {
// TODO code application logic here
//Text Mail Sending................
SendMail sm = new SendMail("reciverid@yahoo.co.in", "Java mail sending test", "test", "debmalya");


//Attachment mail sending................
String[] filenames = {"d:\\B2.JPG"};
SendMail sm1 = new SendMail("reciverid@gmail.com", "Java mail sending test", "test", "debmalya",filenames);
}
}

1 comment: