核心处理附件名称中文乱码
代码如下 | 复制代码 |
html.setFileName("=?GBK?B?" + enc.encode(file.getName().getBytes("GBK")) + "?="); |
即可了
邮件发送代码如下
代码如下 | 复制代码 |
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; /** * 邮件发送处理类 * @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 for(String toAddress : toAddressList) { sendHtmlMail(eventBean.getMailContent(), toAddress); } return false; } /** * Html格式发送邮件 * @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; } // 判断是否需要身份认证 Properties pro = senderInfo.getProperties(); //如果需要身份认证,则创建一个密码验证器 MyAuthenticator authenticator = new MyAuthenticator(fromAddress, senderInfo.getPassword()); // 根据邮件会话属性和密码验证器构造一个发送邮件的session Session sendMailSession = Session.getInstance(pro, authenticator); try { // 根据session创建一个邮件消息 Message mailMessage = new MimeMessage(sendMailSession); // 创建邮件发送者地址 Address from = new InternetAddress(senderInfo.getSenderMail()); // 设置邮件消息的发送者 mailMessage.setFrom(from); // 创建邮件的接收者地址,并设置到邮件消息中 Address to = new InternetAddress(toAddress); // Message.RecipientType.TO属性表示接收者的类型为TO mailMessage.setRecipient(Message.RecipientType.TO, to); // 设置邮件消息的主题 mailMessage.setSubject(senderInfo.getServerName()); // 设置邮件消息发送的时间 mailMessage.setSentDate(new Date()); // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 Multipart mainPart = new MimeMultipart(); // 创建一个包含HTML内容的MimeBodyPart BodyPart html = new MimeBodyPart(); // 设置HTML内容 html.setContent(content, "text/html; charset=utf-8"); mainPart.addBodyPart(html); // 将MiniMultipart对象设置为邮件内容 mailMessage.setContent(mainPart); // 发送邮件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } /** * Html格式发送邮件 * @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; } // 判断是否需要身份认证 Properties pro = senderInfo.getProperties(); //如果需要身份认证,则创建一个密码验证器 MyAuthenticator authenticator = new MyAuthenticator(fromAddress, senderInfo.getPassword()); // 根据邮件会话属性和密码验证器构造一个发送邮件的session Session sendMailSession = Session.getInstance(pro, authenticator); try { // 根据session创建一个邮件消息 Message mailMessage = new MimeMessage(sendMailSession); // 创建邮件发送者地址 Address from = new InternetAddress(senderInfo.getSenderMail()); // 设置邮件消息的发送者 mailMessage.setFrom(from); // 创建邮件的接收者地址,并设置到邮件消息中 Address to = new InternetAddress(toAddress); // Message.RecipientType.TO属性表示接收者的类型为TO mailMessage.setRecipient(Message.RecipientType.TO, to); // 设置邮件消息的主题 mailMessage.setSubject(senderInfo.getServerName()); // 设置邮件消息发送的时间 mailMessage.setSentDate(new Date()); // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 Multipart mainPart = new MimeMultipart(); // 创建一个包含HTML内容的MimeBodyPart MimeBodyPart html = new MimeBodyPart(); // 设置HTML内容 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); // 将MiniMultipart对象设置为邮件内容 mailMessage.setContent(mainPart); // 发送邮件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } } |
附件中文名称乱码处理的方法在代码。
代码如下 | 复制代码 |
html.setFileName("=?GBK?B?" + enc.encode(file.getName().getBytes("GBK")) + "?="); |
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