0

Using System.Web.Mail in an old project I used to use the following code to build an authenticated message

MailMessage msg = new MailMessage();

// ... fill in to, from etc

msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", Application["smtpserver"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", Application["smtpserverport"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Application["sendusername"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Application["sendpassword"].ToString());

System.Net.Mail is recommended as the replacement, but it seems to have done away with these fields.

Here's my new email sending code.

MailMessage em = new MailMessage();

// ... fill in to, from etc

// Init SmtpClient and send
SmtpClient smtpClient = 
    new SmtpClient(
         AppSetting["smtpserver"], 
         Convert.ToInt32(AppSetting["smtpserverport"])
    );
System.Net.NetworkCredential credentials = 
    new System.Net.NetworkCredential(
         AppSetting["sendusername"], 
         AppSetting["sendpassword"]
    );
smtpClient.Credentials = credentials;
smtpClient.Send(em);

I suspect that SmtpClient.Send is now doing all of this behind the scenes?

roryok
  • 9,325
  • 17
  • 71
  • 138

1 Answers1

0

Yes, you don't have to add those fields manually.

SupFrost
  • 41
  • 1
  • 4