从JAVA邮件

时间:2018-06-18 10:14:10

标签: java javamail wildfly-9

我通过JAVA邮件发送带有Excel附件的邮件。每当我将电子邮件发送到IMAP电子邮箱时,它的文件名都会更改为无标题附件,因为我正在传递其工作文件的其他电子邮箱名称。

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

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

这就是我称之为方法的方法:

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()));

这是具有电子邮件方法的类: package 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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

您可以通过将JavaMail系统属性mail.mime.encodeparameters设置为“false”来禁用RFC 2231来对文件名进行编码。