java 如何通过java mail api向多个收件人发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30340352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:56:39  来源:igfitidea点击:

How to send mails to multiple recipients through java mail api

javajavamail

提问by ndsfd ddsfd

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 [email protected]. However, I want to pass multiple entries as [email protected], [email protected], and [email protected]. Please advise how to achieve this.

下面是通过java mail api发送邮件的java程序,现在问题是我想在mailTo字段中输入多个地址。在下面的代码中,您可以看到 mailTo 有一个条目,即 [email protected]。但是,我想以 [email protected][email protected][email protected] 的形式传递多个条目。请告知如何实现这一点。

public class abcMailTest {

            public static void main(String[] args) {

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

                 String mailTo = "[email protected]";
                //String mailCc = "[email protected] ";
                String mailFrom = "[email protected]";
                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("[email protected]"),
                     new InternetAddress("[email protected]")};
                     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();
                }
            }
        }

回答by Nat

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

你只需要做与你对 cc 字段所做的相同的事情

Address[] to = new Address[] {InternetAddress.parse("[email protected]"),
                               InternetAddress.parse("[email protected]"), 
                               InternetAddress.parse("[email protected]")};
message.addRecipients(Message.RecipientType.TO, to);

回答by arjun

Try this

试试这个

 String cc = "[email protected];[email protected]";
    StringTokenizer st = new StringTokenizer(cc,":");
    while(st.hasMoreTokens()) {
    emailMessage.addRecipients(Message.RecipientType.CC, InternetAddress.parse(st.nextToken(),false);
    }