0

JavaMail API を使用してメールを送信します。最初に、メールの送信に次のコードを使用します。正常に動作し、メールを正常に受信できます。ただし、本文メッセージがメッセージ フィールドではなく txt ファイルに保存されるという問題があります。

MailSender クラス:

public class MailSender extends Authenticator { 
    private String user; 
    private String password; 

    private String [] to; 
    private String from; 

    private String port; 
    private String sport; 

    private String host; 

    private String subject; 
    private String body; 

    private boolean auth; 
    private boolean debuggable; 

    private Multipart multi; 

    public MailSender(){ 
        host = "me.mysmtp.com"; 
        port = "25"; 
        sport = "25"; 

        user = ""; 
        password = ""; 
        from = ""; 
        subject = ""; 
        body = ""; 

        debuggable = true; 
        auth = true; 

        multi = new MimeMultipart(); 

        // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. 
        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();  
        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");  
        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");  
        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");  
        CommandMap.setDefaultCommandMap(mc);  
    } 

    public MailSender(String user, String password){ 
        this();       
        this.user = user; 
        this.password = password;    
    } 

    public boolean send() throws Exception { 
        Properties props = setProperties(); 

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

            MimeMessage msg = new MimeMessage(session); 

            msg.setFrom(new InternetAddress(from)); 

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

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

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

            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; 
        } 
    } 

    public void addAttachment(String filename) throws Exception { 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(filename); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName("record.csv"); 

        multi.addBodyPart(messageBodyPart); 
    } 

    @Override  
      public PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication(user, password);  
      } 

    private Properties setProperties() { 
        Properties props = new Properties(); 

        props.put("mail.smtp.host", host); 

        if(debuggable) { 
            props.put("mail.debug", "true"); 
        } 

        if(auth) { 
            props.put("mail.smtp.auth", "true"); 
        } 

        props.put("mail.smtp.ssl.enable", "false");
        props.put("mail.smtp.port", port); 
        props.put("mail.smtp.socketFactory.port", sport); 
        props.setProperty("mail.smtp.ssl.enable", "false");
        props.setProperty("mail.smtp.ssl.socketFactory.fallback", "false");

        return props; 
    } 

    public void setTo(String[] toAddress) { 
        this.to = toAddress; 
    } 

    public void setFrom(String fromAddress) { 
        this.from = fromAddress; 
    } 

    public void setSubject(String subject) { 
        this.subject = subject; 
    } 

    public void setBody(String body) {  
        this.body = body;  
    } 
}

画面キャプチャ:

メッセージ欄に何もない 間違ったファイルに

そこで、マルチパート オブジェクトのボディパーツの順序を変更しました。ただし、コードを変更した後、アプリはメールを正常に送信したと応答しますが、メールを受信できません。

ログのメッセージは次のとおりです。

成功したもの:

08-16 11:44:13.371: I/System.out(25774): MAIL FROM:<me@mysmtp.com>
08-16 11:44:13.379: I/System.out(25774): 250 OK
08-16 11:44:13.379: I/System.out(25774): RCPT TO:<you@mysmtp.com>
08-16 11:44:13.418: I/System.out(25774): 250 Accepted
08-16 11:44:13.418: I/System.out(25774): RCPT TO:<admin@mysmtp.com>
08-16 11:44:13.434: I/System.out(25774): 250 Accepted
08-16 11:44:13.434: I/System.out(25774): DEBUG SMTP: Verified Addresses
08-16 11:44:13.434: I/System.out(25774): DEBUG SMTP:   you@mysmtp.com
08-16 11:44:13.434: I/System.out(25774): DEBUG SMTP:   admin@mysmtp.com
08-16 11:44:13.434: I/System.out(25774): DATA
08-16 11:44:13.442: I/System.out(25774): 354 Enter message, ending with "." on a line by itself
08-16 11:44:13.536: I/System.out(25774): Date: Thu, 16 Aug 2012 11:44:10 +0800 (HKT)
08-16 11:44:13.536: I/System.out(25774): From: me@mysmtp.com
08-16 11:44:13.536: I/System.out(25774): To: you@mysmtp.com, admin@mysmtp.com
08-16 11:44:13.536: I/System.out(25774): Message-ID: <1099034872.1.1345088653528.JavaMail.javamailuser@localhost>
08-16 11:44:13.536: I/System.out(25774): Subject: Record cannot be inserted!
08-16 11:44:13.536: I/System.out(25774): MIME-Version: 1.0
08-16 11:44:13.536: I/System.out(25774): Content-Type: multipart/mixed; 
08-16 11:44:13.536: I/System.out(25774):    boundary="----=_Part_0_1098872744.1345088650701"
08-16 11:44:13.536: I/System.out(25774): 
08-16 11:44:13.536: I/System.out(25774): ------=_Part_0_1098872744.1345088650701
08-16 11:44:13.536: I/System.out(25774): Content-Type: application/octet-stream; name=record.csv
08-16 11:44:13.536: I/System.out(25774): Content-Transfer-Encoding: 7bit
08-16 11:44:13.536: I/System.out(25774): Content-Disposition: attachment; filename=record.csv
08-16 11:44:13.536: I/System.out(25774): 
08-16 11:44:13.536: I/System.out(25774): ABD,HK,A,2012/08/16 10:02, ,
08-16 11:44:13.536: I/System.out(25774): 
08-16 11:44:13.536: I/System.out(25774): ------=_Part_0_1098872744.1345088650701
08-16 11:44:13.536: I/System.out(25774): Content-Type: text/plain; charset=us-ascii
08-16 11:44:13.536: I/System.out(25774): Content-Transfer-Encoding: 7bit
08-16 11:44:13.536: I/System.out(25774): 
08-16 11:44:13.536: I/System.out(25774): Please insert manually!
08-16 11:44:13.536: I/System.out(25774): ------=_Part_0_1098872744.1345088650701--
08-16 11:44:13.536: I/System.out(25774): .
08-16 11:44:13.551: I/System.out(25774): 250 OK id=1T1r0E-0001LQ-Al
08-16 11:44:13.551: I/System.out(25774): QUIT
08-16 11:44:13.567: I/System.out(25774): 221 web.mysmtp.com closing connection

