コード例 #1
0
 public async Task<ActionResult> SendMail(MailVO mailvo)
 {
     List<String> maillist = new List<string>();
     maillist.Add("*****@*****.**");
     maillist.Add("*****@*****.**");
     maillist.Add("*****@*****.**");
     mailvo.MailList = maillist;
     string result = await MailUtil.SendAsyncMail(mailvo);
     return RedirectToAction("SendMail");
 }
コード例 #2
0
        public static async Task<String> SendAsyncMail(MailVO mailvo)
        {
            string username, password;
            username = mailvo.SenderMail;
            password = mailvo.SenderPassword;
            if(ConfigurationManager.AppSettings["Mail"] != null)
                username = ConfigurationManager.AppSettings["Mail"].ToString(); // appConfig dosyası içindeki AppSettings altındaki Mail key in value değeri getirilir.
            if (ConfigurationManager.AppSettings["Mail"] != null)
                password = ConfigurationManager.AppSettings["MailPassword"].ToString();

            MailMessage message = new MailMessage();
            foreach (var mail in mailvo.MailList)
            {
                message.To.Add(new MailAddress(mail));
            }
            message.From = new MailAddress(username);  // replace with valid value
            message.Subject = mailvo.Subject;
            message.Body = mailvo.Body;
            message.IsBodyHtml = true;

            

            using (var smtp = new SmtpClient())
            {
                NetworkCredential credential = new NetworkCredential
                {
                    UserName = username,  // replace with valid value
                    Password = password  // replace with valid value
                };
                smtp.Credentials = credential;
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                try
                {
                    await smtp.SendMailAsync(message); // mail asenkron olarak gönderilir.
                    return "true";
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                    throw;
                }

            }
        }