title: SMTP Mail with Java SSLSocket Text and Attachment
body: <p>So I am trying to send an Email with SSLSocket in Java through a SMTP-Server and want to send a message in the body and attachment together. I can send one or the other one but not both at the same time. I get no errors. It writes the text and the attachment as a text in the body. When I comment the text part out, it sends the attachment.</p>
  
  <p>I searched a lot but only get hints with javamail, which I don't want to use. Any help please?</p>
  
  <pre><code>public void sendEmail(){
      String boundary = "__frontier";
      String boundary2 = "__frontier2";
  
      String fileName = "anhang.txt";
      String textTest ="Hello...i am here";
      try{
          sendCommandAndResultCheck("MAIL FROM: " + my_email_addr);
          sendCommandAndResultCheck("RCPT TO: " + recipient);
          sendCommandAndResultCheck("DATA");
  
          smtp_out.print("From: "+my_email_addr+"\r\n");
          smtp_out.print("To: "+recipient+ "\r\n");
          smtp_out.print("Subject: "+"Test \r\n");
  
          smtp_out.print("MIME-Version: 1.0\n");          
          smtp_out.print("Content-Type: multipart/mixed; boundary="+boundary);
          smtp_out.print("--"+boundary);
          smtp_out.print("Content-Type: multipart/related; boundary="+boundary2);
          smtp_out.print("--"+boundary2);
          smtp_out.print("Content-Type: text/plain; charset=\"ISO-8859-1\"");
          smtp_out.print("Content-Transfer-Encoding: quoted-printable\n");
          smtp_out.print(textTest);
          smtp_out.print("\r\n");
          smtp_out.print("--"+boundary2+"--");
  
          smtp_out.print("--"+boundary);
          smtp_out.print("Content-Type: application/text; name="+fileName+"\n");
          smtp_out.print("Content-Transfer-Encoding: base64\n");
          smtp_out.print("Content-Disposition: attachment; filename="+fileName+"\n");
  
          BufferedReader br = new BufferedReader(new FileReader (new File("src/"+fileName)));
          String s = "";
          while((s=br.readLine())!=null)
          {
              s=Base64.getEncoder().encodeToString(s.getBytes());
              smtp_out.print("\r\n"+s+"\r\n");
          }
          smtp_out.print("\r\n.\r\n");            
          smtp_out.print("--"+boundary+"--");
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Here the Email I get:</p>
  
  <pre><code>Hello...i am here
  --__frontier2----__frontierContent-Type: application/text; name=anhang.txt
  Content-Transfer-Encoding: base64
  Content-Disposition: attachment; filename=anhang.txt
  
  SXQgbWlnaHQgc2VlbSBjcmF6eSB3aGF0IEknbSAnYm91dCB0byBzYXk=
  
  U3Vuc2hpbmUgc2hlJ3MgaGVyZSwgeW91IGNhbiB0YWtlIGEgYnJlYWs=
  
  SSdtIGEgaG90IGFpciBiYWxsb29uIHRoYXQgY291bGQgZ28gdG8gc3BhY2U=
  
  V2l0aCB0aGUgYWlyLCBsaWtlIEkgZG9uJ3QgY2FyZSBiYWJ5IGJ5IHRoZSB3YXk=
  
  QmVjYXVzZSBJJ20gaGFwcHk=
  
  Q2xhcCBhbG9uZyBpZiB5b3UgZmVlbCBsaWtlIGEgcm9vbSB3aXRob3V0IGEgcm9vZg==
  
  QmVjYXVzZSBJJ20gaGFwcHk=
  
  Q2xhcCBhbG9uZyBpZiB5b3UgZmVlbCBsaWtlIGhhcHBpbmVzcyBpcyB0aGUgdHJ1dGg=
  </code></pre>
  
  <p>I tried also to set <code>\r\n</code> at the end of some commands. But I don't really understand when I have to use them or not.</p>