使用不带 TO 的 BCC 在 C# 中通过 SMTP 发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/302271/
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-08-03 22:20:13  来源:igfitidea点击:

Sending Mail via SMTP in C# using BCC without TO

c#emailsmtpbcc

提问by Brad Barker

I am trying to use the System.Net.Mail.MailMessageclass in C# to create an email that is sent to a list of email addresses all via BCC. I do not want to include a TOaddress, but it seems that I must because I get an exception if I use an empty string for the TOaddress in the MailMessageconstructor. The error states:

我正在尝试使用System.Net.Mail.MailMessageC# 中的类创建一封电子邮件,该电子邮件通过BCC. 我不想包含TO地址,但似乎必须包含,因为如果我TOMailMessage构造函数中使用空字符串作为地址,则会出现异常。错误指出:

ArgumentException
The parameter 'addresses' cannot be an empty string.
Parameter name: addresses

Surely it is possible to send an email using only BCCas this is not a limitation of SMTP.

当然可以仅使用发送电子邮件,BCC因为这不是 SMTP 的限制。

Is there a way around this?

有没有解决的办法?

采纳答案by FlySwat

I think if you comment out the whole emailMessage.To.Add(sendTo);line , it will send the email with Tofield empty.

我想如果你注释掉整emailMessage.To.Add(sendTo);行,它会发送To字段为空的电子邮件。

回答by k?e?m?p? ?

You have to include a TO address. Just send it to a "junk" email address that you don't mind getting mail on.

您必须包含一个 TO 地址。只需将其发送到您不介意接收邮件的“垃圾”电子邮件地址即可。

回答by Ben Scheirman

Do the same thing you do for internal mail blasts where you don't want people to reply-to-all all the time.

对内部邮件群发做同样的事情,您不希望人们一直回复。

Send it toyourself (or a dummy account), then add your BCC list.

它发送自己(或虚拟帐户),然后添加您的BCC列表。

回答by FlySwat

It doesn't even need to be a real e-mail address, I typically use [email protected]for TO, and NoReply@CompanyNamefor FROM.

它甚至不需要是真实的电子邮件地址,我通常使用[email protected]forTONoReply@CompanyNamefor FROM

回答by Tommy Carlier

Just don't call the Add-method on the To-property.

只是不要在 To-property 上调用 Add-method。

回答by DaveSaunders

Must have a to address. It has been this way since the original sendmail an implementations in the 80s and all SMTP mailers, I know of, have the same requirement. You can just use a dummy address for to.

必须有一个地址。自从 80 年代最初的 sendmail 实现和我知道的所有 SMTP 邮件程序都有相同的要求以来,一直是这样。你可以只使用一个虚拟地址。

回答by Olivier de Rivoyre

You can use a temporary fake address then remove it just after:

您可以使用临时假地址,然后在以下情况下将其删除:

MailMessage msg = new MailMessage(from, "[email protected]");
msg.To.Clear();
foreach (string email in bcc.Split(';').Select(x => x.Trim()).Where(x => x != ""))
{
     msg.Bcc.Add(email);
}

回答by A J

I think you were trying to do something like :

我认为您正在尝试执行以下操作:

var msg = new MailMessage("[email protected]", "");

That's why you were seeing the error message.

这就是您看到错误消息的原因。

Instead, try something like this:

相反,尝试这样的事情:

MailMessage msg = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.notreal.com");
msg.From = new MailAddress("[email protected]");
//msg.To.Add("[email protected]"); <--not needed
msg.Bcc.Add("[email protected]");