none
smtpclient - custom x-receiver tag RRS feed

  • Question

  • Hi

    I have a requirements to send an email to person with custom x-receiver header. So, We have compliance system which will capture the emails based on x-receiver.

    When ever I am sending email with TO and From, smtpclient automatically adds x-receiver header with To address. Since I am getting

    X-Sender: John@dummyco.com
    X-Receiver: John@Doe.com    << How to Remove this?
    X-Receiver: ccc@custom.com
    MIME-Version: 1.0
    From: John@dummyco.com
    To: John@Doe.com
    Date: 4 Feb 2014 16:39:45 -0500
    Subject: test email

    Thanks


    paperdoc

    Tuesday, February 4, 2014 9:43 PM

Answers

  • >> Some how not able to do with c#.

    Here I made a demo for C#, and please check it below:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Linq;
    
    using System.Net.Mail;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    
    namespace Smpt
    
    {
    
        class Program
    
        {
    
            static bool mailSent = false;
    
            private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    
            {
    
                // Get the unique identifier for this asynchronous operation.
    
                String token = (string)e.UserState;
    
    
                if (e.Cancelled)
    
                {
    
                    Console.WriteLine("[{0}] Send canceled.", token);
    
                }
    
                if (e.Error != null)
    
                {
    
                    Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
    
                }
    
                else
    
                {
    
                    Console.WriteLine("Message sent.");
    
                }
    
                mailSent = true;
    
            }
    
            public static void Main(string[] args)
    
            {
    
                // Command line argument must the the SMTP host.
    
                SmtpClient client = new SmtpClient();
    
    
                client.Credentials = new System.Net.NetworkCredential("YourUserName", "YourPassWord"); ;
    
    
                client.Host = "YourHost";
    
                client.Port = 587;
    
                client.EnableSsl = true; 
    
    
                // Specify the e-mail sender. 
    
                // Create a mailing address that includes a UTF8 character 
    
                // in the display name.
    
                MailAddress from = new MailAddress("YourFromMailAddress");
    
                // Set destinations for the e-mail message.
    
                MailAddress to = new MailAddress("YourToMailAddress");
    
                // Specify the message content.
    
                MailMessage message = new MailMessage(from, to);
    
    
                message.Headers.Add("X-Company", "My Company");
    
                message.Headers.Add("X-Location", "Hong Kong");
    
    
    
                message.Body = "This is a test e-mail message sent by an application. ";
    
                // Include some non-ASCII characters in body and subject. 
    
                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
    
                message.Body += Environment.NewLine + someArrows;
    
                message.BodyEncoding = System.Text.Encoding.UTF8;
    
                message.Subject = "test message 1" + someArrows;
    
                message.SubjectEncoding = System.Text.Encoding.UTF8;
    
                // Set the method that is called back when the send operation ends.
    
                client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
    
                // The userState can be any object that allows your callback  
    
                // method to identify this send operation. 
    
                // For this example, the userToken is a string constant. 
    
                string userState = "test message1";
    
                client.SendAsync(message, userState);
    
                Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
    
                string answer = Console.ReadLine();
    
                // If the user canceled the send, and mail hasn't been sent yet, 
    
                // then cancel the pending operation. 
    
                if (answer.StartsWith("c") && mailSent == false)
    
                {
    
                    client.SendAsyncCancel();
    
                }
    
                // Clean up.
    
                message.Dispose();
    
                Console.WriteLine("Goodbye.");
    
            }
    
        }
    
    }
    

    It will not add x-receiver automatically.

    If this does not work for you, could you please share how you write your smtpclient.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 6, 2014 7:33 AM
    Moderator

All replies

  • Hello,

    In compliance system, does it has codes like below?

    client.Headers.Add("X-Receiver ", " John@Doe.com");

    If it has, comment this line should be ok.

    If I misunderstand, could you please share your smtpclient codes?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 5, 2014 4:34 AM
    Moderator
  • Hi

    Compliance system looks for x-receiver "ccc @ custom.com" and then captures. Since we are getting two x-receivers, having trouble.

    They told me with Java you can easily send email using envelope To and From. Some how not able to do with c#.

    regards


    paperdoc

    Wednesday, February 5, 2014 2:29 PM
  • >> Some how not able to do with c#.

    Here I made a demo for C#, and please check it below:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Linq;
    
    using System.Net.Mail;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    
    namespace Smpt
    
    {
    
        class Program
    
        {
    
            static bool mailSent = false;
    
            private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    
            {
    
                // Get the unique identifier for this asynchronous operation.
    
                String token = (string)e.UserState;
    
    
                if (e.Cancelled)
    
                {
    
                    Console.WriteLine("[{0}] Send canceled.", token);
    
                }
    
                if (e.Error != null)
    
                {
    
                    Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
    
                }
    
                else
    
                {
    
                    Console.WriteLine("Message sent.");
    
                }
    
                mailSent = true;
    
            }
    
            public static void Main(string[] args)
    
            {
    
                // Command line argument must the the SMTP host.
    
                SmtpClient client = new SmtpClient();
    
    
                client.Credentials = new System.Net.NetworkCredential("YourUserName", "YourPassWord"); ;
    
    
                client.Host = "YourHost";
    
                client.Port = 587;
    
                client.EnableSsl = true; 
    
    
                // Specify the e-mail sender. 
    
                // Create a mailing address that includes a UTF8 character 
    
                // in the display name.
    
                MailAddress from = new MailAddress("YourFromMailAddress");
    
                // Set destinations for the e-mail message.
    
                MailAddress to = new MailAddress("YourToMailAddress");
    
                // Specify the message content.
    
                MailMessage message = new MailMessage(from, to);
    
    
                message.Headers.Add("X-Company", "My Company");
    
                message.Headers.Add("X-Location", "Hong Kong");
    
    
    
                message.Body = "This is a test e-mail message sent by an application. ";
    
                // Include some non-ASCII characters in body and subject. 
    
                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
    
                message.Body += Environment.NewLine + someArrows;
    
                message.BodyEncoding = System.Text.Encoding.UTF8;
    
                message.Subject = "test message 1" + someArrows;
    
                message.SubjectEncoding = System.Text.Encoding.UTF8;
    
                // Set the method that is called back when the send operation ends.
    
                client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
    
                // The userState can be any object that allows your callback  
    
                // method to identify this send operation. 
    
                // For this example, the userToken is a string constant. 
    
                string userState = "test message1";
    
                client.SendAsync(message, userState);
    
                Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
    
                string answer = Console.ReadLine();
    
                // If the user canceled the send, and mail hasn't been sent yet, 
    
                // then cancel the pending operation. 
    
                if (answer.StartsWith("c") && mailSent == false)
    
                {
    
                    client.SendAsyncCancel();
    
                }
    
                // Clean up.
    
                message.Dispose();
    
                Console.WriteLine("Goodbye.");
    
            }
    
        }
    
    }
    

    It will not add x-receiver automatically.

    If this does not work for you, could you please share how you write your smtpclient.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 6, 2014 7:33 AM
    Moderator