JavaMail - 循环发送多封电子邮件

时间:2015-02-06 10:41:42

标签: java javamail

我的任务是编写一个应用程序,该应用程序将向1-3个收件人发送电子邮件,其中包含5个可能的调查问题。问题和收件人是从属性文件中提取的,要发送的电子邮件数量也保存在此文件中。

我的问题是,我无法收到使用此当前代码发送的电子邮件,是否有任何建议?

EmailSend

for(int j=0; j<noOfEmails; j++)
            {
                if(noOfEmails==1)
                {
                    for(int i=0; i<noOfQuest; i++)
                    {
                        message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient1));
                        if(noOfQuest==1)
                        {
                            message.setText("Username: " + fName + " "+ sName + "\n" +
                                                "Response 1: " + answers[0]);
                        }
                        if(noOfQuest==2)
                        {
                            message.setText("Username: " + fName + " "+ sName +
                                                "Response 1: " + answers[0] + "\n" +
                                                    "Response 2: " + answers[1]);
                        }
                        if(noOfQuest==3)
                        {
                            message.setText("Username: " + fName + " "+ sName +
                                                "Response 1: " + answers[0] + "\n" +
                                                    "Response 2: " + answers[1] + "\n" +
                                                        "Response 3: " + answers[2]);
                        }
                        if(noOfQuest==4)
                        {
                            message.setText("Username: " + fName + sName +
                                                "Response 1: " + answers[0] + "\n" +
                                                    "Response 2: " + answers[1] + "\n" +
                                                        "Response 3: " + answers[2] + "\n" +
                                                            "Response 4: " + answers[3]);
                        }
                        if(noOfQuest==5)
                        {
                            message.setText("Username: " + fName + " "+ sName +
                                                "Response 1: " + answers[0] + "\n" +
                                                    "Response 2: " + answers[1] + "\n" +
                                                        "Response 3: " + answers[2] + "\n" +
                                                            "Response 4: " + answers[3] + "\n" +
                                                                "Response 4: " + answers[4]);
                        }

                        Transport.send(message);
                    }
                    if(noOfEmails==2)
                    {
                        for(int i=0; i<noOfQuest; i++)
                        {
                            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient2));
                            if(noOfQuest==1)
                            {
                                message.setText("Username: " + fName + " "+ sName + "\n" +
                                                    "Response 1: " + answers[0]);
                            }
                            if(noOfQuest==2)
                            {
                                message.setText("Username: " + fName + " "+ sName +
                                                    "Response 1: " + answers[0] + "\n" +
                                                        "Response 2: " + answers[1]);
                            }
                            if(noOfQuest==3)
                            {
                                message.setText("Username: " + fName + " "+ sName +
                                                    "Response 1: " + answers[0] + "\n" +
                                                        "Response 2: " + answers[1] + "\n" +
                                                            "Response 3: " + answers[2]);
                            }
                            if(noOfQuest==4)
                            {
                                message.setText("Username: " + fName + sName +
                                                    "Response 1: " + answers[0] + "\n" +
                                                        "Response 2: " + answers[1] + "\n" +
                                                            "Response 3: " + answers[2] + "\n" +
                                                                "Response 4: " + answers[3]);
                            }
                            if(noOfQuest==5)
                            {
                                message.setText("Username: " + fName + " "+ sName +
                                                    "Response 1: " + answers[0] + "\n" +
                                                        "Response 2: " + answers[1] + "\n" +
                                                            "Response 3: " + answers[2] + "\n" +
                                                                "Response 4: " + answers[3] + "\n" +
                                                                    "Response 4: " + answers[4]);
                            }

                            Transport.send(message);
                        }
                    }
                    if(noOfQuest==3)
                    {
                        for(int i=0; i<noOfQuest; i++)
                        {
                            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient3));
                            if(noOfQuest==1)
                            {
                                message.setText("Username: " + fName + " "+ sName + "\n" +
                                                    "Response 1: " + answers[0]);
                            }
                            if(noOfQuest==2)
                            {
                                message.setText("Username: " + fName + " "+ sName +
                                                    "Response 1: " + answers[0] + "\n" +
                                                        "Response 2: " + answers[1]);
                            }
                            if(noOfQuest==3)
                            {
                                message.setText("Username: " + fName + " "+ sName +
                                                    "Response 1: " + answers[0] + "\n" +
                                                        "Response 2: " + answers[1] + "\n" +
                                                            "Response 3: " + answers[2]);
                            }
                            if(noOfQuest==4)
                            {
                                message.setText("Username: " + fName + sName +
                                                    "Response 1: " + answers[0] + "\n" +
                                                        "Response 2: " + answers[1] + "\n" +
                                                            "Response 3: " + answers[2] + "\n" +
                                                                "Response 4: " + answers[3]);
                            }
                            if(noOfQuest==5)
                            {
                                message.setText("Username: " + fName + " "+ sName +
                                                    "Response 1: " + answers[0] + "\n" +
                                                        "Response 2: " + answers[1] + "\n" +
                                                            "Response 3: " + answers[2] + "\n" +
                                                                "Response 4: " + answers[3] + "\n" +
                                                                    "Response 4: " + answers[4]);
                            }
                            Transport.send(message);
                        }
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

