无法使用C#向多个地址/收件人发送电子邮件

时间:2010-07-09 00:54:58

标签: c# .net email

我使用以下代码,它只发送一封电子邮件 - 我必须将电子邮件发送到多个地址。

我要使用多封电子邮件:

string connectionString = ConfigurationManager.ConnectionStrings["email_data"].ConnectionString;
OleDbConnection con100 = new OleDbConnection(connectionString);
OleDbCommand cmd100 = new OleDbCommand("select top 3 emails  from bulk_tbl", con100);
OleDbDataAdapter da100 = new OleDbDataAdapter(cmd100);
DataSet ds100 = new DataSet();
da100.Fill(ds100);

    for (int i = 0; i < ds100.Tables[0].Rows.Count; i++)
    //try
    {
        string all_emails = ds100.Tables[0].Rows[i][0].ToString();
        {
            string allmail = all_emails + ";";
            Session.Add("ad_emails",allmail);
            Response.Write(Session["ad_emails"]);
            send_mail();
        }
    }

并发送我使用的电子邮件:

string sendto = Session["ad_emails"].ToString();

MailMessage message = new MailMessage("info@abc.com", sendto, "subject", "body");
SmtpClient emailClient = new SmtpClient("mail.smtp.com");
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("abc", "abc");
emailClient.UseDefaultCredentials = true;
emailClient.Credentials = SMTPUserInfo;
emailClient.Send(message);

6 个答案:

答案 0 :(得分:60)

问题是,当只需要一个表示单个地址的字符串时,您将提供由分号分隔的地址列表到MailMessage构造函数:

  

包含电子邮件收件人地址的字符串。

或可能是以逗号分隔的列表(见下文)。

Source

要指定多个地址,您需要使用To MailAddressCollection属性,但这些页面上的示例并未清楚地显示:

message.To.Add("one@example.com, two@example.com"));
  

要添加到MailAddressCollection的电子邮件地址。必须使用逗号字符(“,”)分隔多个电子邮件地址。

MSDN page

因此,使用逗号分隔列表创建MailMessage应该有效。

答案 1 :(得分:8)

这对我有用。 (收件人是一个字符串数组)

//Fuse all Receivers
var allRecipients = String.Join(",", recipients);

//Create new mail
var mail = new MailMessage(sender, allRecipients, subject, body);

//Create new SmtpClient
var smtpClient = new SmtpClient(hostname, port);

//Try Sending The mail
try
{
    smtpClient.Send(mail);
}
catch (Exception ex)
{
    Log.Error(String.Format("MailAppointment: Could Not Send Mail. Error = {0}",ex), this);
    return false;
}

答案 2 :(得分:6)

此函数验证以逗号分隔或以分号分隔的电子邮件地址列表:

public static bool IsValidEmailString(string emailAddresses)
{
    try
    {
        var addresses = emailAddresses.Split(',', ';')
            .Where(a => !string.IsNullOrWhiteSpace(a))
            .ToArray();

        var reformattedAddresses = string.Join(",", addresses);

        var dummyMessage = new System.Net.Mail.MailMessage();
        dummyMessage.To.Add(reformattedAddresses);
        return true;
    }
    catch
    {
        return false;
    }
}

答案 3 :(得分:4)

要发送给多个收件人,我使用逗号设置收件人字符串作为我的分隔符。

string recipient = "foo@bar.com,foo2@bar.com,foo3@bar.com";

然后将收件人添加到MailMessage对象:

string[] emailTo = recipient.Split(',');
for (int i = 0; i < emailTo.GetLength(0); i++)
    mailMessageObject.To.Add(emailTo[i]);

答案 4 :(得分:0)

此代码用于发送to,bcc和cc

的多个邮件
MailMessage email = new MailMessage();
Attachment a = new Attachment(attach);
email.From = new MailAddress(from);//De
string[] Direcciones;
char[] deliminadores = { ';' };

//Seleccion de direcciones para el parametro to
Direcciones = to.Split(deliminadores);
foreach (string d in Direcciones)
email.To.Add(new MailAddress(d));//Para

//Seleccion de direcciones para el parametro CC
Direcciones = CC.Split(deliminadores);
foreach (string d in Direcciones)
email.CC.Add(new MailAddress(d));

//Seleccion de direcciones para el parametro Bcc
Direcciones = Bcc.Split(deliminadores);
foreach (string d in Direcciones)
enter code here`email.Bcc.Add(new MailAddress(d));

答案 5 :(得分:0)

您还可以通过MailMessage.To.Add() comma separated list of valid RFC 822 e-mail addresses

Nathaniel Borenstein <nsb@bellcore.com>, Ned Freed <ned@innosoft.com>

所以代码是:

message.To.Add("Nathaniel Borenstein <nsb@bellcore.com>, Ned Freed <ned@innosoft.com>");
  

注意:任何已发布到公共领域的代码。无需归属。