Archives for 

smtp

This category is for SMTP. Here we are discussing the examples with SMTP.

Sending email with attachment in Java

We already discussed the fundamentals of Java Mail API. SMTP can be used to send and receive emails.In the just previous chapter we discussed  how SMTP is using to send emails.  In this chapter we are  discussing how we can send an email with attachment .The application sends mail from gmail account.

Sending email with attachment in Java

Create a dynamic web project and create a java  class SendAttachmentSample.java.The code is shown below.(If we need to access mails from J2SE application , then we can download Java Mail API  from  here.Extract the zip file and include the jar files  in the project path)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendAttachmentSample {
private Properties properties = null;
private Session session = null;
private Message message = null;
String fromId;
String toId;
String filePath = "";//provide the attachment file with path
String configLocation = "";// provide the config path
String outputFileName="";//provide output file name
private BodyPart bodyPart = null;
private DataSource source = null;
private Multipart multiPart = null;

public SendAttachmentSample() {

}

public void sendMail() {
properties = new Properties();
try {
properties.load(new FileInputStream(new File(configLocation + "/"
+ "mailconfig.properties")));
fromId = properties.getProperty("fromAddress");
toId = properties.getProperty("toAddress");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {

e1.printStackTrace();
}
session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(properties
.getProperty("username"), properties
.getProperty("password"));
}
});
message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromId));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toId));
message.setSubject("Sample mail with attachment");
message.setText("Hello" + "This is a message ...See attachment");
bodyPart = new MimeBodyPart();
source = new FileDataSource(new File(filePath));
multiPart = new MimeMultipart();
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(outputFileName);
multiPart.addBodyPart(bodyPart);
message.setContent(multiPart);

Transport.send(message);
System.out.println("Sent mail with attachment..");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Send mail with attachment");
}

public static void main(String[] args) {
SendAttachmentSample sample = new SendAttachmentSample();
sample.sendMail();
}

}

mailconfig.properties

# This file defines the various attributes like login credentials and mail ids
username=
password=
fromAddress=
toAddress=
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587

In this application a BodyPart oject is created  and the file source is added to it.Then it is added to a MultiPart object. The MultiPart is adding as content  of  Message.

Output

Provide the  username ,password,fromAddress,toAddress in mailcomfig.properties.Also provide filePath,configLocation and outputFileName in java file .Comile and run the application.It may take few minutes to completly send the mail depending on the file size.

See also:

Java Mail API overview

Deleting email example

SMTP

Sending mail in java example

POP

Reading email using POP

IMAP

Reading email using IMAP