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