Sending Mail via SMTP in C# using BCC without TO

2019-06-15 20:52发布

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?

标签: c# email smtp bcc
8条回答
做个烂人
2楼-- · 2019-06-15 21:15

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");
查看更多
倾城 Initia
3楼-- · 2019-06-15 21:25

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.

查看更多
聊天终结者
4楼-- · 2019-06-15 21:28

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

查看更多
看我几分像从前
5楼-- · 2019-06-15 21:33

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);
}
查看更多
We Are One
6楼-- · 2019-06-15 21:34

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

查看更多
孤傲高冷的网名
7楼-- · 2019-06-15 21:39

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.

查看更多
登录 后发表回答