0
   public static boolean sendEmail() throws Exception {
   ServerSettings server; 
   String receiver="someone@example.org"; 
   File mail = new File("file.eml"); 
   String subject = "subject";
        try {
            Properties props = System.getProperties();
            Session session = Session.getInstance(props, null);
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("someoneelse@example.org"));;
            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(receiver, false));
            InputStream is = new FileInputStream(mail.getAbsoluteFile());
            MimeMessage message = new MimeMessage(session, is);
            msg.setSubject(subject);
            msg.setContent(message.getContent(), message.getEncoding());
            msg.setHeader("X-Mailer", "Email program");
            msg.setSentDate(new Date());
            SMTPTransport t = null;
            props.put("mail.smtp.host", "smtp.outlook.com");
            t = (SMTPTransport) session.getTransport("smtp");
            t.setStartTLS(true);
            t.connect("smtp.outlook.com", 587, "someone@example.org", "1234");
            t.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Response: " + t.getLastServerResponse());
            return true;
        } catch (Exception e) {
            System.out.println("Not sent error: ");
            e.printStackTrace();
            throw e;
            return false;
        }
        return false;
    }

Error:

If I save an EML file using Thunderbird, I the message is sent normally. However if using Google's show original message, I get the following error

javax.mail.internet.ParseException: Expected '/', got null
    at javax.mail.internet.ContentType.<init>(ContentType.java:102)
    at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1331)
    at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
    at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
    at javax.mail.Transport.send(Transport.java:145)

Example:

Thunderbird EML:

Delivered-To: <redacted>@gmail.com Received: by 10.12.175.177 with SMTP id s46csp2076396qvc;
        Tue, 6 Jun 2017 10:00:00 -0700 (PDT) X-Received: by 10.28.194.198 with SMTP id s189mr4723102wmf.123.1496768400250;
        Tue, 06 Jun 2017 10:00:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496768400; cv=none;
        d=google.com; s=arc-20160816;
        b=muU9j+79SMnr5A+zG3srmyV566l+sGO2uNkXKiSBXv6bmGZrrnkKshbWmYuG/glSA0
         zdKK3kxIdqvXq2lgpf5xLUmd2kHLfnmFLRAP44VsX4cWE222XLceCVWB6Kz7FYrvQyOb
         eZsR5TxeNGYNMGYRsA22ojYA5hDapa7N2vA6Uq5DNt3Jh7iMFFfaRemao0IBPdChmP0k
         +1kfS06vuUm8JptpNQUzAR6UI1VHvkilWwLZADwjhTLHxyDXU/dA+m8Tl0kEBALfx5Ey
         sOa5V2yC6jaHXjrFh/YEm3lsOA4DBdUkVXCl+nsm2+ldzypjh6dl7Fjowr/Bh4pqVoM6
         +uPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=content-transfer-encoding:mime-version:subject:message-id:to:from
         :date:arc-authentication-results;
        bh=mez4y5GUUS/twpn+ODAQpRYd5LFrkFHDFwrnRROrKQw=;
        b=CnLDxOneD7gp0ycGUjuOU+HBiVR9pgiVhIi+81yEZ+q48bC/fyhPikIJxsE3gnZZN9
         RDsEcTq3HEii6jBNRNRT6P0lG07HEsVG4kCMUPezmE1SYJj1KKZ1o9XhU33dY5yNz1TZ
         LzbUCQdA0QHNccR5JW7QMzGVBmEMMr9Dn+9jFe4ms4YDwaA8+bas9zfQdVqR+R41p+LA
         WP6+MGAnhjCBTjdqzeM9mAO2i/gpqJhuv/+H5BPJQ8r6Wk0ItXRAVv74rDkMUUMbfAg8
         st8WwRKN5nU+5ZKFON+JTw4+l0hsddtjO8qV2op/Q9VNBKrLcJANhdCXADW4lzTKeP+j
         fklw== ARC-Authentication-Results: i=1; mx.google.com;
       spf=pass (google.com: best guess record for domain of <redacted>@t-online.de designates 194.25.134.84 as permitted sender) smtp.mailfrom=<redacted>@t-online.de Return-Path: <<redacted>@t-online.de> Received: from mailout09.t-online.de (mailout09.t-online.de. [194.25.134.84])
        by mx.google.com with ESMTPS id 91si12402140wrf.105.2017.06.06.10.00.00
        for <<redacted>@gmail.com>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 06 Jun 2017 10:00:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of <redacted>@t-online.de designates 194.25.134.84 as permitted sender) client-ip=194.25.134.84; Authentication-Results: mx.google.com;
       spf=pass (google.com: best guess record for domain of <redacted>@t-online.de designates 194.25.134.84 as permitted sender) smtp.mailfrom=<redacted>@t-online.de Received: from fwd34.aul.t-online.de (fwd34.aul.t-online.de [172.20.26.145])     by mailout09.t-online.de (Postfix) with SMTP id BD7BB427CD24    for <<redacted>@gmail.com>; Tue,  6 Jun 2017 18:59:59 +0200 (CEST) Received: from 192.168.10.106 (Gh+1Z-ZrYh+NeNL9ZgFe5BnHV3ELdJQ--fuZLhOzcsrLBbkHL3uoBI7jVcElEdow8J@[179.228.100.105]) by fwd34.t-online.de    with (TLSv1:ECDHE-RSA-AES128-SHA encrypted)     esmtp id 1dIHph-14uvui0; Tue, 6 Jun 2017 18:59:53 +0200
