1)without attachment
.aspx
| To | |
| Subject | |
| Message | |
.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
Send Mail with multiple Attachments using asp.net | |
| To | |
| Subject | |
| Message | |
Attach a file: | |
.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);
}
}

Satya PrakashPosted Nov 8, 2012, 5:52 AM
Below i give a link that decribe how to send email in asp.net in simple way
http://www.dotnetfactor.com/Article/send-email-in-Asp-Net-2.aspx