Sending Email from Java Application (Email and Attachement )

This tutorial is composed of two parts

1- Sending Simple Email from a Java Application
2- Sending Email with Attachment from a Java Application

You can find alot of help on the web so lets start coding straight away.

Part 1

Sending Simple Email from a Java Application


we will create two java files, although it is not compulsory. One will contain the business logic and the other will contain the main function.

You need to have Java Mail API for this code to run. But don't worry, I have uploaded both the part separately and they contain all the required libraries. So download the code and execute it as it is.

Download part 1 code and required libraries from here.

SendMail.java

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

private String from;
private String to;
private String subject;
private String text;

public SendMail(String from, String to, String subject, String text){
this.from = from;
this.to = to;
this.subject = subject;
this.text = text;
}

public void send(){

Properties props = new Properties();
props.put("mail.smtp.host", "203.215.163.135");
//props.put("mail.smtp.port", "465");

Session mailSession = Session.getDefaultInstance(props);
Message simpleMessage = new MimeMessage(mailSession);

InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {
e.printStackTrace();
}

try {
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);

Transport.send(simpleMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}

Here is the file that will be used to test it. Change the from and to email address and all the other things as per your requirements.

SendMailTest.java

public class SendMailTest {

public static void main(String[] args) {

String from = "ali.hammad@d-p-s.com";
String to = "ali.hammad@d-p-s.com";
String subject = "Test";
String message = "A test message";

SendMail sendMail = new SendMail(from, to , subject, message);
sendMail.send();
}
}


Part 2


Sending Email with Attachment from a Java Application

This part will contain almost the same code as above but with this code you can attach a file with the email.

We have again two files with the same names as above. The file that is going to be attached with the email can be placed anywhere. But in my case, the file name is somefile.txt and it is placed in C drive. So the path mentioned in the SendMailTest.java is c:/somefile.txt

The libraries required to send email are JavaMail API and Java Activation Framework. But you can download the code along with libraries from here.


SendMail.java

import java.util.Properties;

import javax.mail.*;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

import javax.activation.*;

public class SendMail {

private String from;
private String to;
private String subject;
private String text;
private String attachments;

public SendMail(String from, String to, String subject, String text,String attachments){
this.from = from;
this.to = to;
this.subject = subject;
this.text = text;
this.attachments = attachments;
}

public void send(){

Properties props = new Properties();
props.put("mail.smtp.host", "203.215.163.135");
//props.put("mail.smtp.port", "465");

Session mailSession = Session.getDefaultInstance(props);
Message simpleMessage = new MimeMessage(mailSession);

InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {
e.printStackTrace();
}

try {
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);

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

//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);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachments);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachments);
multipart.addBodyPart(messageBodyPart);

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

Transport.send(simpleMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}

}



SendMailTest.java

Change the emails and filename as per your requiremements.

public class SendMailTest {

public static void main(String[] args) {

String from = "ali.hammad@d-p-s.com";
String to = "ali.hammad@d-p-s.com";
String subject = "Test";
String message = "A test message";
String filename ="c:/somefile.txt";

SendMail sendMail = new SendMail(from, to , subject, message,filename);
sendMail.send();
}
}



Downloads

Download Part 1 Code and Libraries
Download Part 2 Code and Libraries



0 comments for this post

Post a Comment

Blog Widget by LinkWithin