Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email in ASP.NET C#

I'm very new to the ASP.NET C# area. I'm planning to send a mail through ASP.NET C# and this is the SMTP address from my ISP:

smtp-proxy.tm.net.my 

Below is what I tried to do, but failed.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SendMail" %> <html> <head id="Head1" runat="server"><title>Email Test Page</title></head> <body>     <form id="form1" runat="server">         Message to: <asp:TextBox ID="txtTo" runat="server" /><br>         Message from: <asp:TextBox ID="txtFrom" runat="server" /><br>         Subject: <asp:TextBox ID="txtSubject" runat="server" /><br>         Message Body:<br>         <asp:TextBox ID="txtBody" runat="server" Height="171px" TextMode="MultiLine"  Width="270px" /><br>         <asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click" Text="Send Email" /><br>         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>     </form> </body> </html> 

And below is my 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.ToString();         }     } } 

PS: I'm sorry that I couldn't understand the receiver/sender SMTP concept, and so I am trying to understand the whole concept from here.

like image 846
SuicideSheep Avatar asked Aug 20 '13 03:08

SuicideSheep


People also ask

What is the code to send an email from an ASP NET application?

For sending email we need a SMTP Server, so in ASP.Net we have the SmtpClient class, using that class object we set its properties for the SMTP settings. SmtpClient client = newSmtpClient("smtp.gmail.com", 587);

How can send email with button click in asp net?

Provide the project a name, such as "Sending Emails" or another as you wish and specify the location. Then right-click on Solution Explorer and seelct "Add New Item" - "Default. aspx" page and one class file. Drag and drop three Text Boxes and two buttons.

Can C# send an email?

The standard approach to send an email using C# is SMTP (Simple Mail Transfer Protocol). It is a network protocol used to send emails over the internet. Additionally, it allows you to relayemails across multiple networks.

Can you send an email to a .net address?

NET and . NET Core come with built-in support for sending emails through the System. Net.Mail namespace. While it might seem like the easy choice, you will need an SMTP server for this to work.


2 Answers

Just go through the below code.

SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25);  smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "myIDPassword"); // smtpClient.UseDefaultCredentials = true; // uncomment if you don't want to use the network credentials 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); 
like image 122
Thamotharan Karuppiah Avatar answered Sep 20 '22 18:09

Thamotharan Karuppiah


Try using this code instead. Note: In the "from address" give your correct email id and password.

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", "<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");     }     catch (Exception ex)     {         Exception ex2 = ex;         string errorMessage = string.Empty;         while (ex2 != null)         {             errorMessage += ex2.ToString();             ex2 = ex2.InnerException;         }         Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");     } } 
like image 27
Thangamalai Avatar answered Sep 16 '22 18:09

Thangamalai