Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SendGrid“;至;列出对列表中的每个人可见的电子邮件ID_C#_Asp.net_Console Application_Email_Sendgrid - Fatal编程技术网

C# SendGrid“;至;列出对列表中的每个人可见的电子邮件ID

C# SendGrid“;至;列出对列表中的每个人可见的电子邮件ID,c#,asp.net,console-application,email,sendgrid,C#,Asp.net,Console Application,Email,Sendgrid,我正在使用SendGrid通过asp.net中的控制台应用程序向用户列表发送电子邮件。在发送电子邮件时,我在AddTo部分发送用户电子邮件地址列表。代码如下所示: SendGridMessage=new SendGridMessage() message.AddTo(新列表(){”user1@abc.com", "user2@xyz.com", "user3@abc.com", "user4@xyz.com" }); 电子邮件按预期发送,但在电子邮件的“收件人”部分,我可以看到发送此电子邮件的用

我正在使用SendGrid通过asp.net中的控制台应用程序向用户列表发送电子邮件。在发送电子邮件时,我在AddTo部分发送用户电子邮件地址列表。代码如下所示:

SendGridMessage=new SendGridMessage()
message.AddTo(新列表(){”user1@abc.com", "user2@xyz.com", "user3@abc.com", "user4@xyz.com" });

电子邮件按预期发送,但在电子邮件的“收件人”部分,我可以看到发送此电子邮件的用户的所有电子邮件ID(见下图)。我希望电子邮件ID被隐藏,这样就不会有人滥用列表中的其他电子邮件ID。我是否可以使用SendGrid实现这一点? 使用.AddBcc()而不是.AddTo()。但是如果您这样做,那么您必须将to地址设置为“no”之类的值-reply@example.com“这并不理想,可能会增加邮件最终进入用户的垃圾邮件或垃圾邮件文件夹的可能性

因此,应该编写一个for循环来为每个用户发送电子邮件

var emailAddresses = new List<string>() { "user1@abc.com", "user2@xyz.com", "user3@abc.com", "user4@xyz.com" };

for (var emailAddress in emailAddresses)
{
     var email = new SendGridMessage();

     email.AddTo(emailAddress);

     // set other values such as the email contact

     // send/deliver email
}
var emailAddresses=new List(){”user1@abc.com", "user2@xyz.com", "user3@abc.com", "user4@xyz.com" };
for(emailAddresss中的var emailAddress)
{
var email=new SendGridMessage();
email.AddTo(emailAddress);
//设置其他值,例如电子邮件联系人
//发送/发送电子邮件
}

电子邮件的内容对每个人都一样吗?我假设每个人都有不同的“每月使用量”,如果是这样,for循环会更好

要向SendGrid中的多个收件人发送邮件而不让他们看到对方,您需要使用,而不是本机SMTP-To头

var header = new Header();

var recipients = new List<String> {"a@example.com", "b@exampe.com", "c@example.com"};
header.SetTo(recipients);

var subs = new List<String> {"A","B","C"};
header.AddSubstitution("%name%", subs);

var mail = new MailMessage
{
    From = new MailAddress("please-reply@example.com"),
    Subject = "Welcome",
    Body = "Hi there %name%"
};

// add the custom header that we built above
mail.Headers.Add("X-SMTPAPI", header.JsonString());
var头=新头();
var recipients=新列表{”a@example.com", "b@exampe.com", "c@example.com"};
header.SetTo(收件人);
var subs=新列表{“A”、“B”、“C”};
header.AddSubstitution(“%name%”,subs);
var mail=新邮件消息
{
From=新邮件地址(“请-reply@example.com"),
Subject=“欢迎”,
Body=“你好,有%name%”
};
//添加我们在上面构建的自定义标题
Add(“X-SMTPAPI”,header.JsonString());

SMTPAPI头将由SendGrid解析,每个收件人将收到一条不同的“收件人”消息。

如果使用SmtpClient和MailMessage类,这将适用。OP正在使用SendGridMessage。如果OP将其代码更改为使用SmtpClient和MailMessage类,那么这将起作用。