从 JAVA 邮件发送到 IMAP 邮箱时,附件文件名发生了变化

Attachment file name get's changed when send to IMAP mail box from JAVA mail

我正在通过 JAVA 邮件发送带有 excel 附件的邮件。每当我将电子邮件发送到 IMAP 邮箱时,它的文件名都会更改为 Untitled attachment,与我传递给其他邮箱的文件名相反。

我已经联系了我提供的电子邮件,他们怀疑这是格式问题,因为 JAVa 邮件默认格式为 RFC 2231,对于 IMAP,它必须是 RFC 2047/2231。

注意:应用程序部署在 Widfly 9 上使用 JAVA 8.

我是这样调用方法的:

ByteArrayOutputStream baos = new ByteArrayOutputStream(excel_bytes.length);
baos.write(excel_bytes, 0, excel_bytes.length);

DataSource aAttachment = new  ByteArrayDataSource(baos.toByteArray(),"application/vnd.ms-excel");

String to_email = tenant.getEmail(); 
email.sendRechargeBillEmail(to_email, aAttachment, file_name + ".xlsx",tenant.getCompanyName(), String.valueOf(job.getStartDate()), String.valueOf(job.getEndDate()));

这是具有电子邮件方法的 class: 包裹 com.nextcontrols.rechargecalculation;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 javax.mail.internet.MimeUtility;

public class SendAttachmentInEmail {
    private static final Logger logger = 
    Logger.getLogger(SendAttachmentInEmail.class.getCanonicalName());
   public void sendEmail(String to_email, DataSource attachment, String 
     file_name, String from_email, String user_name, String password, String 
     email_body, String email_subject) {

      Properties props = new Properties();
      props.put("mail.smtp.host", "mail2.nextcontrols.local");
      Session session = Session.getDefaultInstance(props);

      try { 
         Message message = new MimeMessage(session);

         message.setFrom(new InternetAddress(from_email));

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

         message.setSubject(email_subject);

         MimeBodyPart messageBodyPart = new MimeBodyPart();

         messageBodyPart.setContent(email_body, "text/html; charset=utf-8");

         Multipart multipart = new MimeMultipart();

         multipart.addBodyPart(messageBodyPart);

         MimeBodyPart mailattachment = new MimeBodyPart();
          DataSource source = attachment;

         mailattachment.setFileName(file_name);
         mailattachment.setDataHandler(new DataHandler(source));
         mailattachment.setDisposition(MimeBodyPart.ATTACHMENT); 
         multipart.addBodyPart(mailattachment);
         message.setContent(multipart);

         Transport.send(message);
         logger.info("////// Sent message successfully to "+ to_email);

      } catch (MessagingException e) {
         throw new RuntimeException(e);
      }
   }

   public void sendRechargeBillEmail(String to_email, DataSource attachment, String file_name,String tenant_name, String start_date, String end_date){

       String subject = "Billing - " + tenant_name + " - " + start_date + " to " + end_date;
       String bodyText = "Please find attached your utility recharge bill for " + start_date + " to " + end_date;
       String note = "<i>This is an automated email, please do not reply to this address</i>";
       String fromText = "Regards,<br />-- <br />";
       String disclaimerTop = "Privacy and Confidentiality Notice";
       String disclaimerDown = " txt";

       bodyText = bodyText + "<br /> <br /> <br />" + note + "<br />" + fromText + "<br />" + addColor (disclaimerTop, Color.GRAY ,"2") + "<br />" + addColor(disclaimerDown, Color.GRAY, "1");

       sendEmail(to_email, attachment, file_name, "noreply@nxt.com", "Billing", "billing", bodyText, subject);
   }
   public static String addColor(String msg, Color color, String size) {
        String hexColor = String.format("#%06X",  (0xFFFFFF & color.getRGB()));
        String colorMsg = "<FONT size = \" "+ size +"\" COLOR=\"" + hexColor + "\">" + msg + "</FONT>";
        return colorMsg;
    }
}

如果您的 IMAP 服务器因为不理解 RFC 2231 而更改文件名,那么它确实需要更新。 RFC 2231 已有 20 年历史了。

您可以通过将 JavaMail 系统 属性 mail.mime.encodeparameters 设置为 "false".

来禁止使用 RFC 2231 对文件名进行编码