System.Net.Mail.SmtpClient.SendMailAsync C# (CSharp) Method

SendMailAsync() private method

private SendMailAsync ( string from, string recipients, string subject, string body ) : System.Threading.Tasks.Task
from string
recipients string
subject string
body string
return System.Threading.Tasks.Task
        public Task SendMailAsync(string from, string recipients, string subject, string body)
        {
            var message = new MailMessage(from, recipients, subject, body);
            return SendMailAsync(message);
        }

Same methods

SmtpClient::SendMailAsync ( MailMessage message ) : System.Threading.Tasks.Task

Usage Example

Example #1
1
        public async System.Threading.Tasks.Task<ActionResult> Contact(Email model)
        {
            ViewBag.Message = "Nice too meet you!";
            if (ModelState.IsValid)
            {
                var body = "";
                if (!string.IsNullOrEmpty(model.AboutProduct)) body = "<h2>Email From: {0} ({1})</h2></br><p>Message concerning {2}:</p><p>{3}</p>";
                else body = "<h2>Email From: {0} ({1})</h2></br><p>Message:</p><p>{3}</p>";

                var message = new MailMessage();
                message.To.Add(new MailAddress("*****@*****.**"));
                message.From = new MailAddress("*****@*****.**");
                message.Subject = "Your email subject";
                message.Body = string.Format(body, model.FromName, model.FromEmail, model.AboutProduct, model.Message);
                message.IsBodyHtml = true;

                using (var smtp = new SmtpClient())
                {
                    var credential = new NetworkCredential
                    {
                        UserName = "******",
                        Password = "******"
                    };
                    smtp.Credentials = credential;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);
                    return RedirectToAction("/Sent");
                }
            }
            return View(model);
        }
All Usage Examples Of System.Net.Mail.SmtpClient::SendMailAsync