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("email@example.com", "Sent on Behalf of"); message.Sender = new MailAddress("firstname.lastname@example.org", "Actual address"); message.To.Add(new MailAddress("email@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:
firstname.lastname@example.org 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.