Java mail发送邮件乱码问题

Java可以实现发送邮件,依赖javax.mail包,最近在写用Java mail发送带附件的html格式邮件,工具类源码:

public class ReportSendHelper {

    /**
     * SMTP 服务器域名
     */
    private String host;
    /**
     * 用户名
     */
    private String userName;
    /**
     * 密码
     */
    private String password;
    /**
     * 发送人邮箱
     */
    private String from;
    /**
     * 收件人邮箱
     */
    private String to;
    /**
     * 邮件主题
     */
    private String subject;
    /**
     * html 格式正文的内容
     */
    private String htmlContent;
    /**
     * 附件
     */
    private String attachedFileName;

    public ReportSendHelper(String host, String userName, String password, String from) throws MessagingException {

        this.host = host;
        this.userName = userName;
        this.password = password;
        this.from = from;
    }

    public void send() throws Exception {

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", host);

        final String userName1 = userName;
        final String password1 = password;

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName1, password1);
                    }
                });

        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

        message.setSubject(subject);

        Multipart multipart = new MimeMultipart();

        if (htmlContent != null) {

            BodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(htmlContent, "text/html");
            multipart.addBodyPart(htmlPart);
        }

        if (attachedFileName != null) {

            BodyPart attachmentPart = new MimeBodyPart();
            DataSource source = new FileDataSource(attachedFileName);
            attachmentPart.setDataHandler(new DataHandler(source));
            attachmentPart.setFileName(attachedFileName);
            multipart.addBodyPart(attachmentPart);
        }

        message.setContent(multipart);
        Transport.send(message);
    }

    public void setTo(String to) {
        this.to = to;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setHtmlContent(String htmlContent) {
        this.htmlContent = htmlContent;
    }

    public void setAttachedFileName(String attachedFileName) {
        this.attachedFileName = attachedFileName;
    }

使用send方法发送邮件,邮件标题的中文可以正常显示,但邮件正文和附件名称中的中文以乱码显示,造成乱码的原因是编码问题,解决方法是:

为正文设置编码

htmlPart.setContent(htmlContent, "text/html;charset=gb2312");
使用javax.mail.internet.MimeUtility.encodeText()方法解决附件名称乱码问题

attachmentPart.setFileName(MimeUtility.encodeText(attachedFileName));



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值