gpt4 book ai didi

Javamail 不发送带有较大附件的电子邮件

转载 作者:行者123 更新时间:2023-11-30 01:02:31 27 4
gpt4 key购买 nike

我想使用 Javamail 发送一封带有 2 个附件的电子邮件。其中一个是json文件,另一个是txt文件(logcat.txt)。 logcat.txt 的大小约为 1mb。如果我的代码中有 addAttachment(multipart,reportPath,"logcat.txt");,它不会发送任何电子邮件。如果我删除 addAttachment(multipart,reportPath,"logcat.txt"); 它会起作用。当 json 文件变大时,大约 500kb,它也不会发送。
我的代码:

public synchronized void sendReport(String subject, String body, String filepath, String filename, String reportPath, String sender, String recipients){
try {
Multipart multipart = new MimeMultipart("mixed"); //try adding "mixed" here as well but it doesn't work
MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);

//body
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(body);

Log.d(TAG, "sendReport: " + reportPath);
//this prints sendReport: /storage/emulated/0/Android/data/**app package name**/files/LOG-1472631395.json
Log.d(TAG, "sendReport: " + filepath);
//sendReport: /storage/emulated/0/Android/data/**app package name**/files/logcat.txt
addAttachment(multipart,filepath,filename);
addAttachment(multipart,reportPath,"logcat.txt");
multipart.addBodyPart(messageBodyPart2);

message.setContent(multipart);

if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}

private static void addAttachment(Multipart multipart, String filepath, String filename) throws MessagingException
{
DataSource source = new FileDataSource(filepath);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
}

我也用另一种方法发送附件,但它也不起作用:

private static void addAttachment(Multipart multipart, String filepath, String filename) throws Exception
{
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.attachFile(filepath);
mimeBodyPart.setFileName(filename);
multipart.addBodyPart(mimeBodyPart);
}

有没有人知道如何解决这个问题?提前谢谢你。

最佳答案

    //In this list set the path from the different files you want to attach
String[] attachments;

Multipart multipart = new MimeMultipart();

//Add attachments
if(attachments != null && attachments.length > 0) {
for (String str : attachments) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(str);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
}
}

message.setContent(multipart);

我上传大文件没有问题,你可以试试这个代码。

关于Javamail 不发送带有较大附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39244895/

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