JSP Mail Mass Program code

Source: Internet
Author: User
Tags getmessage

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.