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?
I think if you comment out the whole emailMessage.To.Add(sendTo);
line , it will send the email with To
field empty.
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.
You have to include a TO address. Just send it to a "junk" email address that you don't mind getting mail on.
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
.
Just don't call the Add-method on the To-property.
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.
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);
}
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");