当前位置: 首页>>代码示例>>C#>>正文


C# SmtpClient.SendMailAsync方法代码示例

本文整理汇总了C#中System.Net.Mail.SmtpClient.SendMailAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SmtpClient.SendMailAsync方法的具体用法?C# SmtpClient.SendMailAsync怎么用?C# SmtpClient.SendMailAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Mail.SmtpClient的用法示例。


在下文中一共展示了SmtpClient.SendMailAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Contact

        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("[email protected]"));
                message.From = new MailAddress("[email protected]");
                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 = "[email protected]",
                        Password = "Ptaszysko04464"
                    };
                    smtp.Credentials = credential;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);
                    return RedirectToAction("/Sent");
                }
            }
            return View(model);
        }
开发者ID:gornikp,项目名称:ProjektMVC,代码行数:33,代码来源:HomeController.cs

示例2: SendMailReminder

        public async static Task<string> SendMailReminder(EmailFormModel model)
        {
                var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message = new MailMessage();
                message.To.Add(new MailAddress("[email protected]"));  // replace with valid value 
                message.From = new MailAddress("[email protected]");  // replace with valid value
                message.Subject = "Your email subject";
                message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
                message.IsBodyHtml = true;

                using (var smtp = new SmtpClient())
                {
                    var credential = new NetworkCredential
                    {
                        UserName = "[email protected]",  // replace with valid value
                        Password = "password"  // replace with valid value
                    };
                    smtp.Credentials = credential;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);
                    return "conplete";
                }
        }
开发者ID:card4ash,项目名称:mailsend,代码行数:25,代码来源:Program.cs

示例3: SendAsync

        public async Task SendAsync(IdentityMessage message)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(message.Destination);
                mail.From = new MailAddress("[email protected]","SEATS");
                mail.Subject = message.Subject;
                mail.Body = message.Body;
                mail.IsBodyHtml = true;

                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.Host = "198.60.12.9";
                    smtp.Port = 25;
                    //smtp.UseDefaultCredentials = true;

                    await smtp.SendMailAsync(mail);
                }
                mail.Dispose();

                
            }
            catch (Exception ex)
            {
                throw new HttpException(500, "Confirmation Email Not Sent! " + ex.Message);
            }
        }
开发者ID:sshiotani,项目名称:SEATS,代码行数:28,代码来源:EmailService.cs

示例4: Contact

 public async Task<ActionResult> Contact(EmailFormModel model)
 {
     if (ModelState.IsValid)
     {
         var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
         var message = new MailMessage();
         message.To.Add(new MailAddress("[email protected]"));  
         message.From = new MailAddress(model.FromEmail);  
         message.Subject = model.Subject;
         message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
         message.IsBodyHtml = true;
         using (var smtpClient = new SmtpClient())
         {
             var credential = new NetworkCredential
             {
                 UserName = "[email protected]", 
                 Password = "placeholder"  
             };
             smtpClient.Credentials = credential;   //was getting error fixed with
             smtpClient.Host = "smtp.gmail.com";   //security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps
             smtpClient.Port = 587; 
             smtpClient.EnableSsl = true;
             await smtpClient.SendMailAsync(message);
             return RedirectToAction("Sent");
         }
     }
     return View(model);
 }
开发者ID:happypoofysquirrel,项目名称:PortfolioFinal,代码行数:28,代码来源:HomeController.cs

