2017-06-06 76 views
0
public static boolean sendEmail() throws Exception { 
    ServerSettings server; 
    String receiver="[email protected]"; 
    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("[email protected]"));; 
      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, "[email protected]", "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; 
    } 

錯誤:如何在JavaMail上發送Google的原始消息?

如果我保存使用Thunderbird EML文件,我該消息被正常發送。 但是,如果使用谷歌的show original message,我收到以下錯誤

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) 

例子:

雷鳥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+[email protected][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: <[email protected]> 
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== 

谷歌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+[email protected][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: <[email protected]> 
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== 

是谷歌的顯示原始郵件不是有效的EML?如何使它符合JavaMail EML解析器?

+0

我定你的帖子的格式;我希望這只是一個複製和粘貼錯誤。我沒有看到Content-Type值有任何問題,並且我無法用上面的示例消息重現錯誤。你如何閱讀文件中的消息?您在發送消息之前是否對消息進行了任何更改?你使用的是什麼版本的JavaMail? –

+0

x64 JVM for Windows(Oracle SDK)/ Netbeans上的JavaMail 1.4。 msg.setFrom(新的InternetAddress(「[email protected]」));'' – DeMarco

回答

1

您正在將原始郵件中的內容複製到新郵件中,並且您正在使用編碼作爲MIME類型,而不是這種情況,這就是您獲取異常的原因。 (谷歌與雷鳥不應該有區別就在這裏。)

嘗試,而不是這種簡單的版本:

 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("[email protected]"));; 
     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, "[email protected]", "1234"); 
相關問題