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

Javamail doesn't send email with larger attachments

我想使用 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);
    }

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

我使用客户的私人邮件服务器发送电子邮件,它没有显示任何我能看到的错误(因为它是 public 邮件服务器)。然后我使用 Google 邮件服务器,这是 gmail 服务器给我的电子邮件帐户的回复:

Technical details of permanent failure: Google tried to deliver your message, but it was rejected by the server for the recipient domain mailinator.com by mail2.mailinator.com. [45.79.147.26].

The error that the other server returned was: 552 5.3.4 Message size exceeds fixed limit

这是因为域 mailinator.com 有一些邮件大小限制,所以它没有出现。然后我将东西发送到一个 gmail 电子邮件帐户并且它有效。

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

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