2011-07-14 34 views
6

Hiện tại tôi đang sử dụng Commons Email để gửi thư email, nhưng tôi không thể tìm cách chia sẻ kết nối smtp giữa các email được gửi. Tôi có mã như sau:Email của Apache Commons và sử dụng lại các kết nối 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(); 

Điều rất dễ đọc, nhưng chậm khi tôi thực hiện một lượng lớn thư, tôi tin là phí kết nối lại cho mỗi thư. Vì vậy, tôi đã lược tả nó bằng đoạn mã sau đây và nhận thấy rằng việc sử dụng lại việc vận chuyển sẽ giúp mọi thứ nhanh hơn gấp ba lần.

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()); 

Vì vậy, tôi đã tự hỏi liệu có cách nào để làm cho Email Commons sử dụng lại kết nối SMTP để gửi nhiều email không? Tôi thích API Email của Commons tốt hơn, nhưng hiệu suất là loại đau đớn.

Cảm ơn, Ransom

Trả lời

3

tôi đã đưa ra các giải pháp sau đây sau khi đào vào nguồn commons riêng của mình. Điều này sẽ làm việc, nhưng có thể có giải pháp tốt hơn tôi không biết

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

Chúng ta có thể không đạt được điều này dễ dàng hơn bằng cách phiên mail từ email đầu tiên sử dụng getMailSession() và đưa nó vào tất cả các thư tiếp theo sử dụng setMailSession()?

Không chắc chắn 100% những gì

Xin lưu ý rằng thông qua một tên người dùng và mật khẩu (trong trường hợp xác thực mail) sẽ tạo ra một phiên thư mới với một DefaultAuthenticator. Đây là một sự kiên nhẫn nhưng có thể đến bất ngờ. Nếu xác thực thư được sử dụng nhưng KHÔNG tên người dùng và mật khẩu được cung cấp, việc triển khai giả định rằng bạn đã đặt trình xác thực và sẽ sử dụng phiên thư hiện tại (như mong đợi).

từ javadoc nghĩa, mặc dù: -/ http://commons.apache.org/email/api-release/org/apache/commons/mail/Email.html#setMailSession%28javax.mail.Session%29

xem thêm: https://issues.apache.org/jira/browse/EMAIL-96

không chắc chắn làm thế nào để tiếp tục ở đây ...

Các vấn đề liên quan