GROVER_SYAAN
GROVER_SYAAN

Reputation: 365

ASP.net sending email to multiple recipients upon one click

i have got a datagrid displaying different rows, each has a link which sends email to multiple recipients who match with that row criteria for example

row1 col1 Japan col3 col4 (link send email to all japan users)...................

row2 col1 Sweden co3 col4 (link send email to all sweden users)

now when the user clicks on the link another page opens up with another grid view control showing all recipients for that row but before that sending email to all recipients.

problem is it takes a lot of time, i was wondering if my approach is wrong i am creating a mail message object for each row in the datarow

DataView dv;

if (dv.Count > 0)
{
    foreach (DataRow row in dv.Table.Rows)
    {
        StringBuilder sbEmailBody = new StringBuilder();
        sbEmailBody.Append("<div id='mail' style='height:400px;width:750px; padding:10px; margin: 0 auto; '>");
        sbEmailBody.Append("Hi " + row["FirstName"].ToString() + ", <br/><br/>");
        sbEmailBody.Append("You have registered with siteName, your details match with the following clinical trial.");
        sbEmailBody.Append("Please contact the below trial representative for further details</br></br>");
        sbEmailBody.Append("<b>Trial Name:</b> " + Session["trialName"].ToString() + "</br>");
        sbEmailBody.Append("<b>Contact Name:</b> " + Session["recName"].ToString() + "</br>");
        sbEmailBody.Append("<b>Contact Email:</b> " + Session["username"].ToString() + "</br>");
        sbEmailBody.Append("<b>Contact Telephone:</b> " + Session["tele"].ToString() + "</br>");
        sbEmailBody.Append("<hr> </hr>");
        sbEmailBody.Append("<a href='www.sitename.com' style=text-decoration:none><span id='logo' style='font-size:X-Large;font-weight:bold;color:Black;'>siteName</span></a><br/>");
        sbEmailBody.Append("<span id='stopEmail' style='font-size:Smaller;'>");
        sbEmailBody.Append("if you want to stop receiving emails from sitename please click <a href='www.bbc.co.uk' style=text-decoration:none>here</a>");
        sbEmailBody.Append("</span>");
        sbEmailBody.Append("</div>");


        MailMessage mailMessage = new MailMessage("[email protected]", row["EmailAdd"].ToString());
        mailMessage.Subject = "Clinical trial recruiter shown interest in your profile";
        mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
        mailMessage.Body = sbEmailBody.ToString();
        mailMessage.IsBodyHtml = true;
        SmtpClient smtpClient = new SmtpClient();

        smtpClient.Send(mailMessage);


    }
}

Upvotes: 0

Views: 1536

Answers (2)

Taj
Taj

Reputation: 1728

you can do a async operstion like thois to fasten things up

public delegate void SendMessage(SmtpClient client, MailMessage message);

 SendMessage Smessage = new SendMessage(ResultCallback);
            //smtp.Send(message);
            Smessage.BeginInvoke(smtpClient, message, null, null);





public void ResultCallback(SmtpClient client, MailMessage m)
        {
            try
            {
                client.Send(m);
                client.Dispose();
                m.Dispose();
            }
            catch
            {

            }
        }

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157098

You can create a Task to do the mailing for you.

Task.Run(() => YourMailMethod());

In the YourMailMethod method you can put the code you have already now.

The task will be run in the background then.

Upvotes: 1

Related Questions