Home > Code > ASP.NET > Simple Web Send Mail with Attachment

Simple Web Send Mail with Attachment

by sameer baijal   on Dec 12, 2014   Category: ASP.NET   |  Views: 704    |  Points: 25   |  Starter 


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Net.Mail;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "your email id";
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox2.Focus();
}
protected void Button1_Click(object sender, EventArgs e)
{
string attachmentfile = null;
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs("~\\Mail_Demo\\Attachment" + FileUpload1.FileName);
attachmentfile = FileUpload1.PostedFile.FileName;

MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");

mail.From = new MailAddress(TextBox1.Text);

string[] arr = TextBox2.Text.Split(',');
byte i;
for (i = 0; i < arr.Length; i++)
mail.To.Add(arr);
mail.Subject = TextBox3.Text;
mail.Body = TextBox4.Text;

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(attachmentfile);
mail.Attachments.Add(attachment);

smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("gmail username", "gmail password");
smtpServer.EnableSsl = true;

smtpServer.Send(mail);
Response.Write("Mail send successfully!");
}
else
{
Response.Write("Please select a file for uploading");
}
}
}



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts