Senden Sie E-Mails über SMTP mit Anhang, Nur / Text und Text / HTML

Mein Ziel: Senden Sie Transaktions-E-Mails über SMTP mit Klartext, Text und Anhängen.

Mein Code: Implementiert mit JavaMail

Mein Problem: Bei Hotmail oder Outlook sieht es gut aus. Aber weiterGoogle Mailwird der Nachrichtentext nicht richtig angezeigt, wenn es sich um eine E-Mail mit einem TXT-Anhang handelt (funktioniert in Ordnung, wenn Anhänge Bilder sind)

Jede Hilfe wäre sehr dankbar.

Hier ist meine rohe SMTP-Ausgabe:

Subject: ALTERNATIVE | TXT | HTML |ATT.ATTACHMENT | Thu Jun 13 17:48:04 EDT
 2013
MIME-Version: 1.0
Content-Type: multipart/alternative; 
    boundary="----=_Part_0_21791733.1371160084561"

------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Body message in text format!
------=_Part_0_21791733.1371160084561
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

Body message in <b>html</b> format! Sent on Thu Jun 13 17:48:04 EDT 2013<br> to: [email protected]<br> to: [email protected]<br> cc: [email protected]<br> cc: [email protected]
------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii; name=email_attachment.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=email_attachment.txt

This is a text attachment file!
------=_Part_0_21791733.1371160084561--
.
250 Delivery in progress
QUIT

Einige Screenshots

Wird nur mit einem TXT-Anhang gesendet. Der Nachrichtentext wird nicht angezeigt und die Anlage wird dupliziert.

Gleiche Nachricht, aber mit anderem Anhang (.gif). Alles sieht gut aus.

=== LÖSUNG FÜR JAVA ENTWICKLER ====

Die Gesamtidee wird hier beschrieben:http://www.coderanch.com/t/503380/java/java/Java-Mail-text-html-attachment

Nun sieht mein Code so aus:

// contentPart is the content to be sent. It is divided in bodyContent and attachmentContent
            MimeMultipart contentPart = new MimeMultipart("mixed");

            // Message body in txt and html format
            MimeMultipart bodyPart = new MimeMultipart("alternative");
            // Creates plain text message
            BodyPart bodyTxt = new MimeBodyPart();
            bodyTxt.setText(getMessageBodyText());
            // Creates html message
            BodyPart bodyHtml = new MimeBodyPart();
            bodyHtml.setContent(getMessageBodyHtml(), "text/html");
            bodyPart.addBodyPart(bodyTxt);
            bodyPart.addBodyPart(bodyHtml);

            // Wrapper for bodyTxt and bodyHtml
            MimeBodyPart bodyContent = new MimeBodyPart();
            bodyContent.setContent(bodyPart);

            // At this point, contentPart contains bodyTxt and bodyHtml wrapped in a multipart/alternative
            contentPart.addBodyPart(bodyContent);

            // Adds attachments to contentPart
            if (getAttachments() != null) {
                for(File f : getAttachments()) {
                    try {
                        MimeBodyPart attachmentPart = new MimeBodyPart();
                        attachmentPart.attachFile(f);
                        contentPart.addBodyPart(attachmentPart);
                    } catch (IOException e) {
                        logger.severe("Could not attach file to email!" +
                                " TO: "+ getTo().toString() +
                                "; CC: "+ getCc().toString() +
                                "; ExceptionMessage: " + e.getMessage());
                        throw new SmtpRequestException(e.getMessage());
                    }
                }
            }

Antworten auf die Frage(1)

Ihre Antwort auf die Frage