gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:06:41 31 4
gpt4 key购买 nike

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

我已联系我提供的电子邮件,他们怀疑这是 RFC 2231 中默认格式的 JAVa 邮件的格式问题,对于 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()));

这个类确实有电子邮件方法:包 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 来禁用使用 RFC 2231 对文件名进行编码。为“假”。

关于java - 从JAVA邮件发送到IMAP邮箱时附件文件名发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907178/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com