在某些情况下发送电子邮件 [英] Sending Emails under certain conditions

查看:84
本文介绍了在某些情况下发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,在网格视图中创建了队列.该队列用于设置对某人需要访问的应用程序的访问权限.根据他们需要的应用程序的不同,可能需要将电子邮件发送到其他区域以处理该访问.如果不是,则表中没有电子邮件地址;如果是,则表中没有电子邮件地址.因此,如果要发送一封电子邮件,我想在页面加载上显示每个应用程序的发送情况,然后在发送电子邮件后将字段更改为true,这样它就不会再次发送.我是编程新手,我不确定该如何去做.我知道我需要以某种方式遍历记录,但不确定如何去做.我确实知道发送电子邮件的代码,但其余部分则不知道.有人可以帮帮我吗.谢谢.

这是一个更具体的问题,然后作为附加组件.我知道如何从数据库中提取信息.我需要知道的是如何遍历返回的数据.假设从数据库中提取了10个项目.在这10个人中,有3个人需要发送电子邮件.仅当该记录附有电子邮件地址时,才需要发送电子邮件.因此,如果电子邮件地址不为null并且尚未发送电子邮件,请发送电子邮件,然后将一个位字段更新为true,表示已发送电子邮件.我在解决该问题上遇到了很多麻烦.任何建议/代码都会有所帮助.谢谢.

这是我所拥有的代码,可以帮助其他人,我认为我必须做一个数组,但是我之前从未与他们合作过,所以这可能全错了.我收到该电子邮件地址不能为空,但是我以为我的if语句中排除了空值.请让我知道我可以采取什么措施来纠正我的问题:

I have a website where a queue is created in a gridview. This queue is for setting up access to applications that someone will need to have access to. Depending on what applications they need emails might need to be sent out to other areas to process that access. If not there is not an email address in the table if so there is an email address in the table. So what I would like is on the page load if an email needs to be sent out one gets sent out for each application and then a fieldd is changed to true after the email is sent so that it does not get sent again. I am new to programming and I am not sure how to go about doing this. I know I would need to loop through the records somehow but not sure how to go about doing that. I do know the code to send emails but not the rest. Can someone please help me. Thank you.

Here''s a more specific question then as an add-on. I know how to pull the information from the database. What I need to know is how to loop through the returned data. Say there are 10 items that get pulled from the database. 3 out of those 10 need to have an email sent out. Emails need to be sent only if there is an email address attached to that record. So if the email address it not null and an email has not been sent already, send an email and then update a bit field to true saying that the email has been sent. I am having a great amount of trouble figuring out how to do that. Any advise/code would be helpful. Thank you.

Here''s the code I do have can someone pleae help with the rest, i figured I have to do an array but I have not worked with them before so this is probably all wrong. I am getting that email address cannot be null but I thought I was excluding null values in my if statement. Please let me know what I can do to correct my issue:

protected void Page_Load(object sender, EventArgs e)
   {
       string request = Request.QueryString["request"];
       string requestLink = "<html>http://Default.aspx?request=" + request + "</html>";
       ArrayList emailAddressList = new ArrayList();

       string sqlString = "Select Application.ownerEmail, dbo.RequestItems.applicationProfile, dbo.RequestItems.emailToSystemAdmin from Application join dbo.ApplicationProfile on dbo.ApplicationProfile.application = dbo.Application.aAuid join dbo.RequestItems on dbo.RequestItems.applicationProfile = dbo.ApplicationProfile.apAuid where request = " + request;
       string connString = ConfigurationManager.ConnectionStrings["UserAccessConnectionString"].ConnectionString;
       SqlConnection sqlConn = new SqlConnection(connString);
       SqlCommand sqlComm = new SqlCommand(sqlString, sqlConn);

       sqlConn.Open();
       SqlDataReader reader = sqlComm.ExecuteReader();

       while (reader.Read())
       {
           emailAddressList.Add(reader["ownerEmail"].ToString());
           string applicationProfile = reader["applicationProfile"].ToString();
           bool emailToSystemAdmin = reader.GetBoolean(reader.GetOrdinal("emailToSystemAdmin"));

           //then loop if email is not null and emailToSystemAdmin is not true
           foreach (string emailAddress in emailAddressList)
           {
               if (emailAddress != null && emailToSystemAdmin != true)
               {
                   //thencode to send the email
                   SendEmail(emailAddress, "The followiing link is provided to you to set up User Access for a new employee.  Once IS sets up the network ID you can set-up access for this employee.  Check back if network ID is not set-up yet.</br>" + requestLink);

                   //then update database
                   string sqlString2 = "UPDATE RequestItems SET emailToSystemAdmin=1 where request= " + request + "and applicationProfile = " + applicationProfile;
                   SqlCommand sqlComm2 = new SqlCommand(sqlString2, sqlConn);

                   sqlComm2.ExecuteNonQuery();
               }

               //then after the loop and the update
               reader.Close();
               sqlConn.Close();
           }
       }
   }

推荐答案

使用google,有成千上万个使用C#发送电子邮件的可用示例.
Use google, there are THOUSANDS of available examples of sending email with C#.


我知道如何从数据库中提取信息.

I know how to pull the information from the database.

//sample
// Get the email field 
SqlDataReader Sqldr = SqlCmd1.ExecuteReader();
while (Sqldr.Read())
{
//check email is empty or not
string email=//email value
 if(//email not null and bit field is false)
 {
    //send email.
    // update bit field to true
 }
 else
 {
 //some code
 }
}


我在测试中接受了它,它应该只发送3封电子邮件,但发送6封电子邮件:

I got it accept in my test it should only send 3 emails but it is sending 6 instead:

protected void Page_Load(object sender, EventArgs e)
   {
       string request = Request.QueryString["request"];
       string requestLink = "<html>http://Default.aspx?request=" + request + "</html>";
       ArrayList emailAddressList = new ArrayList();

       string sqlString = "Select Application.ownerEmail, dbo.RequestItems.applicationProfile, dbo.RequestItems.emailToSystemAdmin from Application join dbo.ApplicationProfile on dbo.ApplicationProfile.application = dbo.Application.aAuid join dbo.RequestItems on dbo.RequestItems.applicationProfile = dbo.ApplicationProfile.apAuid where request = " + request;
       string connString = ConfigurationManager.ConnectionStrings["UserAccessConnectionString"].ConnectionString;
       SqlConnection sqlConn = new SqlConnection(connString);
       SqlCommand sqlComm = new SqlCommand(sqlString, sqlConn);

       sqlConn.Open();
       SqlDataReader reader = sqlComm.ExecuteReader();

       while (reader.Read())
       {
           emailAddressList.Add(reader["ownerEmail"].ToString());
           string applicationProfile = reader["applicationProfile"].ToString();
           bool emailToSystemAdmin = reader.GetBoolean(reader.GetOrdinal("emailToSystemAdmin"));

           //then loop if email is not null and emailToSystemAdmin is not true
           foreach (string emailAddress in emailAddressList)
           {
               if (emailAddress != "" && emailToSystemAdmin != true)
               {
                   //thencode to send the email
                   SendEmail(emailAddress, "The followiing link is provided to you to set up User Access for a new employee.  Once IS sets up the network ID you can set-up access for this employee.  Check back if network ID is not set-up yet. </br>" + requestLink);

                   //then update database
                   string sqlString2 = "UPDATE RequestItems SET emailToSystemAdmin=1 where request= " + request + "and applicationProfile = " + applicationProfile;
                   string connString2 = ConfigurationManager.ConnectionStrings["UserAccessConnectionString"].ConnectionString;
                   SqlConnection sqlConn2 = new SqlConnection(connString2);
                   SqlCommand sqlComm2 = new SqlCommand(sqlString2, sqlConn2);
                   sqlConn2.Open();
                   sqlComm2.ExecuteNonQuery();
                   sqlConn2.Close();
               }
           }
       }

       reader.Close();
       sqlConn.Close();
   }


帮助任何人吗?请


Help anyone? Please


这篇关于在某些情况下发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