私は管理者であるすべての電子メールアドレスを返信し、それらに電子メールを送付したいと思います。今私は1つの電子メールアドレスにしか送ることができないし、私はそれをハードコーディングしなければならない(私はそれをハードコーディングしなければならないということを意味する)。しかし、私はAsp.net MVC、配列に電子メールの値を格納し、配列で予約されたそれらの電子メールに電子メールを送信する方法
public JsonResult SendEmailToUser()
{
bool result = false;
// My second question, Here I want to put those email array, But I don't know how to do
// If I hardcoded with one email .. let say [email protected], works fine
// But instead to send to one email address , I want to send to thos emails which are in my array
result = SendEmail("emails from array", "Here is my subject", "<p>Here is my message</p>");
return Json(result, JsonRequestBehavior.AllowGet);
}
そして、ここでは私のsendEmail(1つのハードコードされたメールアドレスで正常に動作します)
である)電子メール// Here is my first problem, to save them in array
public string UserEmails()
{
var emails = context.Users.Where(u => u.IsAdmin == true).Select(e => e.Email).ToArray();
// First question, How to save those emails into an array and return those email array then to use it in my SendEmailToUser();
}
の私の配列に基づいて、複数の電子メールに送信すると、ここに私SendEmailToUser(あります
public bool SendEmail(string toEmail, string subject, string emailBody)
{
try
{
string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString();
string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(senderEmail, senderPassword);
MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = UTF8Encoding.UTF8;
client.Send(mailMessage);
return true;
}
catch(Exception ex)
{
return false;
}
}