我正在尝试使用此方法发送附带hello.json
文件的电子邮件。但是当我构建并运行代码时,电子邮件会被发送,但是附件从未通过电子邮件发送。我缺少的MultPartEmail API有什么特别之处吗?我的代码基于其他示例。
public void sendNotification(String from, String pass, String host, int port, String[] to,
String subject, String body) throws EmailException {
log.trace("Beginning sendNotification function");
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("C:/temp/hello.json");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName("user");
attachment.setDescription("external json file");
File f = new File("C:/temp/hello.json");
MultiPartEmail gmail = new MultiPartEmail();
gmail.setSmtpPort(port);
gmail.setHostName(host);
gmail.setAuthentication(from, pass);
gmail.setSSLOnConnect(true);
gmail.setFrom(from);
gmail.setSubject(subject);
gmail.setContent(body, "text/html; charset=utf-8");
gmail.addTo(to);
if (f.exists() && !f.isDirectory()) {
gmail.attach(attachment);
} else {
log.error(String.format("%s does not exist.", attachment));
}
gmail.send();
}