C# 如果满足某些条件,需要在msg函数上添加沉淀

C# 如果满足某些条件,需要在msg函数上添加沉淀,c#,.net,C#,.net,这是我的代码,请帮忙 当字符串ProductName为“Termination”和“Termination\u ALL”时,我想向send email方法添加更多收件人 是否可以添加一个简单的if语句来添加收件人,而不是整个邮件 public string SendEmailAccountManager(string cPNumber) { string StaffId = ""; string StaffEmail = ""; string AccMgrName = "

这是我的代码,请帮忙

当字符串ProductName为“Termination”和“Termination\u ALL”时,我想向send email方法添加更多收件人

是否可以添加一个简单的if语句来添加收件人,而不是整个邮件

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("OoredooBusOrdMgmt@ooredoo.qa");
                    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("OoredooBusOrdMgmt@ooredoo.qa");

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

有点不清楚,我假设您需要多个收件人,当ProductName是终止或终止时,您可以这样做

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("OoredooBusOrdMgmt@ooredoo.qa");

所以问题是当某些条件为真时,如何包含多个接受方?是的,亲爱的,这正是我所需要的,比如当产品名称为终止时,接受方被添加,或者其他接受方保持不变。谢谢亲爱的,这正是我所需要的,但是我想添加收件人,那么我可以这样做吗?在这里,staffemail将在相同的位置添加新的ID?ifProductName.EqualDestination,StringComparison.InvariantCultureIgnoreCase | | | ProductName.EqualDestination |全部,StringComparison.InvariantCultureIgnoreCase{msg.To.AddStaffEmail;msg.To.Addemail1;msg.To.Addemail2;}else msg.To.AddStaffEmail;&亲爱的,如果在我的代码中的else中,我可以这样做吗?因为您希望为所有情况保留电子邮件,请使用修改后的代码。您不需要其他块。谢谢亲爱的,让我尝试一下,然后返回给您..:Habi Bhaiyya谢谢你这么多的工作就像一个符咒: