Mirza Ghalib
Mirza Ghalib

Reputation: 31

unable to send email to multiple users in java

I am using this code for sending email in java, this is working absolutely fine, but I want to send the email to the multiple Gmail IDs, for that I am doing something like this:

 Address toaddress[] = new InternetAddress[2];
            toaddress[0] = new InternetAddress("[email protected]");
            toaddress[1] = new InternetAddress("[email protected]");
            message.addRecipient(Message.RecipientType.TO,toaddress);

but this is not working, so please tell how can I send it to multiple Gmail IDs?

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 = "[email protected]";
    private String mail_from = "[email protected]";//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();
        }
    }
}

Upvotes: 0

Views: 894

Answers (1)

blackbishop
blackbishop

Reputation: 32650

If you want to add all addresses at once you'll have to use setRecipients() and addRecipients() not addRecipient(). Try with this :

message.addRecipients(Message.RecipientType.TO, 
                      InternetAddress.parse("[email protected], [email protected]"));

Note that you can parse all the addresses at once using InternetAddress.parse().

Or you may prefer to use an array of addresses just like this :

Address[] toaddress = new Address[] {InternetAddress.parse("[email protected]"),
                                     InternetAddress.parse("[email protected]")};

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

Upvotes: 1

Related Questions