Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 958
Devraj Gadhavi Avatar asked Feb 29 '12 06:02

Devraj Gadhavi


People also ask

How do I check email replies?

With Conversation View on Look at a message in your inbox, for example. If you replied to the message, in the sender field you will see the original sender's name followed by "me" and (2).

Can you reply to an email with a different email address?

Always send from a different addressIf you only change the "From" address, replies will go to your original Gmail address by default. To always send email from a different address or alias: On your computer, open Gmail.

How do you reply to an email with a forward of another email?

Reply to an email with an attached emailOpen Gmail. Open the message and click Reply. From your inbox, check the box next to the email that you want to attach and drag it to the message window. Enter any message text and click Send.

Which command is used to reply to the sender of an email?

Depending on whether you want to reply to or forward an email message, do the following: To reply only to the sender, press Ctrl+R to open a message addressed to the sender, with the cursor in the message body, ready to compose a response.


2 Answers

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

like image 96
jbl Avatar answered Oct 11 '22 20:10

jbl


As replied by @jbl, we used the plus addressing concept. We asked our email provider to enable this concept on our SMTP server. Gmail provides this by default.

When sending any email out, we would make a unique reply to address for every note entered on an order like below.

String sReplyToadd = "[email protected]";
String replyToAddress = sReplyToadd.Substring(0, sReplyToadd.IndexOf('@')) + "+on" + orderID + "un" + userID + sReplyToadd.Substring(sReplyToadd.IndexOf('@'), sReplyToadd.Length - sReplyToadd.IndexOf('@'));

This would make the replyToAddress = "[email protected]", a unique address to identify the order and the user who posted the note.

Now, this unique reply to address would be assigned to the email going out as below.

using (MailMessage objEmail = new MailMessage())
{
    objEmail.Body = eMailBody;
    objEmail.From = new MailAddress("[email protected]", "Display Name");
    objEmail.IsBodyHtml = true;
    objEmail.Subject = "email subject goes here";
    objEmail.To.Add(new MailAddress("[email protected]");

    //here we set the unique reply to address for the outgoing email
    objEmail.ReplyTo = new MailAddress(replyToAddress); //[email protected]

    SmtpClient objSmtp = new SmtpClient();

    objSmtp.EnableSsl = true;
    objSmtp.Credentials = new NetworkCredential("username", "password");
    objSmtp.Host = "127.0.0.1";//"smtp.gmail.com" for gmail
    objSmtp.Port = 25;

    objSmtp.Send(objEmail);
}

ReplyTo address would appear in the to address, if the user clicks on reply button in their email client as shown below.

Reply To Address shown in To in the email cient

If the user doesn't change the To address, it would be received on our [email protected] mailbox. We put a note on the bottom of every email going out : Please do not change the To address to map your reply properly to the system.

Once the email reaches the mailbox, we just need to check the To address of the email reply and get the required order id and user id, as below

String replyFor = objEmail.To[0].ToString();
Int64 orderID = Convert.ToInt64(replyFor.Substring(replyFor.LastIndexOf("+on") + 3, replyFor.LastIndexOf("un")));
Int64 userID = replyFor.Substring(replyFor.LastIndexOf("un") + 2, replyFor.IndexOf("@") - replyFor.LastIndexOf("un") - 2);

And then we lived happily ever after!!!

like image 2
Devraj Gadhavi Avatar answered Oct 11 '22 20:10

Devraj Gadhavi