in Education by
Can anybody tell me how to send a mail to group of person by using JavaMail? I have tried but I am getting error at SendEmailToGroupDemo() and at start() method. public class MailJava { public static void main(String[] args) { // Create a SendEmail object and call start // method to send a mail in Java. SendEmailToGroupDemo sendEmailToGroup = new SendEmailToGroupDemo(); sendEmailToGroup.start(); } private void start() { // For establishment of email client with // Google's gmail use below properties. // For TLS Connection use below properties // Create a Properties object Properties props = new Properties(); // these properties are required // providing smtp auth property to true props.put("mail.smtp.auth", "true"); // providing tls enability props.put("mail.smtp.starttls.enable", "true"); // providing the smtp host i.e gmail.com props.put("mail.smtp.host", "smtp.gmail.com"); // providing smtp port as 587 props.put("mail.smtp.port", "587"); // For 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 to take necessary // values from the user. Scanner scanner = new Scanner(System.in); System.out.println("Please provide your Username for Authentication ..."); final String Username = scanner.next(); System.out.println("Please provide your Password for Authentication ..."); final String Password = scanner.next(); System.out.println("Please provide Email Address from which you want to send Email ..."); final String fromEmailAddress = scanner.next(); System.out.println("Please provide Email Addresses to which you want to send Email ..."); System.out.println("If you are done type : Done or done"); // ArrayLists to store email addresses entered by user ArrayList< String> emails = (ArrayList< String >) getEmails(); System.out.println("Please provide Subject for your Email ... "); final String subject = scanner.next(); System.out.println("Please provide Text Message for your Email ... "); final String textMessage = scanner.next(); // Create a Session object based on the properties and // Authenticator object Session session = Session.getDefaultInstance(props, new LoginAuthenticator(Username,Password)); try { // Create a Message object using the session created above Message message = new MimeMessage(session); // setting email address to Message from where message is being sent message.setFrom(new InternetAddress(fromEmailAddress)); // setting the email addressess to which user wants to send message message.setRecipients(Message.RecipientType.BCC, getEmailsList(emails)); // setting the subject for the email message.setSubject(subject); // setting the text message which user wants to send to recipients message.setText(textMessage); // Using the Transport class send() method to send message Transport.send(message); System.out.println("\nYour Message delivered successfully ...."); } catch (MessagingException e) { throw new RuntimeException(e); } } // This method takes a list of email addresses and // returns back an array of Address by looping the // list one by one and storing it into Address[] private Address[] getEmailsList(ArrayList< String > emails) { Address[] emaiAddresses = new Address[emails.size()]; for (int i =0;i < emails.size();i++) { try { emaiAddresses[i] = new InternetAddress(emails.get(i)); } catch (AddressException e) { e.printStackTrace(); } } return emaiAddresses; } // This method prompts user for email group to which he // wants to 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 as long as user enters // emails one by one // the while loop breaks when user types done and // 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 a class for Username and Password authentication // provided by the 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; } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Replace this: // setting the email addressess to which user wants to send message message.setRecipients(Message.RecipientType.BCC, getEmailsList(emails)); With this: // setting the email addressess to which user wants to send message for (String emailBcc : emails) { message.addRecipient(Message.RecipientType.BCC, new InternetAddress(emailBcc)); } I've had problems with setRecipients() before, but never with addRecipient(). If you keep having issues, we'll need the stacktrace with the exact error / exception you're getting.

Related questions

0 votes
    In which of the following, a person is constantly followed/chased by another person or group of several peoples? Phishing Bulling Stalking Identity theft...
asked Mar 4, 2021 in Technology by JackTerrance
0 votes
    Which capturing group can represent the entire expression? (a) group * (b) group 0 (c) group * or ... Regular Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    What does public int end(int group) return? (a) offset from last character of the subsequent group (b) ... Regular Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Anita has to send an email to Raees. She also wants to send the same e-mail to Vandana but does not want ... email address of Vandana? Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    Which one of the following statements is false? a) You need to create an account before you can send an e ... password with others. Select the correct answer from above options...
asked Nov 13, 2021 in Education by JackTerrance
0 votes
0 votes
    Which of these class is related to all the exceptions that can be caught by using catch? (a) Error (b) ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these operator is used to generate an instance of an exception than can be thrown by using throw? ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    How to read entire file in one line using java 8? (a) Files.readAllLines() (b) Files.read() (c) ... Autoboxing & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    How private method can be called using reflection? (a) getDeclaredFields (b) getDeclaredMethods (c) getMethods (d) ... API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    How private field can be called using reflection? (a) getDeclaredFields (b) getDeclaredMethods (c) getMethods (d) ... API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    How to get the class object of associated class using Reflection? (a) Class.forName( className ) (b) Class.name( ... JSP & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I have written tests with Selenium2/WebDriver and want to test if HTTP Request returns an HTTP 403 Forbidden. ... Selenium WebDriver? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Need to read my SMTP email settings defined under system.net section in my web.config file. Below is one example of SMTP email setting defined in web.config file: (Under Section)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Basically i have a java String which i want to restrict all the characters other than this regex code ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
...