title: Javamail sending mail with attachments works on Windows but not on Linux
body: <p>I'm having problems when sending emails with attachments through linux using Javamail on Tomcat 6.0 .</p>
  
  <p>When sending mails using Tomcat server on my machine (Windows 7 64), it works fine. I have tested it with both local smtp server and a gmail account as a relay. But when the mail is sent by my remote Tomcat server, which runs linux, the received mail is a mess, without subject, the attachment is shown as text(the original is a pdf file) and some mail headers comes mixed with the message. I have tried using the same approach that I used when testing on Windows: trying a local smtp server and then using the gmail as a relay. It didn't works in neither cases.</p>
  
  <p>I have also tried to change my local JavaMail libs, and use the same we have on the remote server(copied from the server to my machine), but the results running local are always the same. The email is received in the way it is expected.</p>
  
  <p>When I send the email, it is sent another copy to me, so I can keep track of what is going on. In the log below is shown Javamail output for both mails(the original and the copy) when running local tests using the Javamail that is embedded in my j2ee.jar (See below for further details):</p>
  
  <pre><code>DEBUG: setDebug: JavaMail version 1.3.1
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587
  
  220 mx.google.com ESMTP jz16sm6498879vdb.9
  DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
  
  EHLO Felipe-HP
  250-mx.google.com at your service, [177.17.162.156]
  250-SIZE 35882577
  250-8BITMIME
  250-AUTH LOGIN PLAIN XOAUTH
  250-ENHANCEDSTATUSCODES
  250 STARTTLS
  DEBUG SMTP: Found extension "SIZE", arg "35882577"
  DEBUG SMTP: Found extension "8BITMIME", arg ""
  DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
  DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP: Found extension "STARTTLS", arg ""
  DEBUG SMTP: Attempt to authenticate
  AUTH LOGIN
  334 xxxxxxxxx
  334 xxxxxxxxx
  235 2.7.0 Accepted
  DEBUG SMTP: use8bit false
  MAIL FROM:&lt; xxxxx@xxxxx.xxxx.xxxx&gt;
  250 2.1.0 OK jz16sm6498879vdb.9
  RCPT TO:&lt; xxxxx@xxxxx.xxxx.xxxx&gt;
  250 2.1.5 OK jz16sm6498879vdb.9
  DEBUG SMTP: Verified Addresses
  DEBUG SMTP:    xxxxx@xxxxx.xxxx.xxxx
  DATA
  354  Go ahead jz16sm6498879vdb.9
  Message-ID: &lt;38047647.1335464129114.JavaMail.Felipe@Felipe-HP&gt;
  From:  xxxxx@xxxxx.xxxx.xxxx
  To:  xxxxx@xxxxx.xxxx.xxxx
  Subject: Boleto Amaua - 06/05/2012
  Mime-Version: 1.0
  Content-Type: multipart/mixed; 
      boundary="----=_Part_0_1451495453.1335464128994"
  
  ------=_Part_0_1451495453.1335464128994
  Content-Type: text/plain; charset=ISO-8859-1
  Content-Transfer-Encoding: quoted-printable
  Content-Disposition: inline
  
  
  
  ------=_Part_0_1451495453.1335464128994
  Content-Type: application/octet-stream; 
      name=boleto-158-255-1335464128903.pdf
  Content-Transfer-Encoding: base64
  Content-Disposition: attachment; filename=boleto-158-255-1335464128903.pdf
  Content-Description: Attached file: boleto-158-255-1335464128903.pdf
  
  
  
  ------=_Part_0_1451495453.1335464128994--
  
  .
  250 2.0.0 OK 1335464133 jz16sm6498879vdb.9
  QUIT
  
  
  
  
  
  
  
  
  DEBUG: setDebug: JavaMail version 1.3.1
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587
  
  220 mx.google.com ESMTP jz16sm6499184vdb.9
  DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
  
  EHLO Felipe-HP
  250-mx.google.com at your service, [177.17.162.156]
  250-SIZE 35882577
  250-8BITMIME
  250-AUTH LOGIN PLAIN XOAUTH
  250-ENHANCEDSTATUSCODES
  250 STARTTLS
  DEBUG SMTP: Found extension "SIZE", arg "35882577"
  DEBUG SMTP: Found extension "8BITMIME", arg ""
  DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
  DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP: Found extension "STARTTLS", arg ""
  DEBUG SMTP: Attempt to authenticate
  AUTH LOGIN
  334 xxxxxxxxxx
  334 xxxxxxxxxx
  235 2.7.0 Accepted
  DEBUG SMTP: use8bit false
  MAIL FROM:&lt; xxxxx@xxxxx.xxxx.xxxx&gt;
  250 2.1.0 OK jz16sm6499184vdb.9
  RCPT TO:&lt; xxxxx@xxxxx.xxxx.xxxx&gt;
  250 2.1.5 OK jz16sm6499184vdb.9
  DEBUG SMTP:  xxxxx@xxxxx.xxxx.xxxx
  DEBUG SMTP:    xxxxx@xxxxx.xxxx.xxxx
  DATA
  354  Go ahead jz16sm6499184vdb.9
  Message-ID: &lt;895165784.1335464133437.JavaMail.Felipe@Felipe-HP&gt;
  From:  xxxxx@xxxxx.xxxx.xxxx
  To:  xxxxx@xxxxx.xxxx.xxxx
  Subject: =?ISO-8859-1?Q?C=F3pia_Boleto_Amaua_-_06/05/2012?=
  Mime-Version: 1.0
  Content-Type: multipart/mixed; 
      boundary="----=_Part_1_1973765399.1335464133435"
  
  ------=_Part_1_1973765399.1335464133435
  Content-Type: text/plain; charset=ISO-8859-1
  Content-Transfer-Encoding: quoted-printable
  Content-Disposition: inline
  
  ###################################################################
  Mail Content
  ###################################################################
  
  ------=_Part_1_1973765399.1335464133435
  Content-Type: application/octet-stream; 
      name=boleto-158-255-1335464128903.pdf
  Content-Transfer-Encoding: base64
  Content-Disposition: attachment; filename=boleto-158-255-1335464128903.pdf
  Content-Description: Attached file: boleto-158-255-1335464128903.pdf
  
  
  ##################################################################
  Attachment content
  ##################################################################
  
  ------=_Part_1_1973765399.1335464133435--
  
  .
  250 2.0.0 OK 1335464137 jz16sm6499184vdb.9
  QUIT
  </code></pre>
  
  <p>Below is shown the log generate by my server:</p>
  
  <pre><code>Loading javamail.default.providers from jar:file:/opt/tomcat60/lib/mail.jar!/META-INF/javamail.default.providers
  DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
  
  Loading javamail.default.providers from jar:file:/home/xxxxxxx/public_html/WEB-INF/lib/mail.jar!/META-INF/javamail.default.providers
  DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
  DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
  DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@67aece; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc
  
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
  220 mx.google.com ESMTP dc7sm2927045vdc.6
  DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
  
  EHLO l50dnn0358.locaweb.com.br
  250-mx.google.com at your service, [187.45.198.10]
  250-SIZE 35882577
  250-8BITMIME
  250-STARTTLS
  250-ENHANCEDSTATUSCODES
  250 PIPELINING
  DEBUG SMTP: Found extension "SIZE", arg "35882577"
  DEBUG SMTP: Found extension "8BITMIME", arg ""
  DEBUG SMTP: Found extension "STARTTLS", arg ""
  DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP: Found extension "PIPELINING", arg ""
  STARTTLS
  220 2.0.0 Ready to start TLS
  EHLO l50dnn0358.locaweb.com.br
  250-mx.google.com at your service, [187.45.198.10]
  250-SIZE 35882577
  250-8BITMIME
  250-AUTH LOGIN PLAIN XOAUTH
  250-ENHANCEDSTATUSCODES
  250 PIPELINING
  DEBUG SMTP: Found extension "SIZE", arg "35882577"
  DEBUG SMTP: Found extension "8BITMIME", arg ""
  DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
  DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP: Found extension "PIPELINING", arg ""
  DEBUG SMTP: Attempt to authenticate
  DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
  DEBUG SMTP: AUTH LOGIN command trace suppressed
  DEBUG SMTP: AUTH LOGIN succeeded
  DEBUG SMTP: use8bit false
  MAIL FROM:&lt;xxxxx@xxxxx.xxxx.xxxx&gt;
  250 2.1.0 OK dc7sm2927045vdc.6
  RCPT TO:&lt;xxxxx@xxxxx.xxxx.xxxx&gt;
  250 2.1.5 OK dc7sm2927045vdc.6
  DEBUG SMTP: Verified Addresses
  DEBUG SMTP:   xxxxx@xxxxx.xxxx.xxxx
  DATA
  354  Go ahead dc7sm2927045vdc.6
  
  ------=_Part_0_16475596.1335412817148
  Content-Type: text/plain; charset=ISO-8859-1
  Content-Transfer-Encoding: quoted-printable
  Content-Disposition: inline
  
  ###################################################################
  Mail Content
  ###################################################################
  
  ------=_Part_0_16475596.1335412817148
  Content-Type: application/octet-stream; name=boleto-1436-1707-1335412817075.pdf
  Content-Transfer-Encoding: base64
  Content-Disposition: attachment; filename=boleto-1436-1707-1335412817075.pdf
  Content-Description: Attached file: boleto-1436-1707-1335412817075.pdf
  
  ##################################################################
  Attachment content
  ##################################################################
  
  ------=_Part_0_16475596.1335412817148--
  .
  250 2.0.0 OK 1335412824 dc7sm2927045vdc.6
  QUIT
  221 2.0.0 closing connection dc7sm2927045vdc.6
  
  
  
  
  
  
  
  
  
  DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@67aece; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
  220 mx.google.com ESMTP c17sm2919516vdj.12
  DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
  
  EHLO l50dnn0358.locaweb.com.br
  250-mx.google.com at your service, [187.45.198.10]
  250-SIZE 35882577
  250-8BITMIME
  250-STARTTLS
  250-ENHANCEDSTATUSCODES
  250 PIPELINING
  DEBUG SMTP: Found extension "SIZE", arg "35882577"
  DEBUG SMTP: Found extension "8BITMIME", arg ""
  DEBUG SMTP: Found extension "STARTTLS", arg ""
  DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP: Found extension "PIPELINING", arg ""
  STARTTLS
  220 2.0.0 Ready to start TLS
  EHLO l50dnn0358.locaweb.com.br
  250-mx.google.com at your service, [187.45.198.10]
  250-SIZE 35882577
  250-8BITMIME
  250-AUTH LOGIN PLAIN XOAUTH
  250-ENHANCEDSTATUSCODES
  250 PIPELINING
  DEBUG SMTP: Found extension "SIZE", arg "35882577"
  DEBUG SMTP: Found extension "8BITMIME", arg ""
  DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
  DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP: Found extension "PIPELINING", arg ""
  DEBUG SMTP: Attempt to authenticate
  DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
  DEBUG SMTP: AUTH LOGIN command trace suppressed
  DEBUG SMTP: AUTH LOGIN succeeded
  DEBUG SMTP: use8bit false
  MAIL FROM:&lt;xxxxxxx@xxxxx.xxxx.xxxx&gt;
  250 2.1.0 OK c17sm2919516vdj.12
  RCPT TO:&lt;xxxxx@xxxxx.xxxx.xxxx&gt;
  250 2.1.5 OK c17sm2919516vdj.12
  DEBUG SMTP: Verified Addresses
  DEBUG SMTP:   xxxxx@xxxxx.xxxx.xxxx
  DATA
  354  Go ahead c17sm2919516vdj.12
  
  ------=_Part_1_22662950.1335412824767
  Content-Type: text/plain; charset=ISO-8859-1
  Content-Transfer-Encoding: quoted-printable
  Content-Disposition: inline
  
  ###################################################################
  Mail Content
  ###################################################################
  
  ------=_Part_1_22662950.1335412824767
  Content-Type: application/octet-stream; name=boleto-1436-1707-1335412817075.pdf
  Content-Transfer-Encoding: base64
  Content-Disposition: attachment; filename=boleto-1436-1707-1335412817075.pdf
  Content-Description: Attached file: boleto-1436-1707-1335412817075.pdf
  
  ##################################################################
  Attachment content
  ##################################################################
  
  ------=_Part_1_22662950.1335412824767--
  .
  250 2.0.0 OK 1335412828 c17sm2919516vdj.12
  QUIT
  221 2.0.0 closing connection c17sm2919516vdj.12
  </code></pre>
  
  <p>In my remote server, we have the JavaMail libs on the context/WEB-INF/lib. Where we have a mail.jar and a j2ee.jar which also contains the JavaMail. Also, in the tomcat folder there is another mail.jar(as can be seen on the logs) and another j2ee.jar that contains the Javamail.</p>
  
  <p>I have tried replacing server's /WEB-INF/lib/mail.jar with a recent one but the log didn't even changes. And using the .jars from the server in my local tests produce the correct results.</p>
  
  <p>Please, note that my server don't show some debugs lines that should be present.</p>
  
  <p>Below is the code used to send the email:</p>
  
  <pre><code>Properties props = new Properties();
  props.put("mail.smtp.debug", "true");
  props.put("mail.smtp.auth", "true");
  props.put("mail.mime.charset", "ISO-8859-1"); 
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.starttls.required", "true");
  props.put("mail.smtp.host", smtpHost);
  props.put("mail.smtp.port", Integer.toString(port));
  this.session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(usr, pass);
        }
  });
  
  MimeMultipart mp = new MimeMultipart("mixed");
  MimeBodyPart txtPart = new MimeBodyPart();
  
  if(this.message == null){
      if(this.session != null){
         this.message = new MimeMessage(this.session);
      } else {
         throw new Exception("");
      }
  }
  
  this.session.setDebug(true);
  this.message.setFrom(new InternetAddress(this.from));
  this.message.setRecipient(Message.RecipientType.TO, new InternetAddress(this.to));
  this.message.setSubject(this.subject);
  
  txtPart.setDisposition(Part.INLINE);
  txtPart.setText(text);
  mp.addBodyPart(txtPart);
  
  for (int i = 0; i &lt; this.attach.size(); i++) {
      MimeBodyPart filePart = new MimeBodyPart();
      File file = (File) this.attach.elementAt(i);
      FileDataSource fds = new FileDataSource(file);
      DataHandler dh = new DataHandler(fds);
      filePart.setFileName(file.getName());
      filePart.setDisposition(Part.ATTACHMENT);
      filePart.setDescription("Attached file: " + file.getName());
      filePart.setDataHandler(dh);
      mp.addBodyPart(filePart);
  }
  
  this.message.setContent(mp);
  
  Transport.send(this.message);
  </code></pre>
  
  <p>Any further detail needed, please ask.</p>
  
  <p>Thanks for your consideration!</p>