【问题标题】:ASP.net sending email to multiple recipients upon one clickASP.net 一键向多个收件人发送电子邮件
【发布时间】:2014-02-28 12:52:12
【问题描述】:

我有一个显示不同行的数据网格,每个都有一个链接,可以将电子邮件发送给与该行条件匹配的多个收件人 例如

row1 col1 Japan col3 col4(链接发送电子邮件给所有日本用户)......................

row2 col1 瑞典 co3 col4(链接发送电子邮件给所有瑞典用户)

现在当用户点击链接时,另一个页面打开,另一个网格视图控件显示该行的所有收件人,但在此之前向所有收件人发送电子邮件。

问题是它需要很多时间,我想知道我的方法是否错误我正在为数据行中的每一行创建一个邮件消息对象

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("siteEmail@email.com", 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);


    }
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以创建一个Task 来为您发送邮件。

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

    YourMailMethod 方法中,您可以放入您现在已经拥有的代码。

    任务将在后台运行。

    【讨论】:

    • 你想让我在哪里调用task.run。目前我的代码在页面加载方法中,但我希望页面被加载但发送邮件的任务在后台运行
    • @GROVER_SYAAN:然后把它放在那里。
    【解决方案2】:

    你可以做一个像 thois 这样的异步操作来固定东西

    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
                {
    
                }
            }
    

    【讨论】:

    • 对你同样的问题,我的代码在 page_load 事件中运行,但我希望在邮件方法仍在后台运行时使用数据网格加载页面
    • @GROVER_SYAAN:这也是一个完美的解决方案。你为什么不试试这个?
    猜你喜欢
    • 2012-05-18
    • 1970-01-01
    • 2017-12-05
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多