Friday 22 November 2013

How to send mail with attachment through Gmail in Asp.Net

How to send mail with attachment through Gmail in Asp.Net

Source Code for  SendingMail.aspx…………………………………..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendingMail.aspx.cs" Inherits="SendingMail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
 {
 width: 75%;
 }
</style>

<script type="text/javascript" language="javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div >

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<table class="style1">
<tr><td colspan="2">
<center> <b><font color="red">How to send an email with attachment through gmail</font></b></center></td>
</tr>
<tr><td>Enter Your Name</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Width="445px"></asp:TextBox>
</td>
</tr>
<tr><td>Your Gmail Address</td>
<td>
<asp:TextBox ID="txtGmailAddress" runat="server" Width="445px"></asp:TextBox>
</td>
</tr><tr><td>Your Gmail Password</td>
<td>
<asp:TextBox ID="txtGmailPassword" runat="server" Width="445px"
 TextMode="Password"></asp:TextBox>
</td>
</tr><tr><td>Email To</td><td>
<asp:TextBox ID="txtEmaillTo" runat="server" Width="445px"></asp:TextBox>
</td>
</tr>
<tr><td>Subject</td>
<td>
<asp:TextBox ID="txtSubject" runat="server" Width="445px"></asp:TextBox>
</td>
</tr><tr><td>Attachment</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" multiple="true" />
</td>
</tr>
<tr><td>Message</td>
<td>
<asp:TextBox ID="txtMessageBody" runat="server" Height="176px"
TextMode="MultiLine" Width="445px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Label ID="lblmessage" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
</table>

</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSend"  />

</Triggers>
</asp:UpdatePanel>

<asp:Button ID="btnSend" runat="server" Text="Send Mail" Width="145px"
onclick="btnSend_Click" />
</div>

</form>
</body>
</html>

Source Code for  SendingMail.aspx.cs…………………………………..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Drawing;

public partial class SendingMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSend_Click(object sender, EventArgs e)
{
MailMessage ml = new MailMessage();

ml.To.Add(txtEmaillTo.Text);

ml.From = new MailAddress(txtGmailAddress.Text.Trim(),txtUserName.Text.Trim());
ml.Subject = txtSubject.Text;
string Bd = txtMessageBody.Text;
ml.Body = Bd;

ml.IsBodyHtml = true;
if (FileUpload1.HasFile)
{
ml.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream,FileUpload1.FileName));
}

SmtpClient smtpcode = new SmtpClient();
smtpcode.Host = "smtp.gmail.com";
smtpcode.Port = 587;
smtpcode.UseDefaultCredentials = false;
smtpcode.Credentials = new   System.Net.NetworkCredential(txtGmailAddress.Text.Trim(), txtGmailPassword.Text.Trim());
smtpcode.EnableSsl = true;
lblmessage.Visible = true;
try
{
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Message send successfully";
smtpcode.Send(ml);

}
catch (Exception ex)
{
lblmessage.ForeColor = Color.Red;
lblmessage.Text =ex.Message;
}
}
}


5 comments:

  1. Nice concept with attachment

    ReplyDelete
  2. server pr kya change krna hoga sir codding me

    ReplyDelete
  3. Sir plzz post about CAPTCHA, how to introduce in our web pages and websites...

    ReplyDelete
    Replies
    1. http://kushonline.blogspot.in/2014/01/how-to-create-captcha-with-refresh.html

      Delete