Comment envoyer un email en ASP.NET C #

Je suis très nouveau dans la zone ASP.NET C #. Je compte envoyer un mail via ASP.NET C # et voici l’ adresse SMTP de mon fournisseur d’access :

smtp-proxy.tm.net.my 

Voici ce que j’ai essayé de faire, mais j’ai échoué.

   Email Test Page  
Message to:
Message from:
Subject:
Message Body:


Et ci-dessous est mon code-behind :

 using System; using System.Web.UI.WebControls; using System.Net.Mail; public partial class SendMail : System.Web.UI.Page { protected void Btn_SendMail_Click(object sender, EventArgs e) { MailMessage mailObj = new MailMessage( txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text); SmtpClient SMTPServer = new SmtpClient("127.0.0.1"); try { SMTPServer.Send(mailObj); } catch (Exception ex) { Label1.Text = ex.ToSsortingng(); } } } 

PS: Je suis désolé de ne pas avoir compris le concept de récepteur / expéditeur SMTP et j’essaie donc de comprendre tout le concept d’ici.

Passez simplement par le code ci-dessous.

 SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25); smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "myIDPassword"); smtpClient.UseDefaultCredentials = true; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.EnableSsl = true; MailMessage mail = new MailMessage(); //Setting From , To and CC mail.From = new MailAddress("info@MyWebsiteDomainName", "MyWeb Site"); mail.To.Add(new MailAddress("info@MyWebsiteDomainName")); mail.CC.Add(new MailAddress("[email protected]")); smtpClient.Send(mail); 

Essayez plutôt d’utiliser ce code. Remarque: Dans “depuis l’adresse”, indiquez votre identifiant et votre mot de passe corrects.

 protected void btn_send_Click(object sender, EventArgs e) { System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); mail.To.Add("to gmail address"); mail.From = new MailAddress("from gmail address", "Email head", System.Text.Encoding.UTF8); mail.Subject = "This mail is send from asp.net application"; mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = "This is Email Body Text"; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; mail.Priority = MailPriority.High; SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential("from gmail address", "your gmail account password"); client.Port = 587; client.Host = "smtp.gmail.com"; client.EnableSsl = true; try { client.Send(mail); Page.RegisterStartupScript("UserMsg", ""); } catch (Exception ex) { Exception ex2 = ex; ssortingng errorMessage = ssortingng.Empty; while (ex2 != null) { errorMessage += ex2.ToSsortingng(); ex2 = ex2.InnerException; } Page.RegisterStartupScript("UserMsg", ""); } } 

Vous pouvez essayer ceci en utilisant Hotmail comme ceci: –

 MailMessage o = new MailMessage("From", "To","Subject", "Body"); NetworkCredential netCred= new NetworkCredential("Sender Email","Sender Password"); SmtpClient smtpobj= new SmtpClient("smtp.live.com", 587); smtpobj.EnableSsl = true; smtpobj.Credentials = netCred; smtpobj.Send(o); 

Essayez ce qui suit:

 try { var fromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"].ToSsortingng(); var fromEmailDisplayName = ConfigurationManager.AppSettings["FromEmailDisplayName"].ToSsortingng(); var fromEmailPassword = ConfigurationManager.AppSettings["FromEmailPassword"].ToSsortingng(); var smtpHost = ConfigurationManager.AppSettings["SMTPHost"].ToSsortingng(); var smtpPort = ConfigurationManager.AppSettings["SMTPPort"].ToSsortingng(); ssortingng body = "Your registration has been done successfully. Thank you."; MailMessage message = new MailMessage(new MailAddress(fromEmailAddress, fromEmailDisplayName), new MailAddress(ud.LoginId, ud.FullName)); message.Subject = "Thank You For Your Registration"; message.IsBodyHtml = true; message.Body = body; var client = new SmtpClient(); client.Credentials = new NetworkCredential(fromEmailAddress, fromEmailPassword); client.Host = smtpHost; client.EnableSsl = true; client.Port = !ssortingng.IsNullOrEmpty(smtpPort) ? Convert.ToInt32(smtpPort) : 0; client.Send(message); } catch (Exception ex) { throw (new Exception("Mail send failed to loginId " + ud.LoginId + ", though registration done.")); } 

Et puis dans votre web.config append les éléments suivants entre

       

Check it out …. ça marche

http://www.aspnettutorials.com/tutorials/email/email-aspnet2-csharp/

 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { try { MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text); SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text); emailClient.Send(message); litStatus.Text = "Message Sent"; } catch (Exception ex) { litStatus.Text=ex.ToSsortingng(); } } } 

Cela fonctionne parfaitement pour moi.

  public void sendEmail(ssortingng body) { if (Ssortingng.IsNullOrEmpty(email)) return; try { MailMessage mail = new MailMessage(); mail.To.Add(email); mail.To.Add("[email protected]"); mail.From = new MailAddress("[email protected]"); mail.Subject = "sub"; mail.Body = body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address smtp.Credentials = new System.Net.NetworkCredential ("[email protected]", "Yourpassword"); // ***use valid credentials*** smtp.Port = 587; //Or your Smtp Email ID and Password smtp.EnableSsl = true; smtp.Send(mail); } catch (Exception ex) { print("Exception in sendEmail:" + ex.Message); } } 
 MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text); mm.Subject = txtSubject.Text; mm.Body = txtBody.Text; if (fuAttachment.HasFile)//file upload select or not { ssortingng FileName = Path.GetFileName(fuAttachment.PostedFile.FileName); mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName)); } mm.IsBodyHtml = false; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text); smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; smtp.Send(mm); Response.write("Send Mail"); 

Afficher la vidéo: https://www.youtube.com/watch?v=bUUNv-19QAI

Si vous souhaitez générer vos corps de courrier électronique dans le razor, vous pouvez utiliser Mailzory . Vous pouvez également télécharger le paquet nuget ici .

 // template path var viewPath = Path.Combine("Views/Emails", "hello.cshtml"); // read the content of template and pass it to the Email constructor var template = File.ReadAllText(viewPath); var email = new Email(template); // set ViewBag properties email.ViewBag.Name = "Johnny"; email.ViewBag.Content = "Mailzory Is Funny"; // send email var task = email.SendAsync("[email protected]", "subject"); task.Wait() 

C’est le script le plus facile à tester.

 <%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.Net.Mail" %>    

Vous pouvez essayer MailKit

  MimeMessage mailMessage = new MimeMessage(); mailMessage.From.Add(new MailboxAddress(senderName, [email protected])); mailMessage.Sender = new MailboxAddress(senderName, [email protected]); mailMessage.To.Add(new MailboxAddress(emailid, emailid)); mailMessage.Subject = subject; mailMessage.ReplyTo.Add(new MailboxAddress(replyToAddress)); mailMessage.Subject = subject; var builder = new BodyBuilder(); builder.TextBody = "Hello There"; try { using (var smtpClient = new SmtpClient()) { smtpClient.Connect("HostName", "Port", MailKit.Security.SecureSocketOptions.None); smtpClient.Authenticate("[email protected]", "password"); smtpClient.Send(mailMessage); Console.WriteLine("Success"); } } catch (SmtpCommandException ex) { Console.WriteLine(ex.ToSsortingng()); } catch (Exception ex) { Console.WriteLine(ex.ToSsortingng()); } For more information you can [download package][1] [1]: https://www.nuget.org/packages/MailKit