Send mail with an Attachment

Hi friend

Hers is the code which show the Email Send with the attached file .Have a look..


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnMailSend_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(“SendToMail@gmail.com”);
        //To whome you want to send mail
        mail.From = new MailAddress(“SemderMailId@yahoo.com”);
        mail.Subject = “This is a Subject of the Mail”;
        mail.Body = “Here We Add the Body of the mail”;
        mail.IsBodyHtml = true;
//Here We attached the File 
        if (FileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; 
        smtp.Credentials = new System.Net.NetworkCredential
             ("YourMailID@gmail.com", "Password");
 //It may be your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
 
    }
}

1 comment: