提高Java邮件的发送速度

·  阅读 144

我用Java编写了一个代码,使用Java邮件通过SMTP服务器发送邮件。发件人和收件人的电子邮件地址以及邮件的主题和正文保存在数据库(SQL)中。然而,以这种方式发送邮件是相当缓慢的。我必须发送大量邮件(大约300封邮件/秒)。如何提高使用多线程发送邮件的速度?

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import oracle.jdbc.OraclePreparedStatement;

public class mainclass {

Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;

public static void main(String args[]) throws AddressException,
        MessagingException {
            OraclePreparedStatement pst;
            Connection con;
            String userna;
            con = connectDB.connect();
            try {
                    Statement st = con.createStatement();
                    String sm = "SELECT * FROM EmailQueue WHERE sent = 'no'";
                    ResultSet res = st.executeQuery(sm);
                    while (res.next()) {
                        System.out.println(res.getString("from_email_address") + " " + res.getString("to_email_address"));
                        String id = res.getString("id");
                        String toEmails = res.getString("to_email_address");
                        String emailSubject = res.getString("subject");
                        String emailBody = res.getString("body");
                        String emailHost = "smtp.gmail.com";
                        String fromUser = res.getString("from_email_address");
                        String fromUserEmailPassword = res.getString("password");
                        mainclass javaEmail = new mainclass();
                        javaEmail.setMailServerProperties();
                        javaEmail.createEmailMessage(toEmails, emailSubject, emailBody);
                        javaEmail.sendEmail(emailHost, fromUser, fromUserEmailPassword);
                        Statement sn = con.createStatement();
                        String up = "Update EmailQueue set sent='yes' where id='"+id+"'";
                        sn.executeQuery(up);
                    }

        } catch(Exception e) {
            System.out.println("problem");
        }

}

public void setMailServerProperties() {

    String emailPort = "587";//gmail's smtp port

    emailProperties = System.getProperties();
    emailProperties.put("mail.smtp.port", emailPort);
    emailProperties.put("mail.smtp.auth", "true");
    emailProperties.put("mail.smtp.starttls.enable", "true");

}

public void createEmailMessage(String toEmails, String emailSubject, String emailBody) throws AddressException,
        MessagingException {

    mailSession = Session.getDefaultInstance(emailProperties, null);
    emailMessage = new MimeMessage(mailSession);

        emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails));

    emailMessage.setSubject(emailSubject);
    emailMessage.setContent(emailBody, "text/html");//for a html email
    //emailMessage.setText(emailBody);// for a text email

}

public void sendEmail(String emailHost, String fromUser, String fromUserEmailPassword) throws AddressException, MessagingException {

    Transport transport = mailSession.getTransport("smtp");

    transport.connect(emailHost, fromUser, fromUserEmailPassword);
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
    transport.close();
    System.out.println("Email sent successfully.");
}

}
复制代码

多线程--发送邮件的过程--只会使您的邮件服务器出现数千个请求。

当您发送电子邮件(例如JavaMail)时,您只是向您的邮件服务器提交一封新邮件。实际上,是邮件服务器将邮件发送给收件人。在Java端,您无法提高交付速度。

分类:
后端
标签:
分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改