2013-10-04 74 views
0

我使用代碼的sendmail發在asp.net密件抄送郵件和CC

public void sendmail() 
{ 
    try 
    { 
     System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); 
     EmailServerDAL emailDAL = new EmailServerDAL(); 
     EmailServer emailServer = emailDAL.FindByCondition(c => true).FirstOrDefault(); 
     string mail = emailServer.Email; 
     string mailserver = emailServer.Mailserver; 
     int port = int.Parse(emailServer.Mailport.ToString()); 
     string pass = emailServer.Pass; 
     string detail = CKEditorControl1.Text; 
     if (mailserver == "smtp.gmail.com") 
     { 
      MailMessage em = new MailMessage(); 
      em.IsBodyHtml = true;//khai báo body là html 
      em.BodyEncoding = System.Text.Encoding.UTF8;//khai báo body dùng mã UTF8 
      //em.From = new MailAddress(CKEditorControl1.Text, "itool.vn"); 
      em.To.Add(new MailAddress(txtTo.Text, txtTitle.Text)); 
      //em.To.Add(new MailAddress(txtTo.Text, "ITOOL.VN")); 
      if (!String.IsNullOrEmpty(TxtCC.Text)) 
      { 
       em.CC.Add = TxtCC.Text.Replace(',', ';').Replace(" ", ""); 
      } 
      if (!String.IsNullOrEmpty(txtBCC.Text)) 
      { 
       em.Bcc.Add = (new MailAddress(txtBCC.Text.Replace(',', ';').Replace(" ", ""))); 
      } 
      if (FileUpload1.HasFile) 
      { 
       string upload = Server.MapPath("~/upload/email/" + FileUpload1.FileName); 
       FileUpload1.SaveAs(upload); 
       Attachment attach = new Attachment(upload); 
       em.Attachments.Add(attach); 
      } 
      em.Subject = txtTitle.Text; 
      em.Body = detail; 
      //em.IsBodyHtml = true; 
      SmtpClient sm = new SmtpClient(); 
      sm.Host = "smtp.gmail.com"; 
      sm.EnableSsl = true; 
      sm.Credentials = new NetworkCredential(mail, pass); 
      sm.Send(em); 
      lterror.Text = "<div class='Notice-Info'>Gởi mail thành công!</div>"; 
     } 

}

我想發送郵件寬度BCC和CC,但錯誤「無法分配到‘添加’,因爲它是一個'方法組''

回答

3

MailMessage上的Add是一種方法,而不是屬性。因此,您不要將CC/BCC列表分配給它,而是將它作爲參數傳遞,就像您使用其他任何函數一樣。

em.CC.Add(TxtCC.Text.Replace(',', ';').Replace(" ", "")); 

em.Bcc.Add(new MailAddress(txtBCC.Text.Replace(',', ';').Replace(" ", ""))); 
+0

如何添加Bcc和CC – user2847541

+0

@ user2847541,正如我向您展示的那樣。你到底在做什麼? – Brandon

相關問題