gpt4 book ai didi

java - 在 Java 应用程序中通过亚马逊服务器上的 JavaMail API 发送带有附件的邮件时出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 11:20:50 25 4
gpt4 key购买 nike

使用 javamail API 在亚马逊服务器中发送带有 pdf 附件的邮件时,它在日志中抛出空指针异常。但是相同的代码在本地运行。

public void sendMail(final String mailTo, final String mailSubject, final String mailText, final String filePath, final String fileName) {
logger.info("Inside sendMail Method...");
final Properties config = createConfiguration();

// Creates a mail session. We need to supply username and password for Gmail authentication.
final Session session = Session.getInstance(config, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailFrom, mailPassword);
}
});

// Creates email message
final MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(mailFrom));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
message.setSubject(mailSubject);

final BodyPart messagePart = new MimeBodyPart();
messagePart.setContent(mailText, contentType);

final MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);

if (filePath != null) {
final MimeBodyPart attachmentPart = new MimeBodyPart();
final URL url;
try {
url = new URL(filePath);
final DataSource source = new URLDataSource(url);
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName(fileName);
multipart.addBodyPart(attachmentPart);
} catch (MalformedURLException e) {
logger.error("Malformed URL Exception: " + e.getMessage());
}
}
message.setContent(multipart);
// Send a message
Transport.send(message);
logger.info("Mail triggered successfully");
} catch (final AddressException e) {
logger.error("Address Exception: " + e.getMessage());
} catch (final MessagingException e) {
logger.error("Messaging Exception: " + e.getMessage());
}
}

请在下面找到在 amozon 服务器应用程序日志上生成的异常。

2014-03-20 19:01:30,936 [DefaultQuartzScheduler_Worker-2] INFO            net.app.api.jobs.MailJob - Error in triggering the mail : null
java.lang.NullPointerException
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
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:123)
at net.app.api.mail.MailTrigger.sendMail(MailTrigger.java:104)
at net.app.api.jobs.MailJob.execute(MailJob.java:41)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)

任何人请为此提供一些可行的解决方案。提前致谢。

最佳答案

问题可能是您无法访问filePath中的URL。

filePath 是“file:”URL 吗?如果是这样,为什么不直接使用 FileDataSource?

关于java - 在 Java 应用程序中通过亚马逊服务器上的 JavaMail API 发送带有附件的邮件时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com