1

Below is the java program which send a mail through java mail api now the issue is that I want to enter multiple addresses into mailTo field. In the below code, you can see there is single entry for mailTo that is avdq@abc.com. However, I want to pass multiple entries as avdq@abc.com, tvdq@abc.com, and pvdq@abc.com. Please advise how to achieve this.

public class abcMailTest {

            public static void main(String[] args) {

                String mailSmtpHost = "77.77.77.77";
                String mailSmtpPort = "4321" ;

                 String mailTo = "avdq@abc.com";
                //String mailCc = "avdg@abc.com ";
                String mailFrom = "avdg@abc.com";
                String mailSubject = "sgdtetrtrr";
                String mailText = "Test Mail for mail body ";
                sendEmail(mailTo,  mailFrom, mailSubject, mailText, mailSmtpHost ,mailSmtpPort );
            }

            public static void sendEmail(String to,  String from, String subject, String text, String smtpHost , String mailSmtpPort) {
                try {
                    Properties properties = new Properties();
                    properties.put("mail.smtp.host", smtpHost);
                    properties.put("mailSmtpPort", mailSmtpPort);

                    //obtaining the session 
                    Session emailSession = Session.getDefaultInstance(properties);
                    emailSession.setDebug(true);


                    //creating the message
                    Message emailMessage = new MimeMessage(emailSession);
                    emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                     Address[] cc = new Address[] {
                     new InternetAddress("avdg@abc.com"),
                     new InternetAddress("sWER@gmail.com")};
                     emailMessage.addRecipients(Message.RecipientType.CC, cc);
                     emailMessage.setFrom(new InternetAddress(from));
                     emailMessage.setSubject(subject);



                    // Create the message part
                     BodyPart messageBodyPart = new MimeBodyPart();
                     messageBodyPart.setContent(text, "text/html");
                     messageBodyPart.setText(text);


                    // Create a multipart message
                     Multipart multipart = new MimeMultipart();
                      multipart.addBodyPart(messageBodyPart);

                  // Part two is attachment
                     MimeBodyPart attachPart = new MimeBodyPart();
                     String filename = "c:\\abc.pdf";
                     DataSource source = new FileDataSource(filename);
                     attachPart.setDataHandler(new DataHandler(source));
                     attachPart.setFileName(filename);

                    multipart.addBodyPart(attachPart);

                     // Send the complete message parts
                     emailMessage.setContent(multipart);

                emailSession.setDebug(true);


                    Transport.send(emailMessage);
                }    catch (AddressException e) {
                    e.printStackTrace();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }
Nat
  • 3,587
  • 20
  • 22
ndsfd ddsfd
  • 235
  • 1
  • 5
  • 13
  • http://stackoverflow.com/questions/4320100/email-multiple-recipients-without-revealing-other-recipients this link has what you need – Alaa Abuzaghleh May 20 '15 at 04:43

2 Answers2

4

You just need to do the same thing as what you did for cc field

Address[] to = new Address[] {InternetAddress.parse("avdq@abc.com"),
                               InternetAddress.parse("tvdq@abc.com"), 
                               InternetAddress.parse("pvdq@abc.com")};
message.addRecipients(Message.RecipientType.TO, to);
Nat
  • 3,587
  • 20
  • 22
1

Try this

 String cc = "avdg@abc.com;sWER@gmail.com";
    StringTokenizer st = new StringTokenizer(cc,":");
    while(st.hasMoreTokens()) {
    emailMessage.addRecipients(Message.RecipientType.CC, InternetAddress.parse(st.nextToken(),false);
    }
arjun
  • 197
  • 1
  • 1
  • 12
  • or, if you want it in the TO header, as original asked, you do the same with "Message.RecipientType.TO" instead of "Message.RecipientType.CC". setTo(...) is basically not much more than a shortcut to addRecipients()... – rmalchow May 20 '15 at 05:03