Java Mail (2)

Source: Internet
Author: User

/**
* Method Description: Send a message with an attachment
*
*@throws unsupportedencodingexception
*/
PublicStaticBoolean sendemailwithattachment (email)
Throws Unsupportedencodingexception {
Properties prop =New Properties ();
Prop.put ("Mail.smtp.host", mailserver);
Prop.put ("Mail.smtp.auth","true");
Authenticator auth =New Mailauthenticator ();
Session session = Session.getdefaultinstance (prop, auth);
Message message =New MimeMessage (session);
Boolean flag =False
try {
Message.setsubject (Email.getemail_subject ());Set Message subject
Message.setheader ("Header:", Email.getemail_header ());Set the message header
Message.setsentdate (New Date ());Set Send Time
Address Addressfrom =New InternetAddress (MailAddress, Mailaccount);Set Sender Address
Message.setfrom (Addressfrom);
BodyPart Messagebodypart =New MimeBodyPart ();
Messagebodypart.setcontent (Email.getemail_content (),"Text/html;charset=utf-8");
Multipart Multipart =New Mimemultipart ();
Multipart.addbodypart (Messagebodypart);
MimeBodyPart MimeBodyPart;
Multiple attachments
for (int i =0; I < Email.getemail_file (). Size (); i++) {
map<string, string> map = Email.getemail_file (). get (i);
String filesource="";
String filename="";
For (String Key:map.keySet ()) {
Filesource = key;
FileName = Map.get (key);
}
MimeBodyPart =New MimeBodyPart ();
DataSource Source =New Filedatasource (Filesource);
Mimebodypart.setdatahandler (New DataHandler (source));
Mimebodypart.setfilename (Mimeutility.encodetext (fileName));
Multipart.addbodypart (MimeBodyPart);Put parts in
}
Message.setcontent (multipart);

Set multiple recipient addresses
list<string> list = email.getemail_to ();
String toaddress = sendemail.getaddress (list);

internetaddress[] Internetaddressto =New InternetAddress (). Parse (toaddress);
Message.setrecipients (Message.RecipientType.TO, Internetaddressto);

Set up multiple CC addresses
String ccaddress = sendemail.getaddress (EMAIL.GETEMAIL_CC ());
internetaddress[] INTERNETADDRESSCC =New InternetAddress (). Parse (ccaddress);
Message.setrecipients (Message.RecipientType.CC, INTERNETADDRESSCC);

Message.savechanges ();
System.out.println ("Start sending mail ...");

Transport.send (message);
System.out.println ("Sent successfully!" ");
flag = true;
} catch (Messagingexception e) {
System.out.println ("Send failed! ");
flag = false;
e.printstacktrace ();

  }
return flag;
  }< /c4>

Java Mail (2)

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.