gpt4 book ai didi

java - 使用线程在java中发送批量电子邮件?

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:45 26 4
gpt4 key购买 nike

嘿,我必须通过 Java 发送批量邮件。首先,我通过 smtpserver 完成了它,我能够发送邮件,但速度很慢。所以我搜索并使用线程来实现这一点。但是这些步骤所做的是,每次总是想要连接和验证时,它们都会向 smtp 服务器发送新的请求。 现在我的问题是如何请求服务器连接并仅进行一次身份验证[传输 Transport = session.getTransport("smtps");Transport.connect(主机、端口、用户名、密码);]

如何多次调用发送消息? [transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));]我尝试过如下

public class EmailService implements Runnable{
String customerMail="";
String messageType="";
String subject="";
String message="";
int customerMailId=0;
int campaignId=0;




@Override
public void run() {
try {
System.out.println("inside thread");
this.sendEmail();
} catch (Exception e) {
e.printStackTrace();
}
}

public void setParameters(String customerMail,String messageType,String subject,String message,int customerMailId,int campaignId) {
this.customerMail=customerMail;
this.messageType=messageType;
this.subject=subject;
this.message=message;
this.customerMailId=customerMailId;
this.campaignId=campaignId;
}
final String username = "**********";
final String password = "*******";
String host = "smtp.gmail.com";
String password = "******";
int port = 465;

public void sendEmail() {
System.out.println("inside emailservice");

Properties props = new Properties();
props.put("mail.smtp.username", username);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", port);
props.put("mail.smtp.host", host);
props.put("mail.transport.protocol.", "smtps");
props.put("mail.smtps.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

Transport transport = session.getTransport("smtps");
transport.connect(host, port, username, password);

if (messageType.equals("TO")) {

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(username));
msg.setSubject(subject);
System.out.println("inside to method");
msg.setRecipients(Message.RecipientType.TO, customerMail);
int mailingId = customerMailId;

message = message.replaceAll(".jpeg", ".jpeg?&campId=" + campaignId + "&opId=" + mailingId);
message = message.replaceAll(".JPEG", ".jpeg?&campId=" + campaignId + "&opId=" + mailingId);
message = message.replaceAll(".jpg", ".jpg?&campId=" + campaignId + "&opId=" + mailingId);
message = message.replaceAll(".JPG", ".jpg?&campId=" + campaignId + "&opId=" + mailingId);
message = message.replaceAll(".png", ".png?&campId=" + campaignId + "&opId=" + mailingId);
message = message.replaceAll(".PNG", ".PNG?&campId=" + campaignId + "&opId=" + mailingId);
String msgBody = "<a href=http://wmail.*****.in/?&campId=" + campaignId
+ "&clkId=" + mailingId + ">" + message + "</a>" + "<br><br>" + "<b>To stop receiving these emails please" + "<a href=http://wmail.****.in?&campId=" + campaignId
+ "&unsubsId=" + mailingId + ">" + " click here </a>" + "to unsubscribe.</b>";
msg.setContent(msgBody, "text/html; charset=UTF-8");
transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO)); }
System.out.println("inside sucees");
} catch (MessagingException e) {
e.printStackTrace();
}

}
public void MailSender(List<NameEmailRecord> receiverEmail, String messageType, String subject, String message, int campaignId)
{
String mail="";
int mailId=0;
for (NameEmailRecord ner : receiverEmail) {
mail=ner.getMailingEmail();
mailId=ner.getMailingEmailId();
EmailService es = new EmailService(); es.setParameters(mail,messageType,subject,message,mailId,campaignId);
Thread t = new Thread(es);
t.start();
}
}

}

最佳答案

SMTP 连接发生在套接字上。因此,对于与任何 SMTP 服务器的一次连接,一次只能发送一个消息(即单个消息,每条消息可能有多个收件人)。

我想在你的情况下,这意味着在登录成本和每个线程的电子邮件数之间找到一个很好的折衷方案。

要通过一个传输发送多封电子邮件(通过同一 SMTP 服务器发送),您只需创建一个传输并重用它,为每封要发送的消息调用 sendMessage。

关于java - 使用线程在java中发送批量电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34086168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com