0

我正在使用 javamail api 开发我的 android 应用程序以发送带有附件的邮件。但是,我发现如果附件是多部分的最后一条记录,则邮件无法传递到目的地。我不知道为什么会发生这种情况,我在那里搜索没有类似的问题讨论这个问题。所以我打开这个问题,想在这里寻求专家的回答。

我发送邮件的代码段:

public boolean send(String[] toAddress, String fromAddress, String subject,
                    String body, String filename) throws Exception { 
    Properties props = setProperties(); 

    try{ 
        Session session = Session.getInstance(props, this); 
        session.setDebug(true); 

        MimeMessage msg = new MimeMessage(session); 

        msg.setFrom(new InternetAddress(fromAddress)); 

        InternetAddress[] addressTo = new InternetAddress[toAddress.length]; 
        for(int i=0; i<toAddress.length; i++){ 
            addressTo[i] = new InternetAddress(toAddress[i]); 
        } 

        msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 
        msg.setSubject(subject); 
        msg.setSentDate(new Date()); 


        BodyPart messageBodyPart1 = new MimeBodyPart(); 
        messageBodyPart1.setText(body); 
        multi.addBodyPart(messageBodyPart1); 

        BodyPart messageBodyPart2 = new MimeBodyPart(); 
        DataSource source = new FileDataSource(filename); 
        messageBodyPart2.setDataHandler(new DataHandler(source)); 
        messageBodyPart2.setFileName("record.csv"); 
        multi.addBodyPart(messageBodyPart2);

        BodyPart messageBodyPart3 = new MimeBodyPart(); 
        messageBodyPart3.setText(""); 
        multi.addBodyPart(messageBodyPart3);

        msg.setContent(multi); 

        Transport transport = session.getTransport("smtp"); 
        transport.connect(host, 25, user, password); 
        transport.sendMessage(msg, msg.getAllRecipients()); 
        transport.close(); 
        return true; 
    } catch (Exception e) { 
        e.printStackTrace(); 
        return false; 
    } 
} 

日志消息显示:

没有bodypart3:

