2010-08-30 57 views
2

我正在使用郵件消息類發送電子郵件。但是,如果我檢查我的Gmail帳戶,郵件將作爲單獨的郵件收到。我想在單個線程中發送郵件。我也使用相同的主題,並嘗試在主題前追加「Re:」。它不適合我。 如果得到解決方案,我會很高興。以下是我正在使用的代碼。使用c#中的smtp在gmail的同一線程中發送電子郵件

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; 
     } 
    } 

回答

3

您應該知道線程啓動器電子郵件的內部ID,那麼您應該通過標題「In-Reply-To:」或「References:」將其發送回去。 順便說一句,通過gmail發送郵件相當簡單:

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); 
} 
+0

感謝Nisus的回覆。 – user434909 2010-08-30 14:42:07

相關問題