2017-03-06 15 views
0
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");保存メールを送信済みのアイテムフォルダにメールを送信しました

私はこの上のいくつかの質問があります: -

  1. メール送信部は、NTLMとパスワード検証せずに動作しますが。送信された電子メールをパスワード検証なしで送信済みアイテムフォルダに保存することは可能ですか?はいの場合はどうですか?
  2. 私は を使用するとsession.getStoreが機能しません。 smtp - 例外(無効なプロバイダ) b。 ntlm - 例外(NoSuchProviderException) ここで何を使用しますか?

ご協力いただきありがとうございます。

+0

javax.mail jarのバージョンは何ですか? – Maverick

+0

パッケージjavax.mail; クラスバージョン{ public static final String version = "1.4.6"; } – hnsanadhya

+0

my jarの名前はmailapi.jarで、LICENSE.txtファイルのmeta-infの下に表示されるバージョンは1.0です – hnsanadhya

答えて

0

「ntlm」はStoreのタイプではなく、認証メカニズムです。 The store types supported by JavaMail are "imap" and "pop3". "imap"がほしいと思うことはほとんどありません。送信のように、あなたはあなたのユーザ名とパスワードをあなたのimapサーバに接続するときに供給する必要があります。

また、できるだけ​​にアップグレードしてください。

関連する問題