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;
}
错误:
如果我使用Thunderbird保存EML文件,则该消息是正常发送的。但是,如果使用Google的显示原始消息,我会收到以下错误
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+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==
Google的显示原始消息不是有效的EML吗?如何使其符合Javamail EML解析器?
您正在将内容从原始消息复制到新消息,并且正在使用编码为Mime类型,这就是为什么您要获得的原因例外。(Google vs. Thunderbird不应在这里有所作为。(
尝试此简单版本:
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");