Date: Tue, 6 Jun 2017 13:59:47 -0300 (BRT)
From: <redacted>@t-online.de To: <redacted>@gmail.com
Message-ID: <1310540333.0.1496768392987.JavaMail.Mansueli@Mansueli-Computer>
Subject: japanese encoding issue
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8; Content-Transfer-Encoding=7bit
Content-Transfer-Encoding: base64
X-Mailer: Rodrigo's program
X-ID: Gh+1Z-ZrYh+NeNL9ZgFe5BnHV3ELdJQ--fuZLhOzcsrLBbkHL3uoBI7jVcElEdow8J
X-TOI-MSGID: 21dc56c3-b5f6-4c7d-88d0-93c4171a46c4

PGh0bWw+PGJvZHk+PHA+0JLRiyDQv9C+0LvRg9GH0LjQu9C4INGN0YLQviDQv9C40YHRjNC80L4g 0LjQty3Qt9CwINC/0L7Qv9GL0YLQutC4INCy0YXQvtC00LAg0YEg0LrQvtC80L/RjNGO0YLQtdGA PC9oMT48L2JvZHk+PC9odG1sPg==

Google txt:

Delivered-To: <redacted>@gmail.com Received: by 10.12.175.177 with SMTP id s46csp2076396qvc;
        Tue, 6 Jun 2017 10:00:00 -0700 (PDT) X-Received: by 10.28.194.198 with SMTP id s189mr4723102wmf.123.1496768400250;
        Tue, 06 Jun 2017 10:00:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496768400; cv=none;
        d=google.com; s=arc-20160816;
        b=muU9j+79SMnr5A+zG3srmyV566l+sGO2uNkXKiSBXv6bmGZrrnkKshbWmYuG/glSA0
         zdKK3kxIdqvXq2lgpf5xLUmd2kHLfnmFLRAP44VsX4cWE222XLceCVWB6Kz7FYrvQyOb
         eZsR5TxeNGYNMGYRsA22ojYA5hDapa7N2vA6Uq5DNt3Jh7iMFFfaRemao0IBPdChmP0k
         +1kfS06vuUm8JptpNQUzAR6UI1VHvkilWwLZADwjhTLHxyDXU/dA+m8Tl0kEBALfx5Ey
         sOa5V2yC6jaHXjrFh/YEm3lsOA4DBdUkVXCl+nsm2+ldzypjh6dl7Fjowr/Bh4pqVoM6
         +uPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=content-transfer-encoding:mime-version:subject:message-id:to:from
         :date:arc-authentication-results;
        bh=mez4y5GUUS/twpn+ODAQpRYd5LFrkFHDFwrnRROrKQw=;
        b=CnLDxOneD7gp0ycGUjuOU+HBiVR9pgiVhIi+81yEZ+q48bC/fyhPikIJxsE3gnZZN9
         RDsEcTq3HEii6jBNRNRT6P0lG07HEsVG4kCMUPezmE1SYJj1KKZ1o9XhU33dY5yNz1TZ
         LzbUCQdA0QHNccR5JW7QMzGVBmEMMr9Dn+9jFe4ms4YDwaA8+bas9zfQdVqR+R41p+LA
         WP6+MGAnhjCBTjdqzeM9mAO2i/gpqJhuv/+H5BPJQ8r6Wk0ItXRAVv74rDkMUUMbfAg8
         st8WwRKN5nU+5ZKFON+JTw4+l0hsddtjO8qV2op/Q9VNBKrLcJANhdCXADW4lzTKeP+j
         fklw== ARC-Authentication-Results: i=1; mx.google.com;
       spf=pass (google.com: best guess record for domain of <redacted>@t-online.de designates 194.25.134.84 as permitted sender) smtp.mailfrom=<redacted>@t-online.de Return-Path: <<redacted>@t-online.de> Received: from mailout09.t-online.de (mailout09.t-online.de. [194.25.134.84])
        by mx.google.com with ESMTPS id 91si12402140wrf.105.2017.06.06.10.00.00
        for <<redacted>@gmail.com>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 06 Jun 2017 10:00:00 -0700 (PDT)
