gpt4 book ai didi

java - 无法在java中向多个用户发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 12:38:26 26 4
gpt4 key购买 nike

我使用这个代码在java中发送电子邮件,这工作得很好,但我想将电子邮件发送到多个Gmail ID,为此我正在做这样的事情:

 Address toaddress[] = new InternetAddress[2];
toaddress[0] = new InternetAddress("yyy@gmail.com");
toaddress[1] = new InternetAddress("kkk@gmail.com");
message.addRecipient(Message.RecipientType.TO,toaddress);

但这不起作用,所以请告诉我如何将其发送到多个 Gmail ID?

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class Email_Autherticator extends Authenticator {
String username = "xyz";
String password = "abc";

public Email_Autherticator() {
super();
}
public Email_Autherticator(String user,String pwd){
super();
username = user;
password = pwd;
}

public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
}



class Mail {
private String mail_to = "13besejahmed@seecs.edu.pk";
private String mail_from = "mgagmdc@gmail.com";//using gmail server
private String mail_subject = "this is the subject of this test mail";
private String mail_body = "this is mail_body of this test mail";
private String personalName = "Mirza";

public static void main(String arg[]) throws SendFailedException {
new Mail();
}

public Mail() throws SendFailedException {
sendMail();
}

public void sendMail() throws SendFailedException{
try {
Authenticator auth = new Email_Autherticator();
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host", "smtp.gmail.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.user", "xyz");
properties.setProperty("mail.smtp.password", "abc");
Session session = Session.getDefaultInstance(properties,auth);

MimeMessage message = new MimeMessage(session);
message.setSubject(mail_subject);
message.setText(mail_body);
Address address = new InternetAddress(mail_from,personalName);
message.setFrom(address);

Address toaddress = new InternetAddress(mail_to);
message.addRecipient(Message.RecipientType.TO,toaddress);

Transport.send(message);
System.out.println("Send Mail Ok!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}

最佳答案

如果您想一次添加所有地址,则必须使用 setRecipients()addRecipients() 而不是 addRecipient() 。试试这个:

message.addRecipients(Message.RecipientType.TO, 
InternetAddress.parse("yyy@gmail.com, kkk@gmail.com"));

请注意,您可以使用 InternetAddress.parse() 一次解析所有地址。

或者您可能更喜欢使用像这样的地址数组:

Address[] toaddress = new Address[] {InternetAddress.parse("yyy@gmail.com"),
InternetAddress.parse("kkk@gmail.com")};

message.addRecipients(Message.RecipientType.TO, toaddress );

关于java - 无法在java中向多个用户发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25349707/

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