.zip文件在java中发送电子邮件时被转换成.bin文件 [英] .zip file gets converted to .bin file while sending the email in java

查看:777
本文介绍了.zip文件在java中发送电子邮件时被转换成.bin文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 zip文件作为附件发送邮件,我可以发送电子邮件,但zip文件的附件被转换为某些 .bin 文件。
我需要设置一些属性吗?
为什么zip文件被转换成.bin文件?

I am trying to send mail using below snippet with the zip file as attachment,I am able to send the email but the attachment which is zip file gets converted to some .bin file. Do I need to set some properties? Why zip file gets converted to .bin file?

Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "smtp");
            props.setProperty("mail.host", "smtp.gmail.com");

            Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(true);
            Transport transport = mailSession.getTransport();

            MimeMessage message = new MimeMessage(mailSession);
            message.setSubject("HTML  mail with images");
            message.setFrom(new InternetAddress("b@gmail.com"));
            message.addRecipient(Message.RecipientType.TO,
                 new InternetAddress("a@gmail.com"));
            MimeMultipart multipart = new MimeMultipart("related");

            // first part  (the html)
            BodyPart messageBodyPart = new MimeBodyPart();
            String htmlText = "PFA Query Output.";
            messageBodyPart.setContent(htmlText, "text/html");

            // add it
            multipart.addBodyPart(messageBodyPart);

            // second part (the image)
            messageBodyPart = new MimeBodyPart();
            DataSource fds = new FileDataSource(zipFilePath);
            messageBodyPart.setDataHandler(new DataHandler(fds));
            messageBodyPart.setHeader("Content-ID","<image>");

            // add it
            multipart.addBodyPart(messageBodyPart);

            // put everything together
            message.setContent(multipart);

            transport.connect();
            transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
            transport.close();


推荐答案

删除 messageBodyPart.setHeader Content-ID,< image>)如果您没有发送图像。

Remove messageBodyPart.setHeader("Content-ID","<image>") if you're not sending image.

添加以下语句集附件文件名,

Add follwing statement set attachment filename,

  messageBodyPart.setFileName(zipFilePath);

这篇关于.zip文件在java中发送电子邮件时被转换成.bin文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