We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 8 September 2013

Sending Email with Attachment


In this tutorials we are going to see how to send the email with attachment using java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package used for sending email, for that add latest version of mail.jar into your build path, I used javamail-1.4.7 version.The javax.activation.*; package used for send email with attachment.Follow the simple steps for create application for send email with attachment using java
Create Java project called EmailSender in your eclipse IDE and then create package called com.javatutorialscorner.email under EmailSender project then create Java class called EmailSender.java under com.javatutorialscorner.email package.
EmailSender.java
package com.javatutorialscorner.email;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
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.MimeMultipart;

public class EmailSender {

/**
* @param args
*/
public static void main(String[] args) {
String TO = "toemail@gmail.com";
String host = "smtp.gmail.com";
String port = "465";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties mailConfig = new Properties();
mailConfig.put("mail.smtp.host", host);
mailConfig.put("mail.smtp.socketFactory.port", port);
mailConfig.put("mail.smtp.socketFactory.class", SSL_FACTORY);
mailConfig.put("mail.smtp.auth", "true");
mailConfig.put("mail.smtp.port", port);

Session session = Session.getDefaultInstance(mailConfig,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("fromemail@gmail.com","password");
}
});

try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
message.setSubject("Java Tutorials Corner - Email with Attachment");

BodyPart messageBodyTxt = new MimeBodyPart();
messageBodyTxt.setText("Simple Email sent by Java Tutorials Corner");

MimeBodyPart messageBodyAttachment = new MimeBodyPart();
String filePath = "C:\\attachment.txt";
DataSource source = new FileDataSource(filePath);
messageBodyAttachment.setDataHandler(new DataHandler(source));
messageBodyAttachment.setFileName("attachment.txt");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyTxt);
multipart.addBodyPart(messageBodyAttachment);
message.setContent(multipart);

Transport.send(message);
System.out.println("Email sent successfully");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

}

}

In above program smtp.gmail.com is gmail SMTP server, you can add your SMTP server if you own. The javax.net.ssl.SSLSocketFactory used for SSL support, mail.smtp.auth is to set authentication required by mail server for sending email.The PasswordAuthentication class is used to authenticate user name and password. The Session object used to store information like host,port,user name,password. The getDefaultInstance() method used to get session object.MimeMessage used to set subject ,message and add recipient .

The MimeBodyPart used to set text meaasge and add attachment .The MimeBodyPart object pass as parameter of  addBodyPart() method.The Mulitipart object is passed setContent() method argument  .The Transport class send() method is used to send MIME message

Now you can run the application and receive the mail.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Sending Email with Attachment Rating: 5 Reviewed By: eHowToNow