简体   繁体   English

如何从Web应用程序向多个收件人发送邮件?

[英]How to send mails to multiple reciepients from web application?

I have developed a asp.net Mvc 4 project and now i am planing to integrate a Mail system in my application.Initially i taught like integrating mail System in my existing web application but later i moved it to a console application using Scheduler to send mail at some time interval. 我已经开发了一个asp.net Mvc 4项目,现在我打算将Mail System集成到我的应用程序中。最初,我像在现有的Web应用程序中集成Mail System一样,但是后来我使用Scheduler将其移至控制台应用程序以发送邮件在一定的时间间隔。

My scenario is like i have a list of mail ids and i need to send mail to all these mail ids . 我的情况就像我有一个邮件ID列表,我需要向所有这些邮件ID发送邮件。 I have checked System.Web.Mail and i found i can only give one email address at a time. 我检查了System.Web.Mail,发现我一次只能提供一个电子邮件地址。 Is it possible in System.Web.Mail or is there any other library available to achieve my scenario. 是否可以在System.Web.Mail中使用或者是否有其他可用的库来实现我的方案。

To in System.Net.Mail is a MailAddressCollection ,so you can add how many addresses you need. ToSystem.Net.Mail中发送一个MailAddressCollection ,因此您可以添加所需的地址数。

MailMessage msg = new MailMessage();
msg.To.Add(...);
msg.To.Add(...);

Chris's answer is correct but you may want to also consider using a mail service. 克里斯的答案是正确的,但您可能还需要考虑使用邮件服务。 Here are some you could try - they all have a free tier to get started on. 您可以尝试以下方法-它们都有免费的入门层。

You can easily sent emails to more than one recipient. 您可以轻松地将电子邮件发送给多个收件人。 Here is a sample that uses a SMTP server to send an email to multiple addreses: 这是一个使用SMTP服务器将电子邮件发送到多个地址的示例:

//using System.Net.Mail;

public void SendEmail(){

    MailMessage email = new MailMessage();

    email.To.Add("first@email.com");
    email.To.Add("second@email.com");
    email.To.Add("third@email.com");

    email.From = new MailAddress("me@email.com");

    string smtpHost = "your.SMTP.host";
    int smtpPort = 25;

    using(SmtpClient mailClient = new SmtpClient(smtpHost, smtpPort)){
        mailClient.Send(email);
    }
}

Just a note: if you go with SMTP, you should probably have a look also on MSDN for the SmtpClient.Send method, just to be sure you are catching any related exceptions. 请注意:如果使用SMTP,则可能还应该在MSDN上查看SmtpClient.Send方法,以确保捕获任何相关异常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM