public static void sendEmail(String msgHeader, String msg, String emailId, String emailFrom) {
Properties props = new Properties();
props.put("mail.smtp.auth", "false");
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", mailServer);
props.put("mail.smtp.port", port#);
props.put("mail.smtp.auth.mechanisms", "NTLM");
props.put("mail.smtp.auth.ntlm.domain", domainName);
Session session = Session.getDefaultInstance(props, null);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailFrom));
to = emailId;
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(msgHeader);
message.setText(msg, "utf-8", "html");
message.saveChanges();
session.getDebug();
Transport.send(message);
// Copy message to "Sent Items" folder as read
Store store = session.getStore("ntlm");
store.connect(mailServer, emailFrom, pwd);
Folder folder = store.getFolder("Sent Items");
folder.open(Folder.READ_WRITE);
message.setFlag(Flag.SEEN, true);
folder.appendMessages(new Message[] {message});
store.close();
} catch (Exception ex) {
logger.error("Error occured while sending Email !", ex);
}
}
当我尝试执行上面的代码时,我可以发送电子邮件。问题在于保存电子邮件。我在这一行得到一个错误(NoSuchProviderException) Store store = session.getStore(“ntlm”);
我对此有几个问题: -
提前感谢您的帮助。
答案 0 :(得分:0)
“ntlm”不是Store的类型,它是一种身份验证机制。 The store types supported by JavaMail are "imap" and "pop3".你几乎肯定想要“imap”。就像发送一样,在连接到imap服务器时,您需要提供用户名和密码。
此外,如果可能,请升级到current version of JavaMail。