I am going to show you one simple Java program from which you can send email to multiple persons with multiple attachment,you can also add CC to your mail from this program,
I will give you one general program which consist multiple recipients ,Multiple CC recipients and multiple file attachment
You can change it according to your preferences.
package com.sendMail;
import java.util.Properties;
import javax.activation.DataHandler;
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.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class sendMailWithAttach {
private static String SMTP_HOST_NAME = "smtp.gmail.com";
private static String SMTP_PORT = "587";
private static String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
public static String sendMail(String recipients[],String
ccrecipients[], String subject,String message, String sentfrom,
String filepath1,String filepath2) throws MessagingException
{
boolean debug = true;
String sendStatus=null;
Properties props = new Properties();
props.put("mail.smtp.host",SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port",SMTP_PORT);
props.put("mail.oyazen.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.starttls.enable", "true");
// This block is used to create session event
Session session = Session.getDefaultInstance(props,new
javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new
PasswordAuthentication("yourEmailId","password");
}
});
// If you want to enable debug then set a session debug true,
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(sentfrom);
msg.setFrom(addressFrom);
InternetAddress[] addressCC = new
InternetAddress[ccrecipients.length];
for (int i = 0; i < ccrecipients.length; i++)
{
addressCC[i] = new InternetAddress(ccrecipients[i]);
}
msg.setRecipients(Message.RecipientType.CC,addressCC);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
BodyPart messageBodyPart1 = new MimeBodyPart();
BodyPart messageBodyPart2 = new MimeBodyPart();
BodyPart messageBodyPart3 = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(message, "text/html");
multipart.addBodyPart(htmlPart);
//Creating config for two file
FileDataSource fileDataSource1 =new FileDataSource(filepath1);
messageBodyPart1.setDataHandler(new
DataHandler(fileDataSource1));
messageBodyPart1.setFileName("FileFirst");
multipart.addBodyPart(messageBodyPart1);
FileDataSource fileDataSource2 =new FileDataSource(filepath2);
messageBodyPart2.setDataHandler(new
DataHandler(fileDataSource2));
messageBodyPart2.setFileName("FileSecond");
multipart.addBodyPart(messageBodyPart2);
msg.setContent(multipart);
try {
Transport.send(msg);
sendStatus="Mail Succes";
}
catch(Exception e) {
e.printStackTrace();
}
return sendStatus;
}
/**
* Main method
* */
public static void main(String args[])
{
String recipient[]={"abc@gmail.com","xyz@gmail.com"};
String CCrecipient[]={"CCabc@gmail.com","CCxyz@gmail.com"};
String sentfrom="FromMe@gmail.com";
String subject="Testing";
String message="hello PFA";
String filepath1="/home/amit/Key.txt";
String filepath2="/home/amit/Key.txt";
try {
String result=sendMail (
recipient,CCrecipient,subject,message,sentfrom,filepath1,filepath2);
}
catch (MessagingException e) {
e.printStackTrace();
}
}
}
Please let me know if you face any issue while running this example
No comments:
Post a Comment