Sending mail with attachment in JAVA

Sending mail with attachment in JAVA

The JavaMail API is a set of abstract APIs that model a mail system. The API provides a platform independent and protocol independent framework to build Java technology based email client applications. The JavaMail API provides facilities for reading and sending email. The package defines classes that are specific to mail systems based on internet standards such as MIME, SMTP, POP3, and IMAP.
Jar files required:

1. mail.jar that you can download here

Java Source code Sending Mail


package mail;

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class sendMail {


static String username = "abcdefgh"; //replace this with a valid username
static String password = "password"; //replace this with a valid password

public static void main(String[] args)throws Exception{

try{


String host = "mail.xyz.com"; //replace this with a valid host
int smtpPort = 000; //replace this with a valid port
String from = "testmail@mail.com"; //replace this with a valid email id
String to = "findmail@mail.com"; //replace this with a valid email id
String fileAttachment1 = "D://file1.txt"; // file to attach
String fileAttachment2 = "D://file2.pdf"; // file to attach

Properties props = System.getProperties();
props.put("mail.smtp.host", host); // Setup mail server
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", smtpPort);
Session session = Session.getDefaultInstance(props); // Get session
MimeMessage message = new MimeMessage(session); // Define message
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Find Attachment"); // create the subject
MimeBodyPart messageBodyPart = new MimeBodyPart(); //fill message
messageBodyPart.setText("Welcome to JAVA Family"); // create the message part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart); // Part two is attachment

messageBodyPart = new MimeBodyPart();
DataSource source1 = new FileDataSource(fileAttachment1);
messageBodyPart.setDataHandler(new DataHandler(source1));
messageBodyPart.setFileName(fileAttachment1.substring(4, fileAttachment1.length()));
multipart.addBodyPart(messageBodyPart);

messageBodyPart = new MimeBodyPart();
DataSource source2 = new FileDataSource(fileAttachment2);
messageBodyPart.setDataHandler(new DataHandler(source2));
messageBodyPart.setFileName(fileAttachment2.substring(4, fileAttachment2.length()));
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}

0 comments:

Post a Comment