2012-08-16 38 views
0

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

我的代码段用于发送邮件:

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:<[email protected]> 
08-16 15:09:33.415: I/System.out(6314): 250 OK 
08-16 15:09:33.415: I/System.out(6314): RCPT TO:<[email protected]> 
08-16 15:09:33.470: I/System.out(6314): 250 Accepted 
08-16 15:09:33.470: I/System.out(6314): RCPT TO:<[email protected]> 
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: [email protected] 
08-16 15:09:33.478: I/System.out(6314): DEBUG SMTP: [email protected] 
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: [email protected] 
08-16 15:09:33.564: I/System.out(6314): To: [email protected], [email protected] 
08-16 15:09:33.564: I/System.out(6314): Message-ID: <[email protected]> 
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:<[email protected]> 
08-16 15:12:34.431: I/System.out(6876): 250 OK 
08-16 15:12:34.431: I/System.out(6876): RCPT TO:<[email protected]> 
08-16 15:12:34.470: I/System.out(6876): 250 Accepted 
08-16 15:12:34.470: I/System.out(6876): RCPT TO:<[email protected]> 
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: [email protected] 
08-16 15:12:34.486: I/System.out(6876): DEBUG SMTP: [email protected] 
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: [email protected] 
08-16 15:12:34.571: I/System.out(6876): To: [email protected], [email protected] 
08-16 15:12:34.571: I/System.out(6876): Message-ID: <[email protected]> 
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 

如果我remov e messageBodyPart3,那么我将无法收到我的邮件。请问这是什么原因?

回答

0

第三部分是完全无偿的,它不应该有任何区别。呃,真的,它不应该在那里。除非它改变你的服务器是否认为这个消息是垃圾邮件。

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

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

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

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

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

+0

可能是我可以显示你的日志,那么它会使你更容易识别问题。 – Conrad 2012-08-16 07:02:59