简体   繁体   English

如何从ASP.NET MVC中的SQL Server数据库向多个收件人发送电子邮件?

[英]How can I send email to multiple recipients from SQL Server database in ASP.NET MVC?

I want to send email to multiple recipients. 我想将电子邮件发送给多个收件人。 I retrieve the email addresses of multiple users from a SQL Server database, like this: 我从SQL Server数据库中检索多个用户的电子邮件地址,如下所示:

var students = (from u in db.Users
                where u.projectGroup == id
                select u.userEmail).ToArray();

This will return multiple email addresses like: user1@gmail.com user2@gmail.com and I try to send to multiple users like this 这将返回多个电子邮件地址,例如: user1@gmail.com user2@gmail.com ,我尝试发送给这样的多个用户

for (int h = 0; h < students.Length; h++)
{
    try
    {
        var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
        var smtpClient = new SmtpClient();

        var message = new MailMessage();
        message.To.Add(new MailAddress(students[h])); 
        message.Subject = "I'm interested in your project";
        message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
        message.IsBodyHtml = true;

        using (var smtp = new SmtpClient())
        {
            await smtp.SendMailAsync(message);
            return RedirectToAction("Sent");
        }
    }
    catch (Exception ex)
    {
        //MessageBox("Your Email address is not valid");
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Your Email address is not valid');", true);
    }
}

but this did not work, it sends an email to first user but another user did not receive the email. 但这不起作用,它会向第一位用户发送电子邮件,但另一位用户未收到该电子邮件。

How can I split each email in a one row to send an email to multiple recipients? 如何将每封电子邮件拆分为一行以将电子邮件发送给多个收件人?

Everything seems to be good. 一切似乎都很好。 However, your redirectToAction is executing for the first email and comes out of loop.After that control doesn't know how to continue for second record. 但是,您的redirectToAction正在为第一封电子邮件执行并退出循环。之后,该控件不知道如何继续第二封记录。 that's why your email wont be sent to second email. 这就是为什么您的电子邮件不会发送到第二封电子邮件的原因。 try below code you should be good. 尝试下面的代码,你应该很好。

for (var index= 0; index< students.Length; index++)
    {
        try
        {
            var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
            var smtpClient = new SmtpClient();

            var message = new MailMessage();
            message.To.Add(new MailAddress(students[index])); 
            message.Subject = "I'm interested in your project";
            message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
            message.IsBodyHtml = true;

            using (var smtp = new SmtpClient())
            {
                await smtp.SendMailAsync(message);
                       }
        }
        catch (Exception ex)
        {
            //MessageBox("Your Email address is not valid");
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Your Email address is not valid');", true);
        }
    }

     return RedirectToAction("Sent");

return RedirectToAction("Sent"); is the culprit. 是罪魁祸首。

This will stop your code after the first email. 这将在第一封电子邮件后停止您的代码。 Don't return from your method until they've all been sent: 所有方法发送完毕之前,请不要从方法中返回:

using (var smtp = new SmtpClient())
{
  for (int h = 0; h < students.Length; h++)
  {
    var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";

    var message = new MailMessage();
    message.To.Add(new MailAddress(students[h])); 
    message.Subject = "I'm interested in your project";
    message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
    message.IsBodyHtml = true;

    smtp.Send(message);
  }
}
return RedirectToAction("Sent");

NB I also removed the redundant try/catch (don't use it if you're not going to log the exception (although you should do that globally anyway), suppressing unexpected errors is an anti-pattern) and reduced the number of times you unnecessarily instantiate a brand new SmtpClient - it's fine to use the same one each time. 注意:我还删除了多余的try / catch(如果您不打算记录异常,则不要使用它(尽管您无论如何都要全局记录),抑制意外错误是一种反模式),并减少了次数您不必实例化全新的SmtpClient-每次都使用相同的SmtpClient是可以的。 Also, if you're going to await the async email call, there's no point using it - just use the regular Send instead. 另外,如果您要await异步电子邮件呼叫,则没有用处-只需使用常规的发送即可。

您是从循环内部返回SENT,因此它执行第一个项目然后退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在asp.net mvc中发送确认电子邮件 - How can I send a Confirmation Email in asp.net mvc 如何在ASP.NET MVC中使用邮政将电子邮件发送到数据库中的多个电子邮件地址 - How to send an email to multiple email addresses in the database using Postal in ASP.NET MVC 如何使用Asp.Net MVC数据库中的电子邮件地址在邮政上发送电子邮件 - How to send an email on Postal using an email address from the database Asp.Net MVC 如何以列表形式从数据库中检索文本(SQL Server和ASP.NET MVC) - How do I retrieve text from database in list form (SQL Server & ASP.NET MVC) 如何从Asp.net Mvc-3发送电子邮件? - How to send email from Asp.net Mvc-3? 如何将希伯来语文本从 C# ASP.NET 参数发送到 SQL Server? - How can I send Hebrew text to SQL Server from a C# ASP.NET parameter? SQL Server数据库中ASP.NET MVC中的Bootstrap滑块 - Bootstrap slider in ASP.NET MVC from SQL Server database 如何使用户能够从ASP.NET MVC发送电子邮件? - How do I enable users to send email from ASP.NET MVC? 发送电子邮件并将其保存在数据库(ASP.NET MVC)中 - Send email and save it in database (ASP.NET MVC) 如何获取文本框或下拉列表的值并发送到ASP.NET MVC4中的数据库 - How can I get value of textbox or dropdownlist and send to database in ASP.NET MVC4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM