How to send email with gmail credentials using asp.net?

aswinialuri-19361
Posted by aswinialuri-19361 under ASP.NET category on | Points: 40 | Views : 2560
code in .aspx page:

<table>

<tr><td><h1>Web Mail</h1></td></tr>
<tr><td>To:</td><td>
<asp:TextBox ID="txtto" runat="server" Width="505px"></asp:TextBox></td></tr>
<tr><td>Cc:</td><td>
<asp:TextBox ID="txtcc" runat="server" Height="24px" Width="508px"></asp:TextBox></td></tr>
<tr><td>Bcc</td><td><asp:TextBox ID="txtbcc" runat="server" Height="25px"
Width="512px"></asp:TextBox></td></tr>
<tr><td>Message</td><td>
<asp:TextBox ID="txtmsg" runat="server" TextMode="MultiLine" Height="119px"
Width="513px"></asp:TextBox></td></tr>
<tr><td>Subject:</td>
<td>
<asp:TextBox ID="txtsub" runat="server" Height="23px" Width="514px"></asp:TextBox></td></tr><tr><td></td></tr>
<tr><td>Attachement</td><td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td></tr>
<tr><td>
<asp:Button ID="btnsend" runat="server" Text="Send" onclick="btnsend_Click" /> <asp:UpdateProgress ID="up1" runat="server"><ProgressTemplate>
<img id="Img1" src="IMAGES/ajax-loader (1).gif" alt=" " runat="server" />
</ProgressTemplate></asp:UpdateProgress></td>
<td><asp:Button ID="btnclear" runat="server" Text="Clear" /></td><td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
</tr>

</table>


code in .aspx.cs page:

protected void btnsend_Click(object sender, EventArgs e)
{

try
{

MailMessage msg =new MailMessage("aswinialuri@gmail.com",txtto.Text);
msg.IsBodyHtml = true;
msg.CC.Add(txtcc.Text);
msg.Body=txtsub.Text.ToString();
msg.Subject=txtmsg.Text.ToString();
msg.Bcc.Add(txtbcc.Text);
if (FileUpload1.HasFile)
{
msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
else
{
txtmsg.Text = "empty";
}
NetworkCredential Nc = new NetworkCredential("aswinialuri@gmail.com", "*********@");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = Nc;
smtp.Send(msg);

}


catch (Exception ex)
{
Response.Write(ex.Message);
}

Label1.Text = "Your mail is sent sucsessfull";

}


}

your mail is sent through gmail credentials by using above code

Comments or Responses

Login to post response