tags:

views:

2203

answers:

4

When you send an email using C# and the System.Net.Mail namespace, you can set the "From" and "Sender" properties on the MailMessage object, but neither of these allows you to make the MAIL FROM and the from address that goes into the DATA section different from each other. MAIL FROM gets set to the "From" property value, and if you set "Sender" it only adds another header field in the DATA section. This results in "From [email protected] on behalf of [email protected]", which is not what you want. Am I missing something?

The use case is controlling the NDR destination for newsletters, etc., that are sent on behalf of someone else.

I am currently using aspNetEmail instead of System.Net.Mail, since it allows me to do this properly (like most other SMTP libraries). With aspNetEmail, this is accomplished using the EmailMessage.ReversePath property.

A: 

While you're waiting for a definitive answer, there's a pretty useful resource here...

rohancragg
In 2004, that was useful. But in 2009? Really?
bzlm
A: 

Do you mean this?:

//create the mail message
 MailMessage mail = new MailMessage();

 //set the addresses
 mail.From = new MailAddress("[email protected]");
 mail.To.Add("[email protected]");

 //set the content
 mail.Subject = "This is an email";
 mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
 mail.IsBodyHtml = true;

 //send the message
 SmtpClient smtp = new SmtpClient("127.0.0.1");
 smtp.Send(mail);

From http://www.systemnetmail.com/faq/3.1.2.aspx

George Mauer
No, the mail.From address will determine the MAIL FROM in the envelope and in the DATA section. I want to make those two different addresses.
Eric Z Beard
the mail.From address will not necessarily determine the MAIL FROM in the envelope. It depends on the delivery method.
bzlm
A: 

I just found how to do it:

  • mail.From specify the email from visible to the final user
  • mail.Sender specifies the envelope MAIL FROM

That's it (even if it took me a while to figure it out)

Nip
No, as mentioned in my question: and if you set "Sender" it only adds another header field in the DATA section. This results in "From [email protected] on behalf of [email protected]", which is not what you want.
Eric Z Beard
I see what you mean. I was analyzing the packets using Wireshark and the envelope MAIL FROM was correctly set. I did not see any issue using GMail or AOL (sender, from, to and return-path are correctly set). But when I use my hotmail account or my exchange server, I see the same message as you do.
Nip
It must be a MSFT thing, where they don't want to hide the actual sender of the message. Annoying. I'll just keep using aspNetEmail, it's a great product.
Eric Z Beard
It's not about hiding the sender. The issue is that the mail envelope is technically not part of the message itself, and that the Return-Path header is surrounded by many complex considerations. This means the actual delivery method will have an impact on both of these.
bzlm
+3  A: 

MailMessage.Sender will always insert a Sender header (interpreted as on behalf of in your e-mail client).

If you use the Network delivery method on the SmtpClient, .Sender will also change the sender in the envelope. Using the PickupDirectoryFromIis delivery method will leave it to IIS to determine the envelope sender, and IIS will use the From address, not the Sender address.

There's a similar question on MSDN here.

bzlm