Advance Email Sending Option , with attachement.
Task: Sending Email with attachment using c# code in asp.net website/ web application.
Description: Some time we need to a website viewer can send a enquiry with some attachment as some kind of career option where a candidate can submit his/her resume and the website administrator receive the same within email.
As in my previous post I discuss about a email notification http://hemantrautela.blogspot.com/2012/08/how-to-send-email-from-website-aspnet.html without attachment option , Now we require a email with attachment, I use below code for sending email with attachment . Here I use two fileupload control for uploading attachment file (here I am not checking the extension of file / as you can check it first before sending email ).
// Below Function can be used for sending a email with attachment option using c# code
// using gmail crdential
public bool mailattachement2(string to, string replyto, string body, string subject, FileUpload f1, FileUpload f2)
MailMessage mail = new MailMessage();
mail.From = new MailAddress("firstname.lastname@example.org","Client Enquiry");
mail.Subject = subject;
mail.Body = body;
MailAddress rt = new MailAddress(replyto);
mail.ReplyTo = rt;
//mail.Sender = rt;
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment(f1.PostedFile.InputStream, f1.FileName)); //add the attachment
mail.Attachments.Add(new Attachment(f2.PostedFile.InputStream, f2.FileName)); //add the attachment
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential("email@example.com", "password");
// Set the one email id and its password for authentication )
// email goes via using above email id...
smtp.Port = 587;
smtp.EnableSsl = true;
catch (Exception ex)