失敗したもの:

08-16 11:42:59.129: I/System.out(25322): MAIL FROM:<me@mysmtp.com>
08-16 11:42:59.137: I/System.out(25322): 250 OK
08-16 11:42:59.137: I/System.out(25322): RCPT TO:<you@mysmtp.com>
08-16 11:42:59.192: I/System.out(25322): 250 Accepted
08-16 11:42:59.192: I/System.out(25322): RCPT TO:<admin@mysmtp.com>
08-16 11:42:59.200: I/System.out(25322): 250 Accepted
08-16 11:42:59.200: I/System.out(25322): DEBUG SMTP: Verified Addresses
08-16 11:42:59.200: I/System.out(25322): DEBUG SMTP:   you@mysmtp.com
08-16 11:42:59.200: I/System.out(25322): DEBUG SMTP:   admin@mysmtp.com
08-16 11:42:59.200: I/System.out(25322): DATA
08-16 11:42:59.207: I/System.out(25322): 354 Enter message, ending with "." on a line by itself
08-16 11:42:59.270: I/System.out(25322): Date: Thu, 16 Aug 2012 11:42:56 +0800 (HKT)
08-16 11:42:59.270: I/System.out(25322): From: me@mysmtp.com
08-16 11:42:59.270: I/System.out(25322): To: you@mysmtp.com, admin@mysmtp.com
08-16 11:42:59.270: I/System.out(25322): Message-ID: <1099222896.1.1345088579269.JavaMail.javamailuser@localhost>
08-16 11:42:59.270: I/System.out(25322): Subject: Record cannot be inserted!
08-16 11:42:59.270: I/System.out(25322): MIME-Version: 1.0
08-16 11:42:59.270: I/System.out(25322): Content-Type: multipart/mixed; 
08-16 11:42:59.270: I/System.out(25322):    boundary="----=_Part_0_1098885208.1345088576714"
08-16 11:42:59.270: I/System.out(25322): 
08-16 11:42:59.270: I/System.out(25322): ------=_Part_0_1098885208.1345088576714
08-16 11:42:59.270: I/System.out(25322): Content-Type: text/plain; charset=us-ascii
08-16 11:42:59.270: I/System.out(25322): Content-Transfer-Encoding: 7bit
08-16 11:42:59.270: I/System.out(25322): 
08-16 11:42:59.270: I/System.out(25322): Please insert manually!
08-16 11:42:59.278: I/System.out(25322): ------=_Part_0_1098885208.1345088576714
08-16 11:42:59.278: I/System.out(25322): Content-Type: application/octet-stream; name=record.csv
08-16 11:42:59.278: I/System.out(25322): Content-Transfer-Encoding: 7bit
08-16 11:42:59.278: I/System.out(25322): Content-Disposition: attachment; filename=record.csv
08-16 11:42:59.278: I/System.out(25322): 
08-16 11:42:59.278: I/System.out(25322): ABD,HK,A,2012/08/16 10:02, ,
08-16 11:42:59.278: I/System.out(25322): 
08-16 11:42:59.278: I/System.out(25322): ------=_Part_0_1098885208.1345088576714--
08-16 11:42:59.278: I/System.out(25322): .
08-16 11:42:59.325: I/System.out(25322): 250 OK id=1T1qz2-0001Iq-3T
08-16 11:42:59.325: I/System.out(25322): QUIT
08-16 11:42:59.332: I/System.out(25322): 221 web.mysmtp.com closing connection

2番目のものが正常に配信されない理由がわかりません。誰か手を貸してくれませんか?

4

1 に答える 1

0

メッセージはメール サーバーに配信されています。メールサーバーのログを見て、それが何をしているのかを確認してください。

于 2012-08-16T06:42:40.850 に答える