示例5: ConfigAndSendMessage

        private async Task ConfigAndSendMessage(IdentityMessage message)
        {
            _smtpServer = "msex1.office.finam.ru";
            _from = "[email protected]";
            try
            {
                var email = new MailMessage(new MailAddress(_from), new MailAddress(message.Destination))
                {
                    Subject = message.Subject,
                    Body = message.Body,
                    IsBodyHtml = true
                };

                using (var client = new SmtpClient())
                {
                    client.Host = _smtpServer;
                    client.Port = 25;
                    client.Credentials = new NetworkCredential("[email protected]", "UfrRJzmuR89k");
                    // client.UseDefaultCredentials = true;
                    await client.SendMailAsync(email);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Send message error", ex.ToString());
            }
        }
开发者ID:Eugene-Ishkov,项目名称:RestService,代码行数:27,代码来源:MailService.cs

示例6: Contact

        public async Task<ActionResult> Contact(FormCollection body)
        {
            var from = String.IsNullOrEmpty(body["Email"]) ? "[email protected]" : body["Email"];
            var comments = body["Comments"];

            var content = string.Format("<p>Email From: {0}</p><p>Message:</p><p>{1}</p>", from, comments);

            var message = new MailMessage();
            message.From = new MailAddress("[email protected]"); // "[email protected]"; replace with valid value
            message.To.Add(new MailAddress("[email protected]")); // "[email protected]"; replace with valid value 
            message.Subject = "Email from my .NET portfolio site!";
            message.Body = content;
            message.IsBodyHtml = true;

            using (var smtp = new SmtpClient())
            {
                var credential = new NetworkCredential
                {
                    UserName = "[email protected]", // "[email protected]"; replace with valid value
                    Password = "splhclo!" // "Password"; replace with valid value
                };
                smtp.Credentials = credential;
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                await smtp.SendMailAsync(message);
                return RedirectToAction("ContactThanks");
            }
        }
开发者ID:billluckett,项目名称:PortfolioSite,代码行数:29,代码来源:HomeController.cs

示例7: InviaEmailAsync

        /// <summary>
        /// 
        /// </summary>
        /// <param name="smtpClient"></param>
        /// <param name="MailFrom_Address"></param>
        /// <param name="MailTo_Address"></param>
        /// <param name="Subject"></param>
        /// <param name="Body"></param>
        /// <param name="IsBodyEncoded"></param>
        /// <param name="IsBodyHtml"></param>
        /// <param name="Priority"></param>
        /// <param name="Attachments">DisposableList<Attachment></param>
        /// <returns></returns>
        public static async Task InviaEmailAsync(SmtpClient smtpClient,
                                     MailAddress MailFrom_Address, string MailsTo,
                                     string Subject, string Body,
                                     bool IsBodyEncoded = false,
                                     bool IsBodyHtml = false,
                                     MailPriority Priority = MailPriority.Normal,
                                     List<Attachment> Attachments = null)
        {
            using (MailMessage mailMsg = new MailMessage())
            {
                mailMsg.From = MailFrom_Address;
                mailMsg.To.Add(MailsTo);

                mailMsg.Subject = Subject;
                string body = Body;
                if (IsBodyEncoded)
                    body = System.Net.WebUtility.HtmlDecode(body);
                mailMsg.Body = body;
                mailMsg.IsBodyHtml = IsBodyHtml;
                mailMsg.Priority = Priority;

                //allegati
                if (Attachments != null && Attachments.Count > 0)
                    foreach (var attach in Attachments)
                        mailMsg.Attachments.Add(attach);

                await smtpClient.SendMailAsync(mailMsg).ConfigureAwait(false);
            }
        }
开发者ID:Simone000,项目名称:Utils,代码行数:42,代码来源:EmailManager.cs

示例8: Submit

        public async Task<ActionResult> Submit(Email email)
        {
            if (ModelState.IsValid)
            {
                db.Emails.Add(email);
                db.SaveChanges();
                var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message = new MailMessage();
                message.To.Add(new MailAddress("[email protected]"));  // replace with valid value 
                message.From = new MailAddress(email.Email1);  // replace with valid value
                message.Subject = "Your email subject";
                message.Body = string.Format(body, email.Name, email.Email1, email.Request);
                message.IsBodyHtml = true;

                using (var smtp = new SmtpClient())
                {
                    var credential = new NetworkCredential
                    {
                        UserName = "[email protected]",  //sender's username
                        Password = "sujaykodamala"  // sender's Password
                    };
                    smtp.Credentials = credential;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);
                    return RedirectToAction("Index");
                }
            }
            return View();
        }
