Core processing attachment name Chinese garbled
The code is as follows |
Copy Code |
Html.setfilename ("=? GBK? B "+ Enc.encode (File.getname (). GetBytes (" GBK ")) +"? = "); |
You can.
Mail send code as follows
The code is as follows |
Copy Code |
Package com.grt.michael.network.mail;
Import Java.io.File; Import java.io.IOException; Import Java.util.Date; Import java.util.List; Import java.util.Properties;
Import javax.mail.Address; Import Javax.mail.BodyPart; Import Javax.mail.Message; Import javax.mail.MessagingException; Import Javax.mail.Multipart; 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 Sun.misc.BASE64Encoder;
Import Com.grt.michael.bean.EventMessBean; Import Com.grt.michael.dao.INoticeDAO; Import Com.grt.michael.dao.impl.NoticeDAOImpl; Import Com.grt.michael.resource.CaptionUtil;
/** * Mail Send processing class * @author Administrator * */ public class Mailmanager {
private static Mailsenderinfo Senderinfo;
public static void Initialize () { Updatesenderinfo (); }
public static void Updatesenderinfo () { Inoticedao Noticedao = new Noticedaoimpl (); Senderinfo = Noticedao.getemailserver (); }
public static Boolean Sendeventmail (Eventmessbean eventbean) { Inoticedao Noticedao = new Noticedaoimpl (); list<string> toaddresslist = noticedao.geteventnoticeemailaddresses (Eventbean.getid ()); for (String toaddress:toaddresslist) { Sendhtmlmail (Eventbean.getmailcontent (), toaddress); } return false; }
/** * Send mail in HTML format * @param mailinfo * @return */ public static Boolean Sendhtmlmail (string content, String toaddress) { if (Senderinfo = = null) { return false; } String fromaddress = Senderinfo.getsendermail (); if (captionutil.isempty (content) | | Captionutil.isempty (fromaddress) | | Captionutil.isempty (toaddress)) { return false; } To determine whether an identity certificate is required Properties Pro = Senderinfo.getproperties (); If authentication is required, create a password validator Myauthenticator authenticator = new Myauthenticator (fromaddress, Senderinfo.getpassword ()); Constructs a session that sends a message based on the message conversation properties and the password validator Session sendmailsession = Session.getinstance (pro, authenticator); try { Create a mail message based on session Message MailMessage = new MimeMessage (sendmailsession); Create Mail Sender Address Address from = new InternetAddress (Senderinfo.getsendermail ()); Set the sender of a mail message Mailmessage.setfrom (from); Create the recipient address of the message and set it to the mail message Address to = new internetaddress (toaddress); The Message.RecipientType.TO property indicates that the recipient's type is to Mailmessage.setrecipient (Message.RecipientType.TO, to); Set the subject of a mail message Mailmessage.setsubject (Senderinfo.getservername ()); Set when mail messages are sent Mailmessage.setsentdate (New Date ()); The Minimultipart class is a container class that contains objects of type MimeBodyPart Multipart Mainpart = new Mimemultipart (); Create a mimebodypart that contains HTML content BodyPart html = new MimeBodyPart (); Set HTML content Html.setcontent (content, "text/html; Charset=utf-8 "); Mainpart.addbodypart (HTML); Set the Minimultipart object as the message content Mailmessage.setcontent (Mainpart); Send mail Transport.send (MailMessage); return true; catch (Messagingexception ex) { Ex.printstacktrace (); } return false; }
/** * Send mail in HTML format * @param mailinfo * @return */ public static Boolean Sendfilemail (String toaddress, file file) { if (Senderinfo = = null) { return false; } String fromaddress = Senderinfo.getsendermail (); if (Captionutil.isempty (fromaddress) | | Captionutil.isempty (toaddress)) { return false; } To determine whether an identity certificate is required Properties Pro = Senderinfo.getproperties (); If authentication is required, create a password validator Myauthenticator authenticator = new Myauthenticator (fromaddress, Senderinfo.getpassword ()); Constructs a session that sends a message based on the message conversation properties and the password validator Session sendmailsession = Session.getinstance (pro, authenticator); try { Create a mail message based on session Message MailMessage = new MimeMessage (sendmailsession); Create Mail Sender Address Address from = new InternetAddress (Senderinfo.getsendermail ()); Set the sender of a mail message Mailmessage.setfrom (from); Create the recipient address of the message and set it to the mail message Address to = new internetaddress (toaddress); The Message.RecipientType.TO property indicates that the recipient's type is to Mailmessage.setrecipient (Message.RecipientType.TO, to); Set the subject of a mail message Mailmessage.setsubject (Senderinfo.getservername ()); Set when mail messages are sent Mailmessage.setsentdate (New Date ()); The Minimultipart class is a container class that contains objects of type MimeBodyPart Multipart Mainpart = new Mimemultipart (); Create a mimebodypart that contains HTML content MimeBodyPart html = new MimeBodyPart (); Set HTML content try { Html.attachfile (file); Base64encoder enc = new Base64encoder (); html.setfilename ("=? GBK? B "+ Enc.encode (File.getname (). GetBytes (" GBK ")) +"? = "); catch (IOException e) { TODO auto-generated Catch block E.printstacktrace (); } Mainpart.addbodypart (HTML); Set the Minimultipart object as the message content Mailmessage.setcontent (Mainpart); Send mail Transport.send (MailMessage); return true; catch (Messagingexception ex) { Ex.printstacktrace (); } return false; }
} |
Attachment Chinese name garbled processing method in code.
The code is as follows |
Copy Code |
Html.setfilename ("=? GBK? B "+ Enc.encode (File.getname (). GetBytes (" GBK ")) +"? = "); |