发送邮件多个收件人时发生异常

时间:2014-10-21 10:25:57

标签: c# system.net.mail

我正在尝试向多个电子邮件地址发送电子邮件。但是我得到了这个例外

A recipient must be specified

在这一行

smtp.Send(mail);

我已经检查了stackoverflow上的相关帖子  我试图添加这一行

 mail.To.Add(new MailAddress(c[0].Address));

但这次异常消息显示

: Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated

这是我的代码:

  MailMessage mail = new MailMessage();

            mail.From = new MailAddress(emailFrom);

            MailAddressCollection c = new MailAddressCollection();


            c.Add(new MailAddress("sendto@gmail.com"));

            mail.Subject = "some word";
            mail.Body = "some word";
            mail.IsBodyHtml = true;


            if (c[0].Address.Contains("yahoo"))
            {

                using (SmtpClient smtp = new SmtpClient())
                {

                    smtp.Credentials = new NetworkCredential(emailFrom,PASS);


                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
            }
            else if (c[0].Address.Contains("hotmail"))
            {

                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.Port = 587;
                    smtp.Host = "smtp.live.com";
                    smtp.Credentials = new NetworkCredential(emailFrom, PASS);


                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
            }

            else if (c[0].Address.Contains("gmail"))
            {

                SmtpClient smtp = new SmtpClient();

                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;

                smtp.Credentials = new NetworkCredential(emailFrom,PASS)


                smtp.EnableSsl = true;
                smtp.Send(mail);
            }

0 个答案:

没有答案