简体   繁体   English

使用Javax邮件将已发送电子邮件保存到已发送邮件文件夹

[英]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:- 我对此有一些疑问:

  1. The email sending part works without password verification with ntlm. 电子邮件发送部分无需使用ntlm进行密码验证即可工作。 Is it possible to save the sent email into the sent items folder without password verification. 是否可以在不进行密码验证的情况下将已发送的电子邮件保存到已发送的邮件文件夹中。 If yes then how? 如果是,那怎么办?
  2. session.getStore doesnt work when i use a. session.getStore在我使用时不起作用。 smtp - exception (Invalid provider) b. smtp-异常(无效的提供程序)b。 ntlm - exception (NoSuchProviderException) what should i use here. ntlm-异常(NoSuchProviderException)我应该在这里使用。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM