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

时间:2014-08-17 13:39:33

标签: java javamail

我正在使用此代码在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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果您想一次性添加所有地址,则必须使用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 );