Kamran
Kamran

Reputation: 4100

Unable to add reply to in mail header C#

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

I am using following code to send email.

MailMessage mail = new MailMessage("\"Company Name\" <[email protected]>", 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", "[email protected]");
mail.IsBodyHtml = true;
client.Send(mail);

The only problem is mail.Headers.Add("reply-to", "[email protected]"); is not working.

I also tried to use mail.ReplyTo = new MailAddress("[email protected]");

But still its not working. While using mail.ReplyTo I am getting this warning:

'System.Net.Mail.MailMessage.ReplyTo' is obsolete: '"ReplyTo is obsoleted for this type. Please use ReplyToList instead which can accept multiple addresses.

Upvotes: 4

Views: 11414

Answers (2)

Kaspars Ozols
Kaspars Ozols

Reputation: 7017

Exception tells you what to do - use ReplyToList:

In your case it looks like this:

        MailMessage mail = new MailMessage("\"Company Name\" <[email protected]>", 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", "[email protected]");
        mail.ReplyToList.Add(new MailAddress("[email protected]", "reply-to"));

        mail.IsBodyHtml = true;
        client.Send(mail);

Upvotes: 16

CassOnMars
CassOnMars

Reputation: 6181

It sounds like it's giving you the advice to follow: use ReplyToList instead:

mail.ReplyToList.Add("[email protected]");

Upvotes: 4

Related Questions