Below is the java code which shows the way to send mail with multiple CC addresses. Also it validated the emailID of the sender.
Also it has the code to retrieve a file from FTP and send as attachment.
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.SendFailedException;
import javax.mail.Transport;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.regex.*;
public int SendMail(String subject, String messageText,String TO,String cc,ArrayList arr)
throws MessagingException
{
URL url=null;
String username = "";
String password = "";
String mail_transport_protocol = "";
String mail_smtp_port = "";
String mail_smtp_host = "";
String mail_from="";
try
{
username = <SMTP USERNAME>;
password = <SMTP PASSWORD>;
mail_transport_protocol = smtp;
mail_smtp_port = <PORT>;
mail_smtp_host = <SMTP HOST NAME>;
mail_from=<FROM MAILID>;
Properties props = new Properties();
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", 587);
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.host",smtp.gmail.com);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// props.put("mail.store.protocol", "pop3");
// props.put("mail.transport.protocol", "smtp");
// props.put("mail.debug", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setContent(messageText, mail_messageText);
message.setSubject(subject);
String toarray[] = TO.split(",");
Pattern p=Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
for(int i = 0; i < toarray.length; i++)
{
InternetAddress fromAddress = new InternetAddress(mail_from);
System.out.println("fromAddress"+fromAddress);
message.setFrom(fromAddress);
System.out.println((new StringBuilder("tomail=")).append(toarray[i]).toString());
//VALIDATING EMAILID
Matcher g=p.matcher(toarray[i]);
boolean b=g.matches();
if(b==true)
{
System.out.println("EMAILID:"+toarray[i]);
InternetAddress toAddress = new InternetAddress(toarray[i]);
message.setRecipient(javax.mail.Message.RecipientType.TO, toAddress);
/* CC MAIL */
String toarraycc[] = cc.split(",");
ArrayList ccemailidarr=new ArrayList();
for(int l = 0; l < toarraycc.length; l++)
{
System.out.println((new StringBuilder("ccmail=")).append(toarraycc[l]).toString());
//VALIDATING TO EMAILID
//Validating HR MAIL ID
Matcher v=p.matcher(toarraycc[l]);
//Matcher m=p.matcher(args[0]);
boolean h=v.matches();
if(h==true)
{
System.out.println("CC EMAILID:"+toarraycc[l]);
//END
StringTokenizer st = new StringTokenizer(toarraycc[l],",");
while (st.hasMoreTokens()) {
ccemailidarr.add(st.nextToken());
}//while
}//if h
}//for CC
int sizecc = ccemailidarr.size();
System.out.println("SIZE CC"+sizecc);
InternetAddress ccAddress[] = new InternetAddress[sizecc];
for(int m = 0; m < sizecc; m++)
ccAddress[m] = new InternetAddress(ccemailidarr.get(m).toString());
message.setRecipients(javax.mail.Message.RecipientType.CC, ccAddress);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messageText, mail_messageText);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
// DataSource source = new FileDataSource(attachementPath);
//
url = new URL("ftp://"+FTPUName+":"+FTPPwd+"@"+FTPHOST+"/FOLDER/"+attachementname);
URLDataSource source = new URLDataSource(url);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(displayfilenme);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
}//if b is true
}//for
}try
catch(Exception e)
{
System.out.println((new StringBuilder("Error in sending mail:")).append(e).toString());
}//catch
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
No comments:
Post a Comment