Received-SPF: pass (google.com: best guess record for domain of <redacted>@t-online.de designates 194.25.134.84 as permitted sender) client-ip=194.25.134.84; Authentication-Results: mx.google.com;
       spf=pass (google.com: best guess record for domain of <redacted>@t-online.de designates 194.25.134.84 as permitted sender) smtp.mailfrom=<redacted>@t-online.de
Received: from fwd34.aul.t-online.de (fwd34.aul.t-online.de [172.20.26.145])    by mailout09.t-online.de (Postfix) with SMTP id BD7BB427CD24    for <<redacted>@gmail.com>; Tue,  6 Jun 2017 18:59:59 +0200 (CEST)
Received: from 192.168.10.106 (Gh+1Z-ZrYh+NeNL9ZgFe5BnHV3ELdJQ--fuZLhOzcsrLBbkHL3uoBI7jVcElEdow8J@[179.228.100.105]) by fwd34.t-online.de   with (TLSv1:ECDHE-RSA-AES128-SHA encrypted)     esmtp id 1dIHph-14uvui0; Tue, 6 Jun 2017 18:59:53 +0200
Date: Tue, 6 Jun 2017 13:59:47 -0300 (BRT)
From:<redacted>@t-online.de
To: <redacted>@gmail.com
Message-ID: <1310540333.0.1496768392987.JavaMail.Mansueli@Mansueli-Computer>
Subject: japanese encoding issue
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8; Content-Transfer-Encoding=7bit
Content-Transfer-Encoding: base64
X-Mailer: Rodrigo's program
X-ID: Gh+1Z-ZrYh+NeNL9ZgFe5BnHV3ELdJQ--fuZLhOzcsrLBbkHL3uoBI7jVcElEdow8J
X-TOI-MSGID: 21dc56c3-b5f6-4c7d-88d0-93c4171a46c4

PGh0bWw+PGJvZHk+PHA+0JLRiyDQv9C+0LvRg9GH0LjQu9C4INGN0YLQviDQv9C40YHRjNC80L4g 0LjQty3Qt9CwINC/0L7Qv9GL0YLQutC4INCy0YXQvtC00LAg0YEg0LrQvtC80L/RjNGO0YLQtdGA PC9oMT48L2JvZHk+PC9odG1sPg==

Is Google's show original message not a valid EML? How to make it compliant with JavaMail EML parser?

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
DeMarco
  • 599
  • 1
  • 8
  • 26
  • I fixed the formatting of your post; I hope it was just a copy&paste error. I don't see any problems with the Content-Type value and I can't reproduce the error with the sample message above. How are you reading the message from the file? Are you making any changes to the message before sending it? What version of JavaMail are you using? – Bill Shannon Jun 06 '17 at 19:06
  • JavaMail 1.4 on x64 JVM for Windows (Oracle SDK) / Netbeans. Only changes are on the to field on `msg.setFrom(new InternetAddress("someoneelse@example.org"));` – DeMarco Jun 07 '17 at 00:40

1 Answers1

1

You're copying the content from the original message to a new message and you're using the encoding as the MIME type, which it is not, which is why you're getting the exception. (Google vs. Thunderbird shouldn't make a difference here.)

Try this simpler version instead:

        Properties props = System.getProperties();
        props.put("mail.smtp.host", "smtp.outlook.com");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);
        InputStream is = new FileInputStream(mail.getAbsoluteFile());
        MimeMessage msg = new MimeMessage(session, is);
        msg.setFrom(new InternetAddress("someoneelse@example.org"));;
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(receiver, false));
        msg.setSubject(subject);
        msg.setHeader("X-Mailer", "Email program");
        msg.setSentDate(new Date());
        Transport.send(msg, "someone@example.org", "1234");
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40