2016-04-05 9 views
0

msg機能でレシピエントを追加する必要がある場合特定の条件が満たされた場合

私は(「終了」「Termination_ALL」)文字列の商品名である場合は、送信メールメソッドに複数の受信者を追加したい

文が受信者を追加する場合は、単純なを追加することが可能です、というより全体のメッセージ。

public string SendEmailAccountManager(string cPNumber) 
{ 
    string StaffId = ""; 
    string StaffEmail = ""; 
    string AccMgrName = ""; 
    string custName = ""; 
    string strSql = ""; 
string strSql2 = ""; 

    string custid = ""; 
    string customerGroup = ""; 
    string ProductName = ""; 

    try 
    { 
     { 
      oConn = new OleDbConnection(strConn); 
      oConn.Open(); 

      if (cPNumber != "") 
      { 

       //strSql = "select staff_id, staff_email,staff_name from concerned_staff where staff_id=(select acc_mgr from customers where cust_id=(select cust_id from faxes where cp_no= '" + cPNumber + "'))"; 
       strSql = "select a.cust_name,a.cust_id,a.cust_group,b.staff_id, b.staff_email,b.staff_name from customers a, concerned_staff b where cust_id=(select cust_id from faxes where cp_no= '" + cPNumber + "') and a.acc_mgr = b.staff_id"; 
     strSql2 = "select a.cust_name,a.cust_id,a.cust_group,b.staff_id, b.staff_email,b.staff_name from customers a, concerned_staff b where cust_id=(select cust_id from faxes where cp_no= '" + cPNumber + "') and a.acc_mgr2 = b.staff_id"; 

     strSQL = "SELECT SER_TYPE FROM FAXES WHERE CP_NO = '" + cPNumber + "'"; 

     oComm = new OleDbCommand(strSQL, oConn); 
     dr = oComm.ExecuteReader(); 

     if (dr.Read()) 
     { 
      ProductName = dr["SER_TYPE"].ToString(); 

     } 

       oComm = new OleDbCommand(strSql, oConn); 
       dr = oComm.ExecuteReader(); 
       if (dr.Read()) 
       { 
        StaffId = dr["staff_id"].ToString(); 
        StaffEmail = dr["staff_email"].ToString(); 
        AccMgrName = dr["staff_name"].ToString(); 
        custName = dr["cust_name"].ToString(); 
        custid = dr["cust_id"].ToString(); 
        customerGroup = dr["cust_group"].ToString(); 

       } 



       oComm = new OleDbCommand(strSql2, oConn); 
       dr = oComm.ExecuteReader(); 
       if (dr.Read()) 
       { 

        StaffEmail += " , " + dr["staff_email"].ToString(); 
        AccMgrName += "/" + dr["staff_name"].ToString(); 

       } 
      } 





      oConn.Close(); 

      if (StaffId != "subh") 
      { 
       if ((customerGroup == "Commented SoHo") || (customerGroup == "Commented Small & Medium Enterprise")) 
       { 
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 

        msg.From = new MailAddress("[email protected]"); 
        msg.To.Add(StaffEmail); 

        string font = "Arabic Transparent"; 
        string body = ""; 
        msg.Subject = "CP " + cPNumber + " with Product " + ProductName + " has been created for " + custName; 
        body = ""; 
        body = body + "<html><head>"; 
        body = body + "  <head>"; 
        body = body + "     <meta http-equiv='Content-Type' content='text/html; charset=windows-1256'>"; 
        body = body + "  </head>"; 
        body = body + "<body>"; 
        body = body + "<table align='center' width='80%' border='0' cellspacing='0' cellpadding='0'>"; 
        body = body + "      <!-- logos -->"; 
        body = body + "      <tr align=center>"; 
        body = body + "       <td bgcolor='#FFFFFF'> </td>"; 

        body += "<img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >"; 
        body += "<tr><h2 style='font-size: 21px;font-weight: bold;color: #CC6600;line-height: 28px;'><b><center>Business Solutions - Service Request Acknowledgement</center></b></h2>"; 
        body += "</tr>"; 
        body += "<hr>"; 


        body += " <br><tr style='font-family:Tahoma; font-size:9pt;'; > Dear " + AccMgrName + ",</tr><br><br>"; 
        body += "<br>"; 
        body += " <tr style='font-family:Tahoma; font-size:9pt; margin-left: 80px'>Please be advised that a <B> VIP <B> fax has been received from " + custName + " and CP No. " + cPNumber + " for Product " + ProductName + "has been created.<br><br></tr>"; ; 

        body += "<img alt=\"\" hspace=0 src=\"cid:imageId1\" align=baseline border=0 >"; 
        body += "<hr>"; 
        body += "<tr style='font-family:Tahoma; font-size:7pt; margin-left: 80px'>Note: This E-mail address can not receive messages. Please do not reply to this e-mail.</tr>"; 
        body += "</body></html"; 
        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); 
        LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\qtel_Image.JPEG", "image/jpeg"); 
        LinkedResource imagelink1 = new LinkedResource(Server.MapPath(".") + @"\mail.PNG", "image/png"); 
        //return "imagelink" + imagelink.ToString(); 
        imagelink.ContentId = "imageId"; 
        imagelink1.ContentId = "imageId1"; 
        imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; 
        imagelink1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; 
        htmlView.LinkedResources.Add(imagelink); 
        htmlView.LinkedResources.Add(imagelink1); 
        msg.AlternateViews.Add(htmlView); 
        SmtpClient smtp = new SmtpClient(); 
        //smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; 
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
        //smtp.Host = "192.168.57.152"; 
        smtp.Host = "172.16.224.47"; 
        //smtp.Host = "172.16.224.109"; 

        smtp.Send(msg);//comment till here to disable mail 
       } 
       else { 
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 

        msg.From = new MailAddress("[email protected]"); 

        string font = "Arabic Transparent"; 
        string body = ""; 
        msg.Subject = "CP " + cPNumber + " with Product " + ProductName + " has been created for " + custName; 
        body = ""; 
        body = body + "<html><head>"; 
        body = body + "  <head>"; 
        body = body + "     <meta http-equiv='Content-Type' content='text/html; charset=windows-1256'>"; 
        body = body + "  </head>"; 
        body = body + "<body>"; 
        body = body + "<table align='center' width='80%' border='0' cellspacing='0' cellpadding='0'>"; 
        body = body + "      <!-- logos -->"; 
        body = body + "      <tr align=center>"; 
        body = body + "       <td bgcolor='#FFFFFF'> </td>"; 

        body += "<img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >"; 
        body += "<tr><h2 style='font-size: 21px;font-weight: bold;color: #CC6600;line-height: 28px;'><b><center>Business Solutions - Service Request Acknowledgement</center></b></h2>"; 
        body += "</tr>"; 
        body += "<hr>"; 


        body += " <br><tr style='font-family:Tahoma; font-size:9pt;'; > Dear " + AccMgrName + ",</tr><br><br>"; 
        body += "<br>"; 
        body += " <tr style='font-family:Tahoma; font-size:9pt; margin-left: 80px'>Please be advised that a fax has been received from " + custName + " and CP No. " + cPNumber + " for Product " + ProductName + " has been created.<br><br></tr>"; ; 

        body += "<hr>"; 
        body += "<tr style='font-family:Tahoma; font-size:7pt; margin-left: 80px'>Note: This E-mail address can not receive messages. Please do not reply to this e-mail.</tr>"; 
        body += "</body>"; 
        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); 
        LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\qtel_Image.JPEG", "image/jpeg"); 
        //return "imagelink" + imagelink.ToString(); 
        imagelink.ContentId = "imageId"; 
        imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; 
        htmlView.LinkedResources.Add(imagelink); 
        msg.AlternateViews.Add(htmlView); 
        SmtpClient smtp = new SmtpClient(); 
        //smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; 
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
        //smtp.Host = "192.168.57.152"; 
        smtp.Host = "172.16.224.47"; 
        //smtp.Host = "172.16.224.109"; 

        smtp.Send(msg);//comment till here to disable mail 
       } 
      } 
      return ("successfully inserted"); 
     } 
    } 
    catch (Exception e) 
    { 
     Console.Write("SQL Exception: " + e.Message); 
     return ("Error :" + e.Message); 
    } 
} 
+0

