Failed to add response to C # message header

I am developing a Windows Form application, Dot net Framework 4. for sending SMTP emails.

I use the following code to send email.

MailMessage mail = new MailMessage("\"Company Name\" <info@company.com>", textBox_Email_to.Text); SmtpClient client = new SmtpClient(); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Host = "host name"; mail.Subject = "test email"; mail.Body = file; // file contains some text mail.Headers.Add("reply-to", "service@company.de"); mail.IsBodyHtml = true; client.Send(mail); 

The only problem: mail.Headers.Add("reply-to", "service@company.de"); does not work.

I also tried using mail.ReplyTo = new MailAddress("service@company.de");

But still it doesn't work. When using mail.ReplyTo I get this warning:

'System.Net.Mail.MailMessage.ReplyTo' is deprecated: "ReplyTo is deprecated for this type. Instead, use a ReplyToList that can accept multiple addresses.

+2
c #
source share
2 answers

The exception tells you what to do - use ReplyToList:

In your case, it looks like this:

  MailMessage mail = new MailMessage("\"Company Name\" <info@company.com>", textBox_Email_to.Text); SmtpClient client = new SmtpClient(); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Host = "host name"; mail.Subject = "test email"; mail.Body = file; // file contains some text //mail.Headers.Add("reply-to", "service@company.de"); mail.ReplyToList.Add(new MailAddress("service@company.de", "reply-to")); mail.IsBodyHtml = true; client.Send(mail); 
+9
source share

This seems to give you some advice: use ReplyToList instead:

 mail.ReplyToList.Add("service@company.de"); 
+4
source share

All Articles