[英]Save Sent email to sent items folder using javax mail
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);
}
}
When I try to execute the code above, i am able to send out the emails. 当我尝试执行上面的代码时,我能够发送电子邮件。 the issue is with saving the email. 问题在于保存电子邮件。 I get an error (NoSuchProviderException) at the line Store store = session.getStore("ntlm"); 我在Store store = session.getStore(“ ntlm”);行中收到错误(NoSuchProviderException);
I have a few questions on this:- 我对此有一些疑问:
Thanks in advance for your help. 在此先感谢您的帮助。
"ntlm" is not a type of Store, it's an authentication mechanism. “ ntlm”不是商店的一种,它是一种身份验证机制。 The store types supported by JavaMail are "imap" and "pop3". JavaMail支持的商店类型为“ imap”和“ pop3”。 You almost certainly want "imap". 您几乎可以肯定需要“ imap”。 Just like sending, you're going to need to supply your username and password when connecting to your imap server. 就像发送一样,在连接到imap服务器时,您将需要提供用户名和密码。
Also, upgrade to the current version of JavaMail if possible. 另外,如果可能,请升级到JavaMail的当前版本 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.