Java发送邮件附件名称中文乱码问题

作者:袖梨 2022-06-29

核心处理附件名称中文乱码

 代码如下 复制代码

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 toAddressList = noticeDao.getEventNoticeEmailAddresses(eventBean.getId());
        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")) + "?=");

相关文章

精彩推荐