tags:

views:

70

answers:

1

Hello, I am using mail message class to send an email. But if i check my Gmail account, mail is received as separate mail. I want mails in a single thread. I am also using the same subject and tried appending "Re: " before subject. it did not work for me. I will be pleased if get the solution. following is the code I am using.

public static bool SendEmail(
    string pGmailEmail,
    string pGmailPassword,
    string pTo,
    string pFrom,
    string pSubject,
    string pBody,
    System.Web.Mail.MailFormat pFormat,
    string pAttachmentPath)
    {
        try
        {
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");
            //sendusing: cdoSendUsingPort, value 2, for sending the message using
            //the network.

            //smtpauthenticate: Specifies the mechanism used when authenticating
            //to an SMTP
            //service over the network. Possible values are:
            //- cdoAnonymous, value 0. Do not authenticate.
            //- cdoBasic, value 1. Use basic clear-text authentication.
            //When using this option you have to provide the user name and password
            //through the sendusername and sendpassword fields.
            //- cdoNTLM, value 2. The current process security context is used to
            // authenticate with the service.
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //Use 0 for anonymous
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                pGmailEmail);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                 pGmailPassword);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 "true");
            myMail.From = pFrom;
            myMail.To = pTo;
            myMail.Subject = pSubject;
            myMail.BodyFormat = pFormat;
            myMail.Body = pBody;
            if (pAttachmentPath.Trim() != "")
            {
                MailAttachment MyAttachment =
                        new MailAttachment(pAttachmentPath);
                myMail.Attachments.Add(MyAttachment);
                myMail.Priority = System.Web.Mail.MailPriority.High;
            }

            // System.Web.Mail.SmtpMail.SmtpServer = CCConstants.MAIL_SERVER;
            System.Web.Mail.SmtpMail.Send(myMail);
            return true;
        }
        catch
        {
            throw;
        }
    }
A: 

you should know thread starter email internal ID, then you should send it back via headers "In-Reply-To: " or "References: ". Btw sending email via gmail is rather simple:

var smtpClient = new SmtpClient("smtp.gmail.com",587);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("[email protected]"
            ,"PASSWORD");
using (MailMessage message = new MailMessage("[email protected]","[email protected]"))
{
    message.Subject = "test";
    smtpClient.Send(message);
}

using (MailMessage message = new MailMessage("[email protected]","[email protected]"))
{
    message.Subject = "Re: test";
    message.Headers.Add("In-Reply-To", "<MESSAGEID.From.Original.Message>");
    message.Headers.Add("References",  "<MESSAGEID.From.Original.Message>");
    smtpClient.Send(message);
}
Nisus
thanks Nisus for your reply.