How to implement Sender ID when sending mails through C#?
c#, email, email-bounces
Solution
If you're sending mail on behalf of another user and you want it to be accepted you may need to do the following:
In your MailMessage object:
mail.To = new MailAddress("email@tosomeone.com", "To Someone");
mail.From = new MailAddress("sendinguser@fromsomeone.com", "Sending User");
mail.Sender = new MailAddress("serveraddress@your-domain-with-spf.com", "Your Server");
mail.ReplyTo = new MailAddress("sendinguser@fromsomeone.com", "Sending User");
This will generate the appropriate headers needed for SPF validation to work (assuming the server has it set to the defaults, they can pick which part to validate). This will make the email look like (in outlook)
From: Your Server on behalf of Sending User To: To Someone
Most SPF protocols will validate the `Sender:` header to determine if the sending domain allows or denies it so this needs to come from your domain regardless of who you're sending it "from".
Additionally you may want to double check that you have your SPF records set up right and that your IP address(es) are not on a blacklist such as spamhaus. Checking the actual return text of the call will usually tell you why it is being blocked with a 5.something error.
Problem
I just heard Joel & Jeff talk about Sender ID in their podcast number 83, and it occurred to me that's just what I need for a site I'm working on. Approximately 90% of all e-mails sent from the server is bounced or similar, likely because the server isn't "validated". I have a SPF record in place for the server, but that's also pretty much it. So since StackOverflow have solved these issues, I guess Sender ID must be the way to go. As far as I can tell from wikipedia, it requires you to modify the mail header when sending mails - how would I go about doing this from C# ? Also what would I need to setup DNS wise, etc. to make this work ? Or am I onto a completely wrong track here ? Edit: I'm using the standard SmtpClient class in C# for sending mails, and I do include both a plaintext and a HTML version of the mailbody.