经过一些实验,我注意到3个if语句中有2个嵌套在第一个语句中,导致了一个漏洞。

以下是工作代码:

** for循环检查配置文件,使用read **

for(int i=0; i<noOfEmails; i++)
        {
            if(noOfEmails==1)
            {
                for(int j=0; j<noOfQuest; j++)
                {
                    System.out.println("In Loop[" + j + "] : recipient1: "+ recipient1);


                    if(i==0)
                    {
                        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient1));                            
                    }
                    if(noOfQuest==1)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0]);                           
                    }
                    if(noOfQuest==2)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1]);                           
                    }
                    if(noOfQuest==3)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2]);                           
                    }
                    if(noOfQuest==4)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2] + "\n" +
                                                        "Response 4: " + answers[3]);                           
                    }
                    if(noOfQuest==5)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2] + "\n" +
                                                        "Response 4: " + answers[3] + "\n" +
                                                            "Response 4: " + answers[4]);
                    }
                }
            }   

            if(noOfEmails==2)
            {
                for(int k=0; k<noOfQuest; k++)
                {
                    System.out.println("In Loop[" + k + "] : recipient2: "+ recipient2);

                    if(i==0)
                    {
                        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient1));
                    }
                    if(i==1)
                    {
                        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient2));
                    }
                    if(noOfQuest==1)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0]);                               
                    }
                    if(noOfQuest==2)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1]);                               
                    }
                    if(noOfQuest==3)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2]);                               
                    }
                    if(noOfQuest==4)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2] + "\n" +
                                                        "Response 4: " + answers[3]);                               
                    }
                    if(noOfQuest==5)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2] + "\n" +
                                                        "Response 4: " + answers[3] + "\n" +
                                                            "Response 4: " + answers[4]);                               
                    }
                }
            }

            if(noOfEmails==3)
            {
                if(i==0)
                {
                    message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient1));
                    System.out.println(recipient1);
                }
                if(i==1)
                {
                    message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient2));
                    System.out.println(recipient2);
                }
                if(i==2)
                {
                    message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient3));
                    System.out.println(recipient3);
                }

                for(int l=0; l<noOfQuest; l++)
                {
                    System.out.println("In Loop[" + l + "] : recipient3: "+ recipient3);


                    if(noOfQuest==1)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0]);                               
                    }
                    if(noOfQuest==2)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1]);                               
                    }
                    if(noOfQuest==3)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2]);                               
                    }
                    if(noOfQuest==4)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2] + "\n" +
                                                        "Response 4: " + answers[3]);                               
                    }
                    if(noOfQuest==5)
                    {
                        message.setText("Username: " + fName + " "+ sName + "\n" +
                                            "Response 1: " + answers[0] + "\n" +
                                                "Response 2: " + answers[1] + "\n" +
                                                    "Response 3: " + answers[2] + "\n" +
                                                        "Response 4: " + answers[3] + "\n" +
                                                            "Response 4: " + answers[4]);
                    }
                }
            }
            Transport.send(message);
            System.out.println("Sent: " + message);
        }

对于任何困惑的人,&#34; noOfEmails | noOfQuest&#34;是从属性文件中读取的2个变量。