无法在java中向多个用户发送电子邮件
我使用java发送电子邮件的代码,工作非常好,但我想将电子邮件发送到多个Gmail ID,为此我做了如下工作:无法在java中向多个用户发送电子邮件,java,jakarta-mail,Java,Jakarta Mail,我使用java发送电子邮件的代码,工作非常好,但我想将电子邮件发送到多个Gmail ID,为此我做了如下工作: Address toaddress[] = new InternetAddress[2]; toaddress[0] = new InternetAddress("yyy@gmail.com"); toaddress[1] = new InternetAddress("kkk@gmail.com"); messa
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()
而不是addRecipients()
。试试这个:
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 );