/**
* 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)