Sending Email on Behalf of Your Users

At HelpEncourage.Me, one of the features is the ability to invite your friends and have them send you helpful advice and encouragement.  This means that sometimes I need to send email on your behalf – usually in the form of invitations to join the website or to become a specific user’s friend.

In previous posts, I covered setting up SPF records and DKIM to avoid the spam folder, but I also wanted our messages to “come from” the user who requested the message without losing the benefit of all the deliverability work.

Fortunately, this is pretty simple using .NET and the MailMessage class:

var message = new MailMessage();
message.From = new MailAddress("from@example.com", "Sent on Behalf of");
message.Sender = new MailAddress("sender@example.com", "Actual address");
message.To.Add(new MailAddress("to@example.com"));

The key is the “Sender” property.  Technically, you can use any address you want for both the “Sender” and the “From” field, but the “Sender” field is generally used to validate the email – so by using a HelpEncourage.Me address, I still get the benefit of the SPF and DKIM settings, while the email will show the address of the user who asked us to send it.  Some clients (GMail specifically) will even use this information to show exactly how the message was sent:

from@example.com via helpencourage.me

This way there is no attempt to hide who is sending the message or why.

Of course, allowing users to send emails can be dangerous, since unscrupulous users might abuse it to send spam messages appearing to be from legitimate sources, so care must be taken to ensure that the system is not abused.