mjanach
mjanach

Reputation: 85

C# How to set 'return-path' for smtp mail

I am trying to set the 'return-path' for my emails but I'm not seeing it as an available parameter. It seems like replytolist is not the same thing. I wan't to set the location that bounced emails are delivered. Here is my code so far:

    private static void SendMail(string html,string taxId,string toEmail,string filePath,string fromEmail,string replyToEmail,string emailSubject,string emailAttachPath)
    {
        try
        {
            MailMessage mail = new MailMessage();     
            mail.From = new MailAddress(fromEmail);
            mail.To.Add(toEmail);

            mail.Subject = emailSubject;
            mail.Body = html;

            //specify the priority of the mail message
            mail.ReplyToList.Add(replyToEmail);

            SmtpClient SmtpServer = new SmtpClient("smtp.server.com");
            SmtpServer.Port = 25;
            SmtpServer.UseDefaultCredentials = true;
            SmtpServer.EnableSsl = false;
            mail.IsBodyHtml = true;

            SmtpServer.Send(mail);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

Upvotes: 1

Views: 6890

Answers (1)

Naveen
Naveen

Reputation: 1461

If you dont write return path emailaddress. Server will take from email address convert into the return path. You also can see email report in your email original source.

enter image description here

If you want to add custom reutn path you can use

 MailMessage message = new MailMessage();
 message.Headers.Add("Return-Path", "response@*****.biz");

Also if you using postfix and you want add return path Automatically You will have to make changes in two files

  1. canonical :- put your return path emailaddress here.

    //[email protected]

  2. main.cf :- write your code in main.cf file

canonical_classes = envelope_sender
sender_canonical_maps = regexp:/etc/postfix/canonical

Upvotes: 4

Related Questions