したがって、特定の条件が真のときに複数の「受信者」を含めるにはどうすればよいですか? –

+0

はいこれはまさに私が必要とするものです。製品名が終了したときにレシピエントが追加されたり、残りの部分が同じように残っているようです。 –

答えて

0

それは少し不明である、私はあなたがProductNameTerminationTermination_ALLのいずれかであるとき、あなたはこのような何かを行うことができ、複数のrecipentsをしたいと仮定します。

msg.To.Add(StaffEmail); 

if(ProductName.Equals("Termination", StringComparison.InvariantCultureIgnoreCase) 
|| ProductName.Equals("Termination_ALL", StringComparison.InvariantCultureIgnoreCase)) 
{ 
    // additional emails. 
    msg.To.Add("email1"); 
    msg.To.Add("email2");   
} 


msg.From = new MailAddress("[email protected]"); 
+0

ありがとう、これは私が正確に必要なものですが、私は受信者を追加したいので、私はstaffemailが同じIDをreaminする場所に新しいIDを追加しますか? IF(ProductName.Equals( "終了"、StringComparison.InvariantCultureIgnoreCase) || ProductName.Equals( "Termination_ALL"、StringComparison.InvariantCultureIgnoreCase)) {msg.To.Add(StaffEmail)。 msg.To.Add( "email1"); msg.To.Add( "email2"); } else msg.To.Add(StaffEmail); –

+0

&親愛なる私のコード内のelseの中でこれを行うことができますか? –

+0

すべての場合に 'StaffEmail'を保存したいので、変更されたコードを使用してください。あなたはブロックする必要はありません。 –

関連する問題