08-16 15:09:33.337: I/System.out(6314): EHLO localhost
08-16 15:09:33.345: I/System.out(6314): 250-web.mysmtp.com Hello localhost [202.155.209.250]
08-16 15:09:33.345: I/System.out(6314): 250-SIZE 52428800
08-16 15:09:33.345: I/System.out(6314): 250-PIPELINING
08-16 15:09:33.345: I/System.out(6314): 250-AUTH PLAIN LOGIN
08-16 15:09:33.345: I/System.out(6314): 250-STARTTLS
08-16 15:09:33.345: I/System.out(6314): 250 HELP
08-16 15:09:33.345: I/System.out(6314): DEBUG SMTP: Found extension "SIZE", arg "52428800"
08-16 15:09:33.345: I/System.out(6314): DEBUG SMTP: Found extension "PIPELINING", arg ""
08-16 15:09:33.345: I/System.out(6314): DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
08-16 15:09:33.345: I/System.out(6314): DEBUG SMTP: Found extension "STARTTLS", arg ""
08-16 15:09:33.345: I/System.out(6314): DEBUG SMTP: Found extension "HELP", arg ""
08-16 15:09:33.353: I/System.out(6314): DEBUG SMTP: Attempt to authenticate
08-16 15:09:33.353: I/System.out(6314): AUTH LOGIN
08-16 15:09:33.361: I/System.out(6314): 334 VXNlcm5hbWU6
08-16 15:09:33.361: I/System.out(6314): Y2xpZW50YWxlcnRzQHVuaXNlcnZlaXQuY29t
08-16 15:09:33.368: I/System.out(6314): 334 UGFzc3dvcmQ6
08-16 15:09:33.368: I/System.out(6314): cEBzc3cwcmQhIUA=
08-16 15:09:33.384: I/System.out(6314): 235 Authentication succeeded
08-16 15:09:33.392: I/System.out(6314): DEBUG SMTP: use8bit false
08-16 15:09:33.392: I/System.out(6314): MAIL FROM:<me@mysmtp.com>
08-16 15:09:33.415: I/System.out(6314): 250 OK
08-16 15:09:33.415: I/System.out(6314): RCPT TO:<you@mysmtp.com>
08-16 15:09:33.470: I/System.out(6314): 250 Accepted
08-16 15:09:33.470: I/System.out(6314): RCPT TO:<admin@mysmtp.com>
08-16 15:09:33.478: I/System.out(6314): 250 Accepted
08-16 15:09:33.478: I/System.out(6314): DEBUG SMTP: Verified Addresses
08-16 15:09:33.478: I/System.out(6314): DEBUG SMTP:   you@mysmtp.com
08-16 15:09:33.478: I/System.out(6314): DEBUG SMTP:   admin@mysmtp.com
08-16 15:09:33.478: I/System.out(6314): DATA
08-16 15:09:33.486: I/System.out(6314): 354 Enter message, ending with "." on a line by itself
08-16 15:09:33.564: I/System.out(6314): Date: Thu, 16 Aug 2012 15:09:30 +0800 (HKT)
08-16 15:09:33.564: I/System.out(6314): From: me@mysmtp.com
08-16 15:09:33.564: I/System.out(6314): To: you@mysmtp.com, admin@mysmtp.com
08-16 15:09:33.564: I/System.out(6314): Message-ID: <1099216984.1.1345100973553.JavaMail.javamailuser@localhost>
08-16 15:09:33.564: I/System.out(6314): Subject: Record cannot be inserted!
08-16 15:09:33.564: I/System.out(6314): MIME-Version: 1.0
08-16 15:09:33.564: I/System.out(6314): Content-Type: multipart/mixed; 
08-16 15:09:33.564: I/System.out(6314):     boundary="----=_Part_0_1098789192.1345100970404"
08-16 15:09:33.564: I/System.out(6314): 
08-16 15:09:33.571: I/System.out(6314): ------=_Part_0_1098789192.1345100970404
08-16 15:09:33.571: I/System.out(6314): Content-Type: text/plain; charset=us-ascii
08-16 15:09:33.571: I/System.out(6314): Content-Transfer-Encoding: 7bit
08-16 15:09:33.571: I/System.out(6314): 
08-16 15:09:33.571: I/System.out(6314): Please insert manually!
08-16 15:09:33.571: I/System.out(6314): ------=_Part_0_1098789192.1345100970404
08-16 15:09:33.571: I/System.out(6314): Content-Type: application/octet-stream; name=record.csv
08-16 15:09:33.571: I/System.out(6314): Content-Transfer-Encoding: 7bit
08-16 15:09:33.571: I/System.out(6314): Content-Disposition: attachment; filename=record.csv
08-16 15:09:33.571: I/System.out(6314): 
08-16 15:09:33.571: I/System.out(6314): ABC,HK,A,2012/08/16 10:02, ,
08-16 15:09:33.571: I/System.out(6314): 
08-16 15:09:33.571: I/System.out(6314): ------=_Part_0_1098789192.1345100970404--
08-16 15:09:33.571: I/System.out(6314): .
08-16 15:09:33.611: I/System.out(6314): 250 OK id=1T1uCu-0007Fx-Tu
08-16 15:09:33.611: I/System.out(6314): QUIT
08-16 15:09:33.618: I/System.out(6314): 221 web.mysmtp.com closing connection

有bodypart3:

