Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Sender ID when sending mails through C#?

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.

like image 577
Steffen Avatar asked Mar 04 '10 09:03

Steffen


2 Answers

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 protected]", "To Someone");
mail.From = new MailAddress("[email protected]", "Sending User");
mail.Sender = new MailAddress("[email protected]", "Your Server");
mail.ReplyTo = new MailAddress("[email protected]", "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.

like image 87
Joshua Avatar answered Oct 13 '22 00:10

Joshua


I don't see why you need to make any header changes for Sender ID/SPF. With your record in place, you just need to make sure your outgoing messages are From: a domain that has a matching published sending (public) IP in your SPF record in DNS.

However, I would think you could examine MailMessage.Headers to see what headers are being sent (or send messages to yourself) in order to determine based on your SPF record type how receiving mail servers will be computing the domain to match to your SPF record.

like image 27
NetMage Avatar answered Oct 13 '22 00:10

NetMage