java - how to send a mail to group of person by using Javamail -


can tell me how send mail group of person using java mail. have tried getting error @ sendemailtogroupdemo , @ start() method. thanks

package textfiles; import java.util.arraylist;  import java.util.list;  import java.util.properties;  import java.util.scanner;  import javax.mail.address;  import javax.mail.authenticator;  import javax.mail.message;  import javax.mail.messagingexception;  import javax.mail.passwordauthentication;  import javax.mail.session;  import javax.mail.transport;  import javax.mail.internet.addressexception;  import javax.mail.internet.internetaddress;  import javax.mail.internet.mimemessage;  public class mailjava {   public static void main(string[] args) {        // create sendemail object , call start        // method send mail in java.       sendemailtogroupdemo sendemailtogroup = new sendemailtogroupdemo();       sendemailtogroup.start();       }       private void start() {        // establishment of email client        // google's gmail use below properties.       // tls connection use below properties       // create properties object        properties props = new properties();        // these properties required       // providing smtp auth property true       props.put("mail.smtp.auth", "true");       // providing tls enability       props.put("mail.smtp.starttls.enable", "true");       // providing smtp host i.e gmail.com       props.put("mail.smtp.host", "smtp.gmail.com");       // providing smtp port 587       props.put("mail.smtp.port", "587");        // ssl connection use below properties        /*props.put("mail.smtp.host", "smtp.gmail.com");        props.put("mail.smtp.socketfactory.port", "465");        props.put("mail.smtp.socketfactory.class",          "javax.net.ssl.sslsocketfactory");        props.put("mail.smtp.auth", "true");        props.put("mail.smtp.port", "465");*/        // create scanner object take necessary        // values user.       scanner scanner = new scanner(system.in);        system.out.println("please provide username authentication ...");       final string username = scanner.next();        system.out.println("please provide password authentication ...");       final string password = scanner.next();        system.out.println("please provide email address want send email ...");       final string fromemailaddress = scanner.next();        system.out.println("please provide email addresses want send email ...");       system.out.println("if done type : done or done");        // arraylists store email addresses entered user       arraylist< string> emails = (arraylist< string >) getemails();        system.out.println("please provide subject email ... ");       final string subject = scanner.next();        system.out.println("please provide text message email ... ");       final string textmessage = scanner.next();        // create session object based on properties ,        // authenticator object       session session = session.getdefaultinstance(props,          new loginauthenticator(username,password));        try {         // create message object using session created above        message message = new mimemessage(session);         // setting email address message message being sent        message.setfrom(new internetaddress(fromemailaddress));         // setting email addressess user wants send message         message.setrecipients(message.recipienttype.bcc, getemailslist(emails));         // setting subject email         message.setsubject(subject);         // setting text message user wants send recipients        message.settext(textmessage);         // using transport class send() method send message         transport.send(message);         system.out.println("\nyour message delivered ....");        } catch (messagingexception e) {         throw new runtimeexception(e);        }        }       // method takes list of email addresses ,       // returns array of address looping       // list 1 one , storing address[]      private address[] getemailslist(arraylist< string > emails) {        address[] emaiaddresses = new address[emails.size()];        (int =0;i < emails.size();i++) {        try {         emaiaddresses[i] = new internetaddress(emails.get(i));        }        catch (addressexception e) {          e.printstacktrace();        }       }       return emaiaddresses;      }       // method prompts user email group      // wants send message      public list< string > getemails() {       arraylist< string > emails = new arraylist< string >();        int counter = 1;       string address = "";       scanner scanner = new scanner(system.in);        // looping inifinitely times long user enters        // emails 1 one       // while loop breaks when user types done ,        // press enter.       while(true) {         system.out.println("enter e-mail : " + counter);        address = scanner.next();         if(address.equalsignorecase("done")){         break;        }        else {         emails.add(address);         counter++;        }       }        return emails;      }     }      // creating class username , password authentication     // provided user.     class loginauthenticator extends authenticator {      passwordauthentication authentication = null;       public loginauthenticator(string username, string password) {       authentication = new passwordauthentication(username,password);      }       @override      protected passwordauthentication getpasswordauthentication() {       return authentication;      }     } 

replace this:

   // setting email addressess user wants send message     message.setrecipients(message.recipienttype.bcc, getemailslist(emails)); 

with this:

   // setting email addressess user wants send message    (string emailbcc : emails) {        message.addrecipient(message.recipienttype.bcc, new internetaddress(emailbcc));    } 

i've had problems setrecipients() before, never addrecipient().

if keep having issues, we'll need stacktrace exact error / exception you're getting.


Comments