Sending Mail via SMTP in C# using BCC without TO

2019-06-15 21:21发布

问题:

I am trying to use the System.Net.Mail.MailMessage class 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 TO address, but it seems that I must because I get an exception if I use an empty string for the TO address in the MailMessage constructor. The error states:

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

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

Is there a way around this?

回答1:

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



回答2:

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 to yourself (or a dummy account), then add your BCC list.



回答3:

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



回答4:

It doesn't even need to be a real e-mail address, I typically use Mailer@CompanyName.com for TO, and NoReply@CompanyName for FROM.



回答5:

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



回答6:

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.



回答7:

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

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


回答8:

I think you were trying to do something like :

var msg = new MailMessage("no-reply@notreal.com", "");

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("no-reply@notreal.com");
//msg.To.Add("target@notreal.com"); <--not needed
msg.Bcc.Add("BCCtarget@notreal.com");


标签: c# email smtp bcc