JavaMail mail topic garbled solution

xiaoxiao2021-03-06  60

Many friends have used JavaMail to send mail, and the garbled code in the body of the message is easy to solve. However, there is always a pile of garbled, no matter how the email topic is always displayed. What should I do? The mail topic in javamail needs to perform Base64 encoding,

Format, like: =? GB2312? B? XPQ1XMPCWUVS0B6T1TJWW6OSX U OB / STCFCVKOSSQLQ3RJEW9ZC66OH? =

So, use msg.setsubject ("Chinese topic"), or msg.setsubject ("Chinese Topics"), "8859_1"), "GB2312") will be garbled.

You need to encode the byte of the topic string to the base64 format before setting the email topic, and add the edger, the sample code is as follows:

Try {string Subject = "Chinese National HelloWorld"; String Content = "Test Mail language. Helloworld"; context ctx = new initialContext (); if (ctx == null) throw new exception ("no context ()") System.out.Println (CTX);

Javax.mail.Session mailsession = (javax.mail.session) CTX.lookup ("java: comp / env / mail / session"); system.out.println (Mailsession);

Message msg = new mimeMessage (Mailsession);

InternetDress [] toaddrs = Internetdress.Pars ("David.li@maxcard.com", false; msg.seTrecipients (message.recipienttype.to, toaddrs); // msg.setsubject ("=? GB2312? B?" Base64.encode (SUBJECT.GETBYTES ()) "? =");

// Translate Chinese into GB2312 encoded Subject = Stringutil.getstring (Subject, "GB2312"); Subject = new string (base64.encode ()); msg.setsubject ("=? GB2312? B " Subject "? = "); msg.setFrom (New Internet"); msg.settext (Stringutil.getstring (Content, "GB2312"); // Transport T = Mailsession.getTransport ("SMTP"); T.send (MSG); Out.println ("success"); // transport.send (msg);} catch (Exception E) {Out.println ("failed") System.out.println (E);

If there is any problem, please contact: webmaster@bcxy.com Source: http:// http://www.bcxy.com/java/cnmailsubject.htm

转载请注明原文地址:https://www.9cbs.com/read-113606.html

New Post(0)