我根本不理解这个问题。如果我在作为java应用程序运行时尝试使用main方法,那么邮件将完美地与正确的主题和内容格式化。 当我从localhost尝试时,它会以破碎的格式出现,例如
------ = _ Part_0_1765202668.1460463643056 Content-Type:text / html; charset = utf-8 Content-Transfer-Encoding:7bit
我的内容
------ = _ Part_0_1765202668.1460463643056 -
我添加了所有相关的jar(javax.mail)。无论内容是什么,它都只是这样。它是同一段代码,它可以从主要方法中运行良好但与本地主机无关。 任何想法?
一些相关代码
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(this.from));
if ((this.replyTo != null) && (!this.replyTo.equals("")))
msg.setReplyTo(InternetAddress.parse(this.replyTo));
msg.setSentDate(new Date());
InternetAddress[] address = InternetAddress.parse(this.to);
msg.setRecipients(Message.RecipientType.TO, address);
if (this.cc != null) {
InternetAddress[] address1 = InternetAddress.parse(this.cc);
msg.setRecipients(Message.RecipientType.CC, address1);
}
if (this.bcc != null) {
InternetAddress[] address2 = InternetAddress.parse(this.bcc);
msg.setRecipients(Message.RecipientType.BCC, address2);
}
msg.setSubject(this.subject);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(this.body,"text/html;charset=utf-8");
mp.addBodyPart(mbp);
if (this.attachfiles != null) {
for (Enumeration e = this.attachfiles.keys(); e.hasMoreElements();) {
String filename = (String) e.nextElement();
mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(
(String) this.attachfiles.get(filename));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(filename);
mp.addBodyPart(mbp);
}
}
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
答案 0 :(得分:2)
pom.xml存在问题 事实证明包装中存在冲突。 Tomcat自动在Web项目中的另外两个Jars的Maven构建中包含自己的JavaMail包,导致问题而不是从标准JavaMail jar导入。
只需排除以下广告
即可 <dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.7</version>
<exclusions>
<exclusion>
<artifactId>geronimo-javamail_1.4_spec</artifactId>
<groupId>org.apache.geronimo.specs</groupId>
</exclusion>
<exclusion>
<artifactId>geronimo-activation_1.1_spec</artifactId>
<groupId>org.apache.geronimo.specs</groupId>
</exclusion>
</exclusions>
</dependency>