2011-07-14 5 views
6

現在、私はCommons Emailを使用して電子メールメッセージを送信していますが、送信された電子メール間のsmtp接続を共有する方法を見つけることができませんでした。非常に読みやすいですが、私は、各メッセージのために再接続のオーバーヘッドであると信じてメッセージを大量に行うときに遅いApache Commons電子メールとSMTP接続の再利用

Email email = new SimpleEmail(); 
    email.setFrom("[email protected]"); 
    email.addTo("[email protected]"); 
    email.setSubject("Hello Example"); 
    email.setMsg("Hello Example"); 
    email.setSmtpPort(25); 
    email.setHostName("localhost"); 
    email.send(); 

:私は、次のようなコードを持っています。そこで私は以下のコードでそれをプロファイリングし、Transportを再利用することで、約3倍高速になります。

Properties props = new Properties(); 
    props.setProperty("mail.transport.protocol", "smtp"); 
    Session mailSession = Session.getDefaultInstance(props, null); 
    Transport transport = mailSession.getTransport("smtp"); 
    transport.connect("localhost", 25, null, null); 

    MimeMessage message = new MimeMessage(mailSession); 
    message.setFrom(new InternetAddress("[email protected]")); 
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); 
    message.setSubject("Hello Example"); 
    message.setContent("Hello Example", "text/html; charset=ISO-8859-1"); 

    transport.sendMessage(message, message.getAllRecipients()); 

Commons Emailを複数のメール送信に再利用する方法があるのだろうかと思っていましたか? Commons Email APIの方が気に入っていますが、パフォーマンスは苦しいです。

おかげで、 身代金

答えて

3

私はコモンズソース自体に掘った後、次の解決策を考え出しました。これは、我々は(getMailSessionを使用して最初のメールからメールセッションを取得)とsetMailSessionを使用して後続のすべてのメッセージにそれを置くことによってこれを簡単に実現することができませんでした動作するはずですが、私は

Properties props = new Properties(); 
    props.setProperty("mail.transport.protocol", "smtp"); 
    Session mailSession = Session.getDefaultInstance(props, null); 
    Transport transport = mailSession.getTransport("smtp"); 
    transport.connect("localhost", 25, null, null); 

    Email email = new SimpleEmail(); 
    email.setFrom("[email protected]"); 
    email.addTo("[email protected]"); 
    email.setSubject("Hello Example"); 
    email.setMsg("Hello Example"); 
    email.setHostName("localhost"); // buildMimeMessage call below freaks out without this 

    // dug into the internals of commons email 
    // basically send() is buildMimeMessage() + Transport.send(message) 
    // so rather than using Transport, reuse the one that I already have 
    email.buildMimeMessage(); 
    Message m = email.getMimeMessage(); 
    transport.sendMessage(m, m.getAllRecipients()); 
1

のか分からない、より良い解決策があるかもしれません()?

は(メール認証の場合)、ユーザ名とパスワードを渡すと、DefaultAuthenticatorを使用して新しいメールセッションを作成しますので、予めご了承ください何

ない100%を確認してください。これは意識ですが、予期しないことがあります。メール認証が使用されているにもかかわらず、ユーザー名とパスワードが指定されていない場合、実装では認証者を設定し、既存のメールセッションを使用します(想定どおり)。 javadocの手段から

、しかし: -/ http://commons.apache.org/email/api-release/org/apache/commons/mail/Email.html#setMailSession%28javax.mail.Session%29

も参照してください。 https://issues.apache.org/jira/browse/EMAIL-96

ないここに継続する方法がわから...

関連する問題