Search code examples
c#asp.netemailopenpop

How can I know which email is a reply to another email, in C#?


I have developed a web application, in which there's a functionality to enter note for a particular Sales-Order.

When a note is entered by a Customer or a Customer Service Executive, an email notification is sent to the relevant party (email notification is sent using SmtpClient & MailMessage objects in C#).

using (MailMessage objEmail = new MailMessage())
{
    Guid objGuid = new Guid();
    objGuid = Guid.NewGuid();
    String MessageID = "<" + objGuid.ToString() + ">";
    objEmail.Body = messagebody.ToString();
    objEmail.From = new MailAddress(sFrmadd, sFrmname);
    objEmail.Headers.Add("Message-Id", MessageID);
    objEmail.IsBodyHtml = true;
    objEmail.ReplyTo = new MailAddress("[email protected]");                    
    objEmail.Subject = sSubject;                    
    objEmail.To.Add(new MailAddress(sToadd));

    SmtpClient objSmtp = new SmtpClient();
    objSmtp.Credentials = new NetworkCredential("mynetworkcredential", "mypassword");
    objSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    objSmtp.EnableSsl = true;
    objSmtp.Host = "myhostname";
    objSmtp.Port = 25;
    objSmtp.Timeout = 3 * 3600;

    objSmtp.Send(objEmail);                    
}

I am setting a Guid as the Message-Id of the message being sent in the message headers.

All this works fine.

Now I want to develope a functionality for the parties to reply to the email notification from their respective inbox.

And I want to log the replies in the notes for the same Sales -Order (for which the party received the notification).

I am using OpenPop.dll for reading the inbox for notification-replies.

/// <summary>
/// Fetch all messages from a POP3 server
/// </summary>
/// <param name="hostname">Hostname of the server. For example: pop3.live.com</param>
/// <param name="port">Host port to connect to. Normally: 110 for plain POP3, 995 for SSL POP3</param>
/// <param name="useSsl">Whether or not to use SSL to connect to server</param>
/// <param name="username">Username of the user on the server</param>
/// <param name="password">Password of the user on the server</param>
/// <returns>All Messages on the POP3 server</returns>
public static List<Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password)
{
    // The client disconnects from the server when being disposed
    using (Pop3Client client = new Pop3Client())
    {
        // Connect to the server
        client.Connect(hostname, port, useSsl);

        // Authenticate ourselves towards the server
        client.Authenticate(username, password);

        // Get the number of messages in the inbox
        int messageCount = client.GetMessageCount();

        // We want to download all messages
        List<Message> allMessages = new List<Message>(messageCount);

        // Messages are numbered in the interval: [1, messageCount]
        // Ergo: message numbers are 1-based.
        for (int i = 1; i <= messageCount; i++)
        {
            allMessages.Add(client.GetMessage(i));
        }

        // Now return the fetched messages
        return allMessages;
    }
}

From the above function I am able to read all the emails from my "[email protected]" account. But I am not able to find the Message-Id in the In-reply-to header of the emails.

I don't know what I am doing wrong.


Solution

  • Best solution I can think of is putting your data in the "From" and/or "Reply-to" header using for example the '+' sign.

    Say your return adress is [email protected]

    You have to add a filter rule in you mail server, so that any message sent to [email protected] falls into [email protected] mailbox

    Facebook notifications use this for direct email replies.

    gmail uses it too (try it if you have a gmail address)

    ( see http://forums.smartertools.com/showthread.php/27790-Plus-Addressing-configure-symbol )

    Hope this will help. If so, good luck with mail server configuration