Java Mail Send multi-point attachments

Source: Internet
Author: User

Package com.sungrow.sendmail;


Import Java.io.File;

Import java.util.ArrayList;

Import Java.util.Date;

Import java.util.List;

Import java.util.Properties;


Import Javax.activation.DataHandler;

Import Javax.activation.DataSource;

Import Javax.activation.FileDataSource;

Import Javax.mail.Authenticator;

Import Javax.mail.BodyPart;

Import Javax.mail.Message.RecipientType;

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;

Import javax.mail.internet.MimeUtility;


Import Org.slf4j.Logger;

Import Org.slf4j.LoggerFactory;


Import Com.sungrow.commons.ConfigDataService;

Import Com.sungrow.dao.entity.ReportMail;

Import com.sungrow.exception.InsightDBException;

public class SendMail {

/**

* Outbox [email protected]

*/

private static String from = NULL;

/**

* Outbox Password fmc123

*/

private static String PASSWORD = null;

private static Logger _log = Loggerfactory.getlogger ("Task");

/***

* Send mail with attachments (for multiple people)

*

* @param URL

* Attachment Road Strength

* @param title

* Email title

* @param content

* Email Content

* @param email Recipient Email

*/

public static Boolean Sendemailappendixlist () throws Insightdbexception {

Session session =getsession ();

MimeMessage message = new MimeMessage (session);

String str= "EMAIL1,EMAIL2,EMAIL3";//multi-score Mail Recipient

try {

@SuppressWarnings ("Static-access")

internetaddress[] Iatolistcs = new InternetAddress (). Parse (str);

Mimemultipart mm = new Mimemultipart ();

Message.setsubject ("Mail Header");

Message.setsentdate (New Date ());

Message.setfrom (New InternetAddress (from));

Message.setrecipient (recipienttype.to, new internetaddress (email));

Message.setrecipients (Recipienttype.cc,iatolistcs);//Send to multiple people

Set up message content

BodyPart body =null;

DataSource Ds=null;

List<string> url=new arraylist<string> ();//multi-sub-attachment of road strength

Url.add ("Attachment Road Strength");

for (int i=0;i<url.size (); i++) {

BodyPart Contentpart = new MimeBodyPart ();

Contentpart.settext ("Mail Content");

Mm.addbodypart (Contentpart);

Set up message Attachments

BODY = new MimeBodyPart ();

ds= New Filedatasource (New File (Url.get (i));

DataHandler dh = new DataHandler (DS);

Body.setdatahandler (DH);

Body.setfilename (Mimeutility.encodetext (Ds.getname (), "Utf-8", null));

Mm.addbodypart (body);

}

Message.setcontent (mm);

Send e-mail

Transport.send (message);

} catch (Exception e) {

E.printstacktrace ();

_log.error (E.getmessage ());

_log.error ("Send Mail failed ======" Sendemailappendixlist ");

return false;

}

return true;

}


/***

* Get session

*

* @return

*/

public static Session getsession () {

Configdataservice sd=configdataservice.getinstance ();

Map=daofactory.getreportdaoinstance (). Selectemail ();

FROM=SD.GETMAIL_ADDR ();//Sender Mailbox

PASSWORD=SD.GETMAIL_PSD ();//sender's mailbox password

Properties Props = new properties ();

Props.setproperty ("Mail.transport.protocol", Sd.getmail_protocol ());

Props.put ("Mail.smtp.localhost", "localhostadress");

Props.setproperty ("Mail.smtp.host", Sd.getmail_host ());

Props.setproperty ("Mail.smtp.port", Sd.getmail_port ());

Props.setproperty ("Mail.smtp.auth", "true");

Session session = Session.getinstance (props, new Authenticator () {

@Override

Protected Passwordauthentication getpasswordauthentication () {

return new Passwordauthentication (from, PASSWORD);

}

});

return session;

}

}


This article is from the "Carefree seeking defeat" blog, please make sure to keep this source http://6355601.blog.51cto.com/6345601/1564586

Java Mail Send multi-point attachments

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.