Sunday 22 January 2012

Simple method to send mails form your asp.net with attachment


first include

Using System.Net.Mail;

-------------------------------------------


protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add("manjunath4ualways@gmail.com");
        mail.To.Add("manjunath4ualways@gmail.com");
        mail.From = new MailAddress("manjunath.le@smartdrivelabs.com");
        mail.Subject = "Mail from manju";

        string Body = "Hi, this mail is From Manju ";
        mail.Body = Body;

        mail.IsBodyHtml = true;
        if (FileUpload1.HasFile)
        {
            string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
        }


        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gamil.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("your mailid", "password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = false;
        smtp.Send(mail);
    }
thats it...............

your SMTP server name means......... the outgoing mail server for your mail id which ll be used in outlook for mail configuration


No comments:

Post a Comment