Showing posts with label Email. Show all posts
Showing posts with label Email. Show all posts

Friday, 30 June 2017

Send Attachment in Email using Gmail in ASP.NET C#

Introduction:

In this topic, I will explain code to send attachment in email using Gmail in ASP.NET C#.

Description:

Usually, it is necessary to send email to user from ASP.NET application. We should use SMTP (Simple Mail Transfer Protocol) server to send email. If we don't have any SMTP server at that time we can use Gmail SMTP server to send email.

Following is source code to send email in ASP.NET :
  • Import following namespaces :
      using System.Net;   
      using System.Net.Mail;   
  • Add following button click event with function to send mail :
      public bool SendMail(string subject, string body, string to, string attachments)  
      {  
           bool result = false;  
           try  
           {  
                string emailSender = "Sender Gmail Email Address";  
                string passwordSender = "Sender Gmail Password";  
                using (SmtpClient smtpClient = new SmtpClient())  
                {  
                     smtpClient.Host = "smtp.gmail.com";  
                     smtpClient.Port = 587;  
                     smtpClient.Credentials = new NetworkCredential(emailSender, passwordSender);  
                     smtpClient.EnableSsl = true;  
                     using (MailMessage message = new MailMessage())  
                     {  
                          message.From = new MailAddress(emailSender);  
                          message.Subject = subject;  
                          message.Body = body;
                          if (!string.IsNullOrEmpty(attachments))
                          {
                       foreach (string attachment in attachments.Split(",".ToCharArray()))
                       message.Attachments.Add(new Attachment(attachment));
                          }  
                          message.To.Add(to);  
                          smtpClient.Send(message);  
                     }  
                }  
                result = true;  
           }  
           catch (Exception)  
           {  
                result = false;  
           }  
           return result;  
      }  

Friday, 26 May 2017

Send Email using Gmail in ASP.NET C#

Introduction:

In this topic, I will explain code to send email using Gmail in ASP.NET C#.

Description:

Usually, it is necessary to send email to user from ASP.NET application. We should use SMTP (Simple Mail Transfer Protocol) server to send email. If we don't have any SMTP server at that time we can use Gmail SMTP server to send email.

Following is source code to send email in ASP.NET :

In aspx page,
  • Add following code,
      <div>   
           <asp:TextBox ID="txtTo" runat="server" placeholder="To"></asp:TextBox>   
      </div>   
      <br />   
      <div>   
           <asp:TextBox ID="txtSubject" runat="server" placeholder="Subject"></asp:TextBox>   
      </div>   
      <br />   
      <div>   
           <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Rows="3" Columns="20" placeholder="Message"></asp:TextBox>   
      </div>   
      <br />   
      <div>   
           <asp:LinkButton ID="lnkbtnSendMail" runat="server" OnClick="lnkbtnSendMail_Click"> Send </asp:LinkButton>   
      </div>   
Here,
         txtTo - It will contain Email address of User to which we need to send Email
  txtSubject - It will contain Subject of Email
  txtMessage - It will contain content of Body of Email
  lnkbtnSendMail - It is button to send Email

In code behind,
  • Import following namespaces :
      using System.Net;   
      using System.Net.Mail;   
  • Add following button click event with function to send mail :
      protected void lnkbtnSendMail_Click(object sender, EventArgs e)  
      {  
           bool isMailSent = SendMail(txtSubject.Text,txtMessage.Text,txtTo.Text);  
      }  
      public bool SendMail(string subject, string body, string to)  
      {  
           bool result = false;  
           try  
           {  
                string emailSender = "Sender Gmail Email Address";  
                string passwordSender = "Sender Gmail Password";  
                using (SmtpClient smtpClient = new SmtpClient())  
                {  
                     smtpClient.Host = "smtp.gmail.com";  
                     smtpClient.Port = 587;  
                     smtpClient.Credentials = new NetworkCredential(emailSender, passwordSender);  
                     smtpClient.EnableSsl = true;  
                     using (MailMessage message = new MailMessage())  
                     {  
                          message.From = new MailAddress(emailSender);  
                          message.Subject = subject;  
                          message.Body = body;  
                          message.To.Add(to);  
                          smtpClient.Send(message);  
                     }  
                }  
                result = true;  
           }  
           catch (Exception)  
           {  
                result = false;  
           }  
           return result;  
      }  

Featured post

Send Attachment in Email using Gmail in ASP.NET C#

Introduction : In this topic, I will explain code to send attachment in email using Gmail in ASP.NET C#. Description : Usually, it is ...