JSP Mail Mass Program code
public static Boolean SendMail (String subject,string body,string to,string content,address[] validunsentaddresses, Boolean isresend) {
Logger.info ("Originating Send mail *");
Boolean result=false;
try {
Security.addprovider (New Com.sun.net.ssl.internal.ssl.Provider ());
Get a Properties Object
Properties props = System.getproperties ();
Props.setproperty ("Mail.smtp.host", stmt_server);
Props.setproperty ("Mail.transport.protocol", "SMTP");
Props.setproperty ("Mail.smtp.socketFactory.class", ssl_factory);
Props.setproperty ("Mail.smtp.socketFactory.fallback", "false");
Props.setproperty ("Mail.smtp.port", port);
Props.setproperty ("Mail.smtp.socketFactory.port", port);
Props.put ("Mail.smtp.auth", "true");
Final String username = Username;
Final String password = password;
Session session = Session.getdefaultinstance (props,
New Authenticator () {
Protected Passwordauthentication getpasswordauthentication () {
Return to New Passwordauthentication (username,
password);
}
});
Session.setdebug (TRUE);
Transport transport = Session.gettransport ("SMTP");
msg = new MimeMessage (session);
if (validunsentaddresses==null) {
List list=new ArrayList ();;
String[] Gods=to.split (",");
for (int i = 0; i < gods.length; i++) {
try {
if (gods[i]!=null &&! "". Equals (Gods[i]) &&sendmailmanager.checkemail (Gods[i])) {
List.add (New InternetAddress (gods[i));
}
catch (Addressexception e) {
Logger.error (SendMailManager.class.getName () + ":" +e.getmessage ());
E.printstacktrace ();
}
}
// ----------
Internetaddress[] address = (internetaddress[]) List.toarray (new Internetaddress[list.size ()));
Msg.setrecipients (Message.RecipientType.BCC, address);
}else{
Msg.setrecipients (Message.RecipientType.BCC, validunsentaddresses);
}
Msg.setfrom (New InternetAddress (User_email));
Msg.setsubject (subject);
Msg.setsentdate (New Date ());
if (body!=null&&! "". Equals (body)) {
Mimemultipart MP = new Mimemultipart ();
MimeBodyPart bp = new MimeBodyPart ();
Bp.setcontent (Body, "text/html;charset=utf-8");
Mp.addbodypart (BP);
Msg.setcontent (MP);
}else if (! "". Equals (content)) {
Msg.settext (content);
}
Msg.savechanges ();
Transport.connect ((String) props.get ("Mail.smtp.host"), user_email, password);
Transport.sendmessage (msg, msg.getrecipients (Message.RecipientType.BCC));
Transport.close ();
Result=true;
System.out.println ("Mail has been sent successfully!");
catch (Sendfailedexception e) {
try {
Thread.CurrentThread (). Sleep (10000);
catch (Interruptedexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
if (e.getvalidunsentaddresses ()!=null && isresend) {
SendMail (Subject,body, "", Content,e.getvalidunsentaddresses (), false);
}
}catch (Messagingexception e) {
Result=false;
System.out.println ("Mail has failed!");
Logger.error (SendMailManager.class.getName () + ":" +e.getmessage ());
E.printstacktrace ();
}finally{
System.out.println (New Date (). toLocaleString () + "Connect SMTP once");
}
return result;
}
External JS Calling method
SendMail (subject,body,to,content,null,true);