开发者ID:sujaykodamala,项目名称:CodeFirst_ASP,代码行数:31,代码来源:EmailController.cs

示例9: btnSend_Click

		private async void btnSend_Click(object sender, RoutedEventArgs e){
			try {
				_sendingReport = true;
				IsHitTestVisible = false;
				lblInfo.Visibility = Visibility.Visible;
				lblInfo.Text = "Sending Report...";
				var from = new MailAddress("[email protected]", "PCSX2 Tester");
				var to = new MailAddress("[email protected]", "PCSX2 Developer");
				var smtp = new SmtpClient {
					Host = "smtp.gmail.com",
					Port = 0x24b,
					EnableSsl = true,
					DeliveryMethod = SmtpDeliveryMethod.Network,
					Credentials = new NetworkCredential(from.Address, "[email protected]"),
					Timeout = 0x4e20
				};
				var asyncVariable0 = new MailMessage(from, to) {
					Subject = "Error Report",
					Body = Message + "\n\n\n" + StackTrace
				};
				using (var message = asyncVariable0) {
					await smtp.SendMailAsync(message);
				}
				_sendingReport = false;
				lblInfo.Text = "Report Sent!";
				await Task.Delay(0x3e8);
				Environment.Exit(0);
			}
			catch {
				Environment.Exit(0);
			}
		}
开发者ID:CyberFoxHax,项目名称:PCSXBonus,代码行数:32,代码来源:wndErrorReport.xaml.cs

示例10: SendEmailAsync

        public async static TaskEventHandler SendEmailAsync(string email, string subject, string message)
        {
            try
            {
                var _email = "[email protected]";
                var _epass = ConfigurationManager.AppSettings["EmailPassword"];
                var _dispName = "Sean";
                MailMessage myMessage = new MailMessage();
                myMessage.To.Add(email);
                myMessage.From = new MailAddress(_email, _dispName);
                myMessage.Subject = subject;
                myMessage.Body = message;
                myMessage.IsBodyHtml = true;

                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.EnableSsl = true;
                    smtp.Host = "smtp.live.com";
                    smtp.Port = 587;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential(_email, _epass);
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.SendCompleted += (s, e) => { smtp.Dispose(); };
                    await smtp.SendMailAsync(myMessage);
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
开发者ID:knolegsb,项目名称:MvcSimpleBlog,代码行数:31,代码来源:MessageServices.cs

示例11: Contact

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(EmailFormModel model)
        {
            if (ModelState.IsValid)
            {
                var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message = new MailMessage();
                message.To.Add(new MailAddress(model.ToEmail));
                message.From = new MailAddress(model.FromEmail);
                message.Subject = "test";
                message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
                message.IsBodyHtml = true;
                message.Attachments.Add(new Attachment(model.File.InputStream, Path.GetFileName(model.File.FileName)));

                using (SmtpClient smtp = new SmtpClient())
                {
                    NetworkCredential credetial = new NetworkCredential
                    {
                        UserName = model.FromEmail, Password = model.Password
                    };

                    smtp.Credentials = credetial;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);

                    return RedirectToAction("Sent");
                }
            }

            return View(model);
        }
开发者ID:cosminpopescu14,项目名称:MVCWebApp,代码行数:34,代码来源:HomeController.cs

示例12: SendAsync

        /// <summary>
        /// Sends an email
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static Task SendAsync(string subject, string body, string to)
        {
            // Credentials
            var credentialUserName = ConfigurationManager.AppSettings["SmtpUserName"];
            var sentFrom = ConfigurationManager.AppSettings["SmtpSendFrom"];
            var pwd = ConfigurationManager.AppSettings["SmtpPassword"];
            var server = ConfigurationManager.AppSettings["SmtpHostName"];
            var port = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]);

            // Configure the client
            var client = new SmtpClient(server);
            client.Port = port;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.EnableSsl = false;
            // Create the credentials
            client.Credentials = new NetworkCredential(credentialUserName, pwd);

            // Create the message
            var mail = new MailMessage(sentFrom, to);
            mail.IsBodyHtml = true;
            mail.Subject = subject;
            mail.Body = body;

            // Send
            return client.SendMailAsync(mail);
        }
