需要在msg函数上添加配方如果满足某些条件

时间:2016-04-05 05:39:46

标签: c# .net

这是我的Code Please Help。

当字符串ProductName为('终止','终止_ALL')时,我想向发送电子邮件方法添加更多收件人

是否可以添加简单的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);
    }
}

1 个答案:

答案 0 :(得分:0)

有点不清楚,我认为当ProductNameTerminationTermination_ALL时你需要多个配方,你可以这样做。

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