title: Send email via SMTP with attachment, plain/text, and text/hml
body: <p><strong>My goal:</strong> Send transactional emails via SMTP with plain/text, text/html, and attachments.</p>
  
  <p><strong>My code:</strong> Implemented with JavaMail</p>
  
  <p><strong>My issue:</strong> It looks fine on hotmail, or outlook. But on <strong>gmail</strong>, it does not show the message body properly if it is an email with a .txt attachment (it works alright if attachments are images)</p>
  
  <p>Any help would be highly appreciated.</p>
  
  <p>Here is my raw SMTP output:</p>
  
  <pre><code>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 &lt;b&gt;html&lt;/b&gt; format! Sent on Thu Jun 13 17:48:04 EDT 2013&lt;br&gt; to: me@gmail.com&lt;br&gt; to: me@mijo.com&lt;br&gt; cc: me@hotmail.com&lt;br&gt; cc: rafael.santos.test@hotmail.com
  ------=_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
  </code></pre>
  
  <p><strong>Some screenshots</strong></p>
  
  <p>Sent with only one .txt attachment. The message body does not display and attachment are duplicated.</p>
  
  <p><img src="https://i.stack.imgur.com/0KLAB.png" alt="enter image description here"></p>
  
  <p>Same message but with different attachment (.gif). Everything looks fine.
  <img src="https://i.stack.imgur.com/MpkJI.png" alt="enter image description here"></p>
  
  <p><strong><em>=== SOLUTION FOR JAVA DEVELOPERS ====</em></strong></p>
  
  <p>The overall idea is described here: <a href="http://www.coderanch.com/t/503380/java/java/Java-Mail-text-html-attachment" rel="noreferrer">http://www.coderanch.com/t/503380/java/java/Java-Mail-text-html-attachment</a></p>
  
  <p>So, now my code looks like:</p>
  
  <pre><code>// 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());
                      }
                  }
              }
  </code></pre>