08-16 15:12:34.353: I/System.out(6876): EHLO localhost
08-16 15:12:34.361: I/System.out(6876): 250-web.mysmtp.com Hello localhost [202.155.209.250]
08-16 15:12:34.361: I/System.out(6876): 250-SIZE 52428800
08-16 15:12:34.361: I/System.out(6876): 250-PIPELINING
08-16 15:12:34.361: I/System.out(6876): 250-AUTH PLAIN LOGIN
08-16 15:12:34.361: I/System.out(6876): 250-STARTTLS
08-16 15:12:34.361: I/System.out(6876): 250 HELP
08-16 15:12:34.368: I/System.out(6876): DEBUG SMTP: Found extension "SIZE", arg "52428800"
08-16 15:12:34.368: I/System.out(6876): DEBUG SMTP: Found extension "PIPELINING", arg ""
08-16 15:12:34.368: I/System.out(6876): DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
08-16 15:12:34.368: I/System.out(6876): DEBUG SMTP: Found extension "STARTTLS", arg ""
08-16 15:12:34.368: I/System.out(6876): DEBUG SMTP: Found extension "HELP", arg ""
08-16 15:12:34.368: I/System.out(6876): DEBUG SMTP: Attempt to authenticate
08-16 15:12:34.368: I/System.out(6876): AUTH LOGIN
08-16 15:12:34.384: I/System.out(6876): 334 VXNlcm5hbWU6
08-16 15:12:34.384: I/System.out(6876): Y2xpZW50YWxlcnRzQHVuaXNlcnZlaXQuY29t
08-16 15:12:34.392: I/System.out(6876): 334 UGFzc3dvcmQ6
08-16 15:12:34.392: I/System.out(6876): cEBzc3cwcmQhIUA=
08-16 15:12:34.400: I/System.out(6876): 235 Authentication succeeded
08-16 15:12:34.415: I/System.out(6876): DEBUG SMTP: use8bit false
08-16 15:12:34.415: I/System.out(6876): MAIL FROM:<me@mysmtp.com>
08-16 15:12:34.431: I/System.out(6876): 250 OK
08-16 15:12:34.431: I/System.out(6876): RCPT TO:<you@mysmtp.com>
08-16 15:12:34.470: I/System.out(6876): 250 Accepted
08-16 15:12:34.470: I/System.out(6876): RCPT TO:<admin@mysmtp.com>
08-16 15:12:34.486: I/System.out(6876): 250 Accepted
08-16 15:12:34.486: I/System.out(6876): DEBUG SMTP: Verified Addresses
08-16 15:12:34.486: I/System.out(6876): DEBUG SMTP:   you@mysmtp.com
08-16 15:12:34.486: I/System.out(6876): DEBUG SMTP:   admin@mysmtp.com
08-16 15:12:34.486: I/System.out(6876): DATA
08-16 15:12:34.493: I/System.out(6876): 354 Enter message, ending with "." on a line by itself
08-16 15:12:34.571: I/System.out(6876): Date: Thu, 16 Aug 2012 15:12:32 +0800 (HKT)
08-16 15:12:34.571: I/System.out(6876): From: me@mysmtp.com
08-16 15:12:34.571: I/System.out(6876): To: you@mysmtp.com, admin@mysmtp.com
08-16 15:12:34.571: I/System.out(6876): Message-ID: <1099235248.1.1345101154551.JavaMail.javamailuser@localhost>
08-16 15:12:34.571: I/System.out(6876): Subject: Record cannot be inserted!
08-16 15:12:34.571: I/System.out(6876): MIME-Version: 1.0
08-16 15:12:34.571: I/System.out(6876): Content-Type: multipart/mixed; 
08-16 15:12:34.571: I/System.out(6876):     boundary="----=_Part_0_1098789960.1345101152034"
08-16 15:12:34.571: I/System.out(6876): 
08-16 15:12:34.571: I/System.out(6876): ------=_Part_0_1098789960.1345101152034
08-16 15:12:34.571: I/System.out(6876): Content-Type: text/plain; charset=us-ascii
08-16 15:12:34.571: I/System.out(6876): Content-Transfer-Encoding: 7bit
08-16 15:12:34.571: I/System.out(6876): 
08-16 15:12:34.571: I/System.out(6876): Please insert manually!
08-16 15:12:34.571: I/System.out(6876): ------=_Part_0_1098789960.1345101152034
08-16 15:12:34.571: I/System.out(6876): Content-Type: application/octet-stream; name=record.csv
08-16 15:12:34.571: I/System.out(6876): Content-Transfer-Encoding: 7bit
08-16 15:12:34.571: I/System.out(6876): Content-Disposition: attachment; filename=record.csv
08-16 15:12:34.571: I/System.out(6876): 
08-16 15:12:34.571: I/System.out(6876): ABC,HK,A,2012/08/16 10:02, ,
08-16 15:12:34.571: I/System.out(6876): 
08-16 15:12:34.571: I/System.out(6876): ------=_Part_0_1098789960.1345101152034
08-16 15:12:34.571: I/System.out(6876): Content-Type: text/plain; charset=us-ascii
08-16 15:12:34.571: I/System.out(6876): Content-Transfer-Encoding: 7bit
08-16 15:12:34.571: I/System.out(6876): 
08-16 15:12:34.571: I/System.out(6876): 
08-16 15:12:34.571: I/System.out(6876): ------=_Part_0_1098789960.1345101152034--
08-16 15:12:34.571: I/System.out(6876): .
08-16 15:12:34.587: I/System.out(6876): 250 OK id=1T1uFp-0007Ka-TP
08-16 15:12:34.587: I/System.out(6876): QUIT
08-16 15:12:34.603: I/System.out(6876): 221 web.mysmtp.com closing connection

如果我删除 messageBodyPart3,那么我将无法接收我的邮件。请问这是什么原因?

4

1 回答 1

0

第三部分是完全免费的,它不应该有任何区别。嗯,真的,它根本不应该在那里。除非它改变您的服务器是否认为该邮件是垃圾邮件。

无论如何,您需要提供比“无法接收”更多的信息。

您的 SMTP 服务器是否接受该邮件?

您的 SMTP 服务器是否将其发送到目标服务器?

目标服务器是否将其存储在您的邮箱中?

您是否可以使用任何邮件阅读器在您的邮箱中看到它?

于 2012-08-16T06:50:55.600 回答