2013-06-13 13 views
9

मेरा लक्ष्य: सादे/पाठ, पाठ/एचटीएमएल, और अनुलग्नक के साथ एसएमटीपी के माध्यम से लेनदेन संबंधी ईमेल भेजें।एसएमटीपी के माध्यम से अनुलग्नक, सादा/पाठ, और पाठ/एचएमएल

मेरे कोड: JavaMail

मेरे मुद्दे के साथ लागू किया: यह हॉटमेल पर ठीक लग रहा है, या दृष्टिकोण। लेकिन जीमेल पर, यह संदेश बॉडी को ठीक से नहीं दिखाता है अगर यह एक .txt अटैचमेंट वाला ईमेल है (यह अनुलग्नक छवियों के ठीक से काम करता है)

किसी भी मदद की अत्यधिक सराहना की जाएगी।

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 

कुछ स्क्रीनशॉट

केवल एक .txt अनुलग्नक के साथ भेजा गया:

यहाँ मेरी कच्चे एसएमटीपी उत्पादन होता है। संदेश निकाय प्रदर्शित नहीं करता है और अनुलग्नक डुप्लिकेट किया जाता है।

enter image description here

एक ही संदेश लेकिन अलग अनुलग्नक के साथ (.gif)। सब ठीक दिखता है। enter image description here

जावा डेवलपर्स ==== लिए === समाधान

समग्र विचार यहाँ वर्णित है:

// 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()); 
        } 
       } 
      } 
: http://www.coderanch.com/t/503380/java/java/Java-Mail-text-html-attachment

तो, अब मेरी कोड की तरह दिखता है

उत्तर

10

आपके संदेश की संरचना गलत है। आपको सही संरचना प्राप्त करने के लिए नेस्टेड मल्टीपार्ट की आवश्यकता है, इस तरह कुछ:

multipart/mixed 
    multipart/alternative (holding the two forms of the body part) 
     text/plain 
     text/html 
    text/plain or image/gif (the attachment) 
+0

यह सही है। असल में, मैंने आपके उत्तर को देखने से पहले इस मुद्दे के समाधान को थोड़ा सा लागू किया। आपका बहुत बहुत धन्यवाद। – Rafa

+0

क्या होगा अगर कुछ अनुलग्नक वास्तव में चित्र/HTML भाग में एम्बेडेड चित्र हैं? सही संरचना कैसा दिखता है, तो? क्या मैं बस "मिश्रित" को "संबंधित" से बदल सकता हूं? या क्या मुझे इनलाइन स्वभाव के साथ "संबंधित" अनुलग्नकों के लिए अलग-अलग उप-वर्गों का उपयोग करना होगा, और बाकी के लिए "मिश्रित" होना चाहिए? मैं बहुत उलझन में हूं ... :( – snooze92

+0

मिश्रित के साथ रखें। यहां प्रस्तुत उत्तर टेक्स्ट और बाइनरी फाइलों के साथ बहुत काम करता है। – Rafa

संबंधित मुद्दे