关于发送多封邮件

时间:2019-03-04 13:53:38

标签: asp.net

我试图发送一封邮件,但其中不包含多封邮件只有一封邮件。如何在asp.net中发送多封邮件? 并应为不同的邮件用户指定邮件内容

1 个答案:

答案 0 :(得分:1)

如果我对您的问题的解释正确,您的意思是多个收件人?

您可以尝试这样的事情:

string yourEmail = "mural@krish.com"; 
using (MailMessage send = new MailMessage())
{
    send.From = new MailAddress(yourEmail);
    send.To.Add("recipient@mailaddress.com");
    send.To.Add("recipient2@mailaddress.com");
}

否则,如果您需要多封电子邮件,可以尝试以下操作:

// if you want to run this multiple times, 
// just wrap this in a function, and call it however many times you want.
var startHTML = "<html><head></head><body>";
var endHTML = "</body></html>";
var emailArray = ["email1","email2","email3"]; 
foreach (string emailAddress in emailArray)
{
    SmtpClient smtpClient = new SmtpClient(); // your mailclient.
    MailMessage msg = new MailMessage();
    MailAddress to = new MailAddress(emailAddress);
    msg.IsBodyHtml = true;
    msg.Body = startHTML +
        "<p>Your Message :) </p>" +
         endHTML;
    msg.Subject = "Email Subject";
    msg.To.Add(to);

    smtpClient.Send(message);
}

OP询问:

“可以,但是我需要一次将不同的消息发送到不同的邮件地址”

var startHTML = "<html><head></head><body>";
var endHTML = "</body></html>";
var messageList = new List<KeyValuePair<string,string>>()
{
    new KeyValuePair<string, string>("recipient1@gmail.com", "your custom message1"),
    new KeyValuePair<string, string>("recipient2@gmail.com", "your custom message2"),
    new KeyValuePair<string, string>("recipient3@gmail.com", "your custom message3"),
    new KeyValuePair<string, string>("recipient4@gmail.com", "your custom message4"),
};

foreach (var receiver in messageList)
{
    SmtpClient smtpClient = new SmtpClient(); // your mailclient.
    MailMessage msg = new MailMessage();
    MailAddress to = new MailAddress(receiver.Key);
    msg.IsBodyHtml = true;
    msg.Body = startHTML +
        receiver.Value +
         endHTML;
    msg.Subject = "Email Subject";
    msg.To.Add(to);

    smtpClient.Send(message);
}