Sending Email With Asp.Net WebForm

doganhrn

New member
Joined
Feb 23, 2021
Messages
2
Programming Experience
1-3
Hi,

I send e-mails with the method I prepared. In the content of this e-mail, there is an html table containing the records responsible for the persons to be sent. However, at the moment, the mail is not sent in a private way. In other words, the mail sent to the people sends not only the records of the relevant person, but also the records of other people. However, for example, I want only data related to the records that he is responsible for to be sent to the person named x. I think for that, I need to check that the next record does not belong to the person concerned and exit the loop if the condition is valid. And then I have to loop again and continue from the next user. But somehow I could not achieve the result I wanted. I ask for your help.

My method is here:
private void SendMailIfNoComment()
    {
        using (SqlConnection conn = new SqlConnection(myDbConnect))
        {
            using (SqlCommand cmd = new SqlCommand("sp_CheckIfaCommentHasBeenMade", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                    int mailsayac = 0;
                    DataSet ds = new DataSet();
                    SqlDataAdapter adp = new SqlDataAdapter();
                    adp.SelectCommand = cmd;
                    adp.Fill(ds);
                    string MailSubject = string.Empty, danismanMail = string.Empty, MailBody = string.Empty, IstekTarihi = string.Empty, MailFrom = "xxx";
                    int i;
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MailSubject = "xxx";
                        MailBody = "<table style=\"border:0px;width:900px;font-family:Arial;font-size:12px;\"><tr>";
                        MailBody += "<td valign=\"top\" style=\"font-weight:bold;width:100px;border:1px solid navy\">İstek No</td>";
                        MailBody += "<td valign=\"top\" style=\"font-weight:bold;width:400px;border:1px solid navy\">İstek</td>";
                        MailBody += "<td valign=\"top\" style=\"font-weight:bold;width:100px;border:1px solid navy\">Sorumlu Danışman</td>";
                        MailBody += "<td valign=\"top\" style=\"font-weight:bold;width:100px;border:1px solid navy\">İstek Sorumlusu</td>";
                        MailBody += "<td valign=\"top\" style=\"font-weight:bold;width:100px;border:1px solid navy\">İstek Durumu</td>";
                        MailBody += "<td valign=\"top\" style=\"font-weight:bold;width:100px;border:1px solid navy\">İstek Tarihi</td>";
                        MailBody += "";
                        mailsayac = 0;

                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            danismanMail = row["DanismanMail"].ToString();
                                IstekTarihi = string.IsNullOrEmpty(row["istek_tarihi"].ToString()) == false ? string.Format("{0:dd.MM.yyyy}", row["istek_tarihi"]) : string.Empty;
                                MailBody += "<tr><td valign=\"top\" style=\"width:100px;border:1px solid navy\"><href="" + row["IstekNo"].ToString() + "\">" + row["IstekNo"].ToString() + "</a></td>";
                                MailBody += "<td valign=\"top\" style=\"width:400px;border:1px solid navy\">" + row["istek"].ToString() + "</td>";
                                MailBody += "<td valign=\"top\" style=\"width:100px;border:1px solid navy\">" + row["Danisman"].ToString() + "</td>";
                                MailBody += "<td valign=\"top\" style=\"width:100px;border:1px solid navy\">" + row["IstekSorumlusu"].ToString() + "</td>";
                                MailBody += "<td valign=\"top\" style=\"width:100px;border:1px solid navy\">" + row["durum"].ToString() + "</td>";
                                MailBody += "<td valign=\"top\" style=\"width:100px;border:1px solid navy\">" + IstekTarihi + "</td>";
                                mailsayac++;
                                
                            
                        }
                        MailBody += "</table>";

                            if (mailsayac > 0) MailUtils.SendMail(MailSubject, MailFrom, danismanMail.Split(';'), "smtp.yandex.com.tr", MailBody, true, Session["UserEmail"].ToString());

                }
            }
        }

    }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,583
Location
Chesapeake, VA
Programming Experience
10+
A better approach would be to first group your dataset rows by mail recipient first, then for each group compose and send out the mail. C#'s LINQ let's you do this grouping much more easily than if you had to do this in C, C++, or Pascal where you would have to roll your own grouping code, or try to do what you are doing above where you are trying to process all the data and figure out the groupings on the fly.
 

doganhrn

New member
Joined
Feb 23, 2021
Messages
2
Programming Experience
1-3
A better approach would be to first group your dataset rows by mail recipient first, then for each group compose and send out the mail. C#'s LINQ let's you do this grouping much more easily than if you had to do this in C, C++, or Pascal where you would have to roll your own grouping code, or try to do what you are doing above where you are trying to process all the data and figure out the groupings on the fly.
Thank you. I will try this.
 
Top Bottom