salikram maurya

salikram maurya

  • NA
  • 46
  • 2.9k

Sending Email in Asp.net

Nov 8 2012 4:58 AM
Sending Email in Asp.net With Attachment and Without Attachment
1)without attachment
.aspx

<html>
<head></head>
<body>
<div>
<br />
<table>
<tr>
<td>To</td><td><asp:TextBox ID="txtto" runat="server" Width="200px"></asp:TextBox></td>
</tr>
<tr>
<td>Subject</td><td><asp:TextBox ID="txtsubject" runat="server" Width="250px"></asp:TextBox></td>
</tr>
<tr>
<td>Message</td><td><asp:TextBox ID="txtmessage" runat="server" Width="350px" TextMode="MultiLine" Height="60px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="lblmsg" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td colspan="2">
<center>
<asp:Button ID="bensave" runat="server" Text="Send" onclick="bensave_Click" />
</center>
</td>
</tr>
</table>
</div>
</body>
</html>

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

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

    }
    protected void bensave_Click(object sender, EventArgs e)
    {
        var mail = new MailMessage();
        mail.From = new MailAddress("[email protected]");
        mail.To.Add(txtto.Text);
        mail.Subject = txtsubject.Text;
        mail.IsBodyHtml = true;
        mail.Body =txtmessage.Text;
        mail.Priority = MailPriority.High;
        var mailClient = new SmtpClient("smtp.gmail.com", 587);
        mailClient.EnableSsl = true;
        mailClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
        mailClient.Send(mail);
        lblmsg.Text = "Mail sent successfully!";
        txtto.Text = txtsubject.Text = txtmessage.Text = string.Empty;
    }
}

2) With Attachment

.aspx code

<html>
<head></head>
<body>
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail with multiple Attachments using asp.net</b>
</td>
</tr>
<tr>
<td>To</td><td><asp:TextBox  ID="txtto" runat="server" Width="200px" ></asp:TextBox></td>
</tr>
<tr>
<td>Subject</td><td><asp:TextBox  ID="txtsubject" runat="server" Width="200px" ></asp:TextBox></td>
</tr>
<tr>
<td>Message</td><td><asp:TextBox  ID="txtmessage" runat="server" Width="200px" TextMode="MultiLine" ></asp:TextBox></td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" /><br />
<asp:FileUpload ID="fileUpload2" runat="server" /><br />
<asp:FileUpload ID="fileUpload3" runat="server" /><br />
<asp:FileUpload ID="fileUpload4" runat="server" /><br />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</body>
</html>

.cs code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Net.Mail;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        MailMessage mail = new MailMessage();
        mail.To.Add(txtto.Text);
        mail.From = new MailAddress("[email protected]");
        mail.Subject = txtsubject.Text;
        mail.Body = txtmessage.Text;
        mail.IsBodyHtml = true;

        //Attach file using FileUpload Control and put the file in memory stream
        if (fileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
        }
        if (fileUpload2.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload2.PostedFile.InputStream, fileUpload2.FileName));
        }
        if (fileUpload3.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload3.PostedFile.InputStream, fileUpload3.FileName));
        }
        if (fileUpload4.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload4.PostedFile.InputStream, fileUpload4.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
   
    }

}

Answers (1)