【问题标题】:How to send mails to multiple recipients through java mail api [duplicate]如何通过java mail api将邮件发送给多个收件人[重复]
【发布时间】:2015-05-20 04:35:07
【问题描述】:

下面是通过java mail api发送邮件的java程序现在的问题是我想在mailTo字段中输入多个地址。在下面的代码中,您可以看到 mailTo 的单个条目是 avdq@abc.com。但是,我想将多个条目作为 avdq@abc.com、tvdq@abc.com 和 pvdq@abc.com 传递。请告知如何实现这一目标。

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();
                }
            }
        }

【问题讨论】:

标签: java jakarta-mail


【解决方案1】:

你只需要和你对 cc 字段所做的一样

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);

【讨论】:

    【解决方案2】:

    试试这个

     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);
        }
    

    【讨论】:

    • 或者,如果您希望它在 TO 标头中,如原始要求的那样,您可以使用“Message.RecipientType.TO”而不是“Message.RecipientType.CC”。 setTo(...) 基本上不过是 addRecipients() 的快捷方式...
    猜你喜欢
    • 2014-12-15
    • 2017-09-26
    • 2013-09-02
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2014-03-24
    • 1970-01-01
    相关资源
    最近更新 更多