Sending Email To Gmail Account In Asp.Net C#
In this Post I will explain about send email using Gmail SMTP Server in ASP.Net.The main thing to remember To send email with Gmail SMTP Server, you will need to use a Gmail account having a valid email address and password and along with that you will need the Gmail SMTP Mail Server settings.
Properties for MailMessage Class
Required properties of the MailMessage class.are:
From – Sender’s email address
To – Recipient(s) Email Address
CC – Carbon Copies (if any)
BCC – Blind Carbon Copies (if any)
Subject – Subject of the Email
Body – Body of the Email
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any)
ReplyTo – ReplyTo Email address.
Properties of SMTP Class
Required properties of the SMTP class.are:
Host – SMTP Server URL (Gmail: smtp.gmail.com)
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True)
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password)
Port – Port Number of the SMTP server (Gmail: 587)
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 80px">
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td valign = "top">
Message Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode = "MultiLine" Height = "150" Width = "200"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
File Attachment:
</td>
<td>
<asp:FileUpload ID="fuAttachment" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
Gmail Email:
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode = "Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button Text="Send" OnClick="SendEmail" runat="server" />
</td>
</tr>
</table>
Now switch to the code behind window by double clicking on Send Button and write down following code :
protected void SendEmail(object sender, EventArgs e)
{
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if (fuAttachment.HasFile)
{
string 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);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
}
That's it. Now you able to send a email to any gmail account.
0 comments:
Post a Comment