How to Send Email through Gmail using C#



How to Send Email through Gmail using C#

<asp:TextBox ID="txtEmail" runat="server" ToolTip="Enter Email"></asp:TextBox>  
  <asp:TextBox ID="txtSubject" runat="server"  ToolTip="Enter Subject"></asp:TextBox>  
  <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Rows="4" ToolTip="Enter Massage"></asp:TextBox>  
  <asp:Button ID="submit" runat="server" Text="Submit us" OnClick="submit_Click" />  
   
protected void Page_Load(object sender, EventArgs e)  
{  
}  
protected void submit_Click(object sender, EventArgs e)  
{  
   SendEmail(txtEmail.Text,txtSubject.Text,txtBody.Text);  
}  
   
public void SendEmail(string txtToEmail, string txtSubject, string txtBody)  
{  
   try  
   {  
      String strMailFrom, strMailBCc, strMailFromPwd, strMailServer, strMailPort, strMailIsSSL;  
   
      //get these Detail From Webconfi file//  
      strMailFrom = ConfigurationManager.AppSettings["strMailFrom"].ToString();  
      strMailBCc = ConfigurationManager.AppSettings["strMailBCc"].ToString();  
      strMailFromPwd = ConfigurationManager.AppSettings["strMailPWD"].ToString();  
      strMailServer = ConfigurationManager.AppSettings["strMailServer"].ToString();  
      strMailPort = ConfigurationManager.AppSettings["strMailPort"].ToString();  
      strMailIsSSL = ConfigurationManager.AppSettings["strMailIsSSL"].ToString();  
      //End//  
      MailMessage mail = new MailMessage();  
      SmtpClient smtp = new SmtpClient();  
      smtp.Host = strMailServer;  
      smtp.Port = Int32.Parse(strMailPort);  
      smtp.EnableSsl = Boolean.Parse(strMailIsSSL);  
      smtp.UseDefaultCredentials = Convert.ToBoolean("false");  
      CredentialCache che = new CredentialCache();  
      che.Add(strMailServer, Convert.ToInt32(strMailPort), "LOGIN", new System.Net.NetworkCredential(strMailFrom, strMailFromPwd));  
      smtp.Credentials = che;  
      smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;  
      System.Web.Mail.SmtpMail.SmtpServer = strMailServer;  
      mail.From = new MailAddress(strMailFrom, "Your Title");  
      mail.To.Add(txtToEmail);  
      if (strMailBCc.Trim().Length > 0)  
      {  
         mail.Bcc.Add(strMailBCc);  
      }  
      mail.Subject = txtSubject;  
      mail.Body = txtBody;  
      mail.IsBodyHtml = true;  
      smtp.Send(mail);  
   }  
   catch (Exception ex)  
   {  
      System.Diagnostics.Debug.Print(ex.Message);  
   }  
}  
   
In WebConfi File -  

<configuration>  
   <appSettings>  
      <add key="strBookmarkText" value="Your BookMark Name"/>  
      <add key="strMailFrom" value="Your EmailID"/>  
      <add key="strMailDefaultTo" value="Your EmailID"/>  
      <add key="strMailBCc" value=""/>  
      <add key="strMailPWD" value="Your PassWord"/>  
      <add key="strMailServer" value="smtp.gmail.com"/>  
      <add key="strMailPort" value="587"/>  
      <add key="strMailIsSSL" value="True"/>  
      <add key="strSMSKey" value=""/>  
      <add key="strSMSSENDERID" value=""/>  
      <add key="strIpInfodbKey" value=""/>  
   </appSettings>  
</configuration>  

0 Comment's

Comment Form

Submit Comment