ASP.Net C#| Send Email


  ASP.Net C#| Send Email


Here I write a code to send email and its 100% working if you pass correct value.

HTML:

<div class="container">
       <div style="min-height30px">
           <label id="lblMessage" runat="server"></label>
       </div>
       <div class="row">
           <div class="col-md-6">
 
               <div class="form-group">
                   <label>SMTP:</label>
                   <asp:TextBox ID="txtSMTP" runat="server" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <label>Port:</label>
                   <asp:TextBox ID="txtPort" runat="server" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <label>Username</label>
                   <asp:TextBox ID="txtUsername" runat="server" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <label>Password</label>
                   <asp:TextBox ID="txtPassword" runat="server" TextMode="password" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <label>SSL Enable</label>
                   <asp:CheckBox ID="chkSSL" runat="server"></asp:CheckBox>
               </div>
           </div>
           <div class="col-md-6">
               <div class="form-group">
                   <label>From:</label>
                   <asp:TextBox ID="txtFrom" runat="server" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <label>Display Name:</label>
                   <asp:TextBox ID="txtFromDisplay" runat="server" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <label>To:</label>
                   <asp:TextBox ID="txtTo" runat="server" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <label>Subject:</label>
                   <asp:TextBox ID="txtSubject" runat="server" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <label>Body:</label>
                   <asp:TextBox ID="txtBody" runat="server" CssClass="form-control"></asp:TextBox>
               </div>
               <div class="form-group">
                   <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" CssClass="btn btn-success" />
               </div>
           </div>
       </div>
   </div>

Code:

using System;
using System.Net;
using System.Net.Mail; 
protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                var mailmessage = new MailMessage { From = new MailAddress(txtFrom.Text.Trim(), txtFromDisplay.Text.Trim()) };
 
                mailmessage.To.Add(txtTo.Text.Trim());
                mailmessage.Subject = txtSubject.Text.Trim();
                mailmessage.Body = txtBody.Text.Trim();
                mailmessage.IsBodyHtml = true;
 
                SmtpClient smtpClient = null;
                smtpClient = new SmtpClient();
 
                smtpClient.Host = txtSMTP.Text.Trim();
                smtpClient.Port = Convert.ToInt32(txtPort.Text.Trim());
                smtpClient.UseDefaultCredentials = false;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;                
                smtpClient.Credentials = new NetworkCredential(txtUsername.Text.Trim(), txtPassword.Text.Trim());
 
                if (chkSSL.Checked)
                    smtpClient.EnableSsl = true;
                else
                    smtpClient.EnableSsl = true;
 
                smtpClient.Send(mailmessage);
                lblMessage.InnerText = "Email sent.";
            }
            catch (Exception ex)
            {
                lblMessage.InnerText = ex.Message;
            }
        }
 
        public static string GenerateRandomString(int length)
        {
            //It will generate string with combination of small,capital letters and numbers
            char[] charArr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
            string randomString = string.Empty;
            Random objRandom = new Random();
            for (int i = 0; i < length; i++)
            {
                //Don't Allow Repetation of Characters
                int x = objRandom.Next(1, charArr.Length);
                if (!randomString.Contains(charArr.GetValue(x).ToString()))
                    randomString += charArr.GetValue(x);
                else
                    i--;
            }
            return randomString;
        }

Method 2

 using (MailMessage email = new MailMessage())
                {
                    email.From = new MailAddress(contact.Email);
                    email.To.Add(toEmail);
                    email.Subject = "Hello World";
                    email.Body = "<h1>Hello</h1>";
                    email.IsBodyHtml = true;
                   email.Attachments.Add(new Attachment("C:\\file.zip"));

                    using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
                    {
                        smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["fromUserName"], password);
                        smtp.EnableSsl = true;
                        smtp.Send(email);
                    }
                }

After 2024

Follow the steps mentioned https://mailtrap.io/blog/gmail-smtp/


Enjoy !!!

:)

 

1 comment:

  1. Hi,

    Use below settings to send email using Gmail

    Host = "smtp.gmail.com"
    Port = 587

    And then set security in Gmail account

    Open link - https://myaccount.google.com/security
    set security - Allow less secure apps: ON

    Thanks,
    Rohit

    ReplyDelete