日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java mail 发送邮件 主题(标题)乱码

發(fā)布時間:2023/12/18 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java mail 发送邮件 主题(标题)乱码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近開發(fā)遇到Javamail? 發(fā)送郵件標(biāo)題亂碼問題,騰訊、網(wǎng)易郵箱不會亂碼,阿里郵箱? 標(biāo)題則會亂碼。解決辦法:

String subject = MimeUtility.encodeWord(mailEntity.getMailSubject(), "UTF-8", "Q");message.setSubject(subject);

發(fā)送郵件代碼:

/*** 發(fā)送郵件 可以帶附件 內(nèi)容可為HTML** @param mailEntity* @return* @throws GeneralSecurityException*/public static boolean sendMail(MailEntity mailEntity)throws GeneralSecurityException {// 發(fā)件人電子郵箱final String from = mailEntity.getMailFromAddress();// 發(fā)件人電子郵箱密碼final String pass = mailEntity.getMailFromPwd();// 指定發(fā)送郵件的主機(jī)為 smtp.qq.comString host = mailEntity.getMailFromHost(); // 郵件服務(wù)器 String port = mailEntity.getPort();// 獲取系統(tǒng)屬性Properties properties = System.getProperties();// 設(shè)置郵件服務(wù)器properties.setProperty("mail.smtp.host", host);properties.setProperty("mail.smtp.port",port);properties.put("mail.smtp.auth", "true");MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);properties.put("mail.smtp.ssl.enable", "false");properties.put("mail.smtp.ssl.socketFactory", sf);// 獲取默認(rèn)session對象Session session = Session.getDefaultInstance(properties, new Authenticator() {public PasswordAuthentication getPasswordAuthentication() { // qq郵箱服務(wù)器賬戶、第三方登錄授權(quán)碼return new PasswordAuthentication(from, pass); // 發(fā)件人郵件用戶名、密碼 }});try {// 創(chuàng)建默認(rèn)的 MimeMessage 對象MimeMessage message = new MimeMessage(session);// Set From: 頭部頭字段message.setFrom(new InternetAddress(from));// Set To: 頭部頭字段message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailEntity.getMailReceiverAddress()));// Set Subject: 主題文字String subject = MimeUtility.encodeWord(mailEntity.getMailSubject(), "UTF-8", "Q");message.setSubject(subject);// 創(chuàng)建消息部分BodyPart messageBodyPart = new MimeBodyPart();// 消息 // messageBodyPart.setText(mailEntity.getMailContent());messageBodyPart.setContent(mailEntity.getMailContent(), "text/html;charset=utf-8");// 創(chuàng)建多重消息Multipart multipart = new MimeMultipart();// 設(shè)置文本消息部分 multipart.addBodyPart(messageBodyPart);if (mailEntity.getFilePaths() != null && mailEntity.getFilePaths().size() > 0) {for (String filePath : mailEntity.getFilePaths()) {// 附件部分messageBodyPart = new MimeBodyPart();String suffix = "";String oldPath = "";if (filePath.indexOf(".") >0){suffix = filePath.substring(filePath.indexOf(".")+1);oldPath = filePath.substring(0,filePath.indexOf("."));DataSource source = new FileDataSource(oldPath);File file = ((FileDataSource) source).getFile();file.renameTo(new File(oldPath + "." + suffix));source = new FileDataSource(filePath);messageBodyPart.setDataHandler(new DataHandler(source));}else {// 設(shè)置要發(fā)送附件的文件路徑DataSource source = new FileDataSource(filePath);messageBodyPart.setDataHandler(new DataHandler(source));}// 設(shè)置要發(fā)送附件的文件路徑// messageBodyPart.setFileName(filename);// 處理附件名稱中文(附帶文件路徑)亂碼問題 messageBodyPart.setFileName(MimeUtility.encodeText(filePath));multipart.addBodyPart(messageBodyPart);}}// 發(fā)送完整消息 message.setContent(multipart);// 發(fā)送消息 Transport.send(message);logger.info("Sent mail successfully....");return true;} catch (MessagingException e) {logger.debug(e.getMessage());} catch (UnsupportedEncodingException e) {logger.debug(e.getMessage());}return false;}

實體:

/*** @Auther: mxf* @Date: 2018/10/31 16:08* @Description:發(fā)送郵件實體類*/ public class MailEntity implements Serializable {/*** 郵件接收地址*/private String mailReceiverAddress;/*** 郵件主題*/private String mailSubject;/*** 郵件內(nèi)容*/private String mailContent;/*** 附件路徑*/private List<String> filePaths = new ArrayList<String>();/*** 發(fā)送地址*/private String mailFromAddress;/*** 發(fā)送者的郵件密碼*/private String mailFromPwd;/*** 指定發(fā)送郵件的主機(jī)*/private String mailFromHost;/*** 發(fā)送郵件主機(jī)端口*/private String port;public String getPort() {return port;}public void setPort(String port) {this.port = port;}public String getMailReceiverAddress() {return mailReceiverAddress;}public void setMailReceiverAddress(String mailReceiverAddress) {this.mailReceiverAddress = mailReceiverAddress;}public String getMailSubject() {return mailSubject;}public void setMailSubject(String mailSubject) {this.mailSubject = mailSubject;}public String getMailContent() {return mailContent;}public void setMailContent(String mailContent) {this.mailContent = mailContent;}public List<String> getFilePaths() {return filePaths;}public void setFilePaths(List<String> filePaths) {this.filePaths = filePaths;}public String getMailFromAddress() {return mailFromAddress;}public void setMailFromAddress(String mailFromAddress) {this.mailFromAddress = mailFromAddress;}public String getMailFromPwd() {return mailFromPwd;}public void setMailFromPwd(String mailFromPwd) {this.mailFromPwd = mailFromPwd;}public String getMailFromHost() {return mailFromHost;}public void setMailFromHost(String mailFromHost) {this.mailFromHost = mailFromHost;} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/blog411032/p/10065693.html

總結(jié)

以上是生活随笔為你收集整理的Java mail 发送邮件 主题(标题)乱码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。