开发者ID:KryptPad,项目名称:KryptPadWebsite,代码行数:34,代码来源:EmailHelper.cs

示例13: Contact

        public async Task<ActionResult> Contact(OTMST.Models.MVCEmail model)
        {
            if (ModelState.IsValid)
            {
                var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message = new MailMessage();
                message.To.Add(new MailAddress("[email protected]"));  // replace with valid value 
                message.To.Add(new MailAddress("[email protected]"));
                message.From = new MailAddress("[email protected]");  // replace with valid value
                message.Subject = "OTMST inquiry from web";
                message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
                message.IsBodyHtml = true;

                using (var smtp = new SmtpClient("localhost"))
                {
                    //var credential = new NetworkCredential
                    //{
                    //    UserName = "[email protected]",  // replace with valid value
                    //    Password = "password"  // replace with valid value
                    //};
                    //smtp.Credentials = credential;
                    //smtp.Host = "smtp-mail.outlook.com";
                    //smtp.Port = 587;
                    //smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);
                    return RedirectToAction("Sent");
                }
            }
            return View(model);
        }
开发者ID:dCogs,项目名称:OTMST,代码行数:30,代码来源:HomeController.cs

示例14: SendEmail

        public async Task SendEmail(string smtpFrom,string smtpTo, string messageSubject, string messageBody)
        {
            int smtpPort = int.Parse(ConfigurationManager.AppSettings["SMTP_Port"]);
            string smtpServer = ConfigurationManager.AppSettings["SMTP_Server"];
            //string smtpTo = ConfigurationManager.AppSettings["SMTP_To"];
            string smtpUser = ConfigurationManager.AppSettings["SMTP_User"];
            string smtpPassword = ConfigurationManager.AppSettings["SMTP_Password"];
            bool smtpUseSSL = ConfigurationManager.AppSettings["SMTP_UseSSL"] == "1";

            MailMessage mail = new MailMessage(smtpFrom, smtpTo);
            mail.Headers.Add("From", smtpFrom);
            SmtpClient client = new SmtpClient();
            client.Port = smtpPort;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = smtpServer;
            client.EnableSsl = smtpUseSSL;
            if(!string.IsNullOrWhiteSpace(smtpPassword))
                client.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword);
            client.Timeout = 20000;

            mail.Subject = messageSubject;
            mail.Body = messageBody;
            mail.IsBodyHtml = true;
            await client.SendMailAsync(mail);
        }
开发者ID:oanaticu,项目名称:CityAlert,代码行数:26,代码来源:EmailService.cs

示例15: SendEmail

 //Method sends the email
 private async Task<String> SendEmail(string name, string email, string messages, string phone, int Id)
 {
     var getRecordEmployee = db.Records.First(i => i.Id == Id).Record_Employee;
     var getEmployeeEmail = db.Employees.First(i => i.Id == getRecordEmployee).UserName;
     var getEmployeeName = db.Employees.First(i => i.Id == getRecordEmployee).FirstName;
     var message = new MailMessage();
     message.To.Add(new MailAddress(getEmployeeEmail));  // replace with receiver's email id  
     message.From = new MailAddress("[email protected]");  // replace with sender's email id 
     message.Subject = "EPMS Email Notification";
     message.Body = messages;
     message.IsBodyHtml = true;
     using (var smtp = new SmtpClient())
     {
         var credential = new NetworkCredential
         {
             UserName = "[email protected]",  // sender's email id
             Password = "Txtekgsn1"  // Sender password
         };
         smtp.Credentials = credential;
         smtp.Host = "smtp.live.com";
         smtp.Port = 587;
         smtp.EnableSsl = true;
         await smtp.SendMailAsync(message);
         return "sent";
     }
 }
开发者ID:GitSmudj1,项目名称:EPMS,代码行数:27,代码来源:PerformancesController.cs


注:本文中的System.Net.Mail.SmtpClient.SendMailAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。