Hi friends,in this post I would like to explain how to send email with attachments in Asp.Net.
For this I am taking 3 TextBoxes(For ToAddress,Subject & Body),FileUpload control,label for displaying message and a Button control.
For working with emails you need to add the following namespace:
using System.Net.Mail;
Then code under Send Button Click Event:
MailMessage mssg = newMailMessage();
mssg.From = new MailAddress("yourgmailid@gmail.com");
mssg.To.Add(txtToAddtess.Text);
mssg.Subject = txtSubject.Text;
mssg.IsBodyHtml = true;
mssg.Body = txtBody.Text;
mssg.Priority = MailPriority.High;
string strFName = null;
strFName = Path.GetFileName(fulAttach.PostedFile.FileName);
fulAttach.PostedFile.SaveAs(Server.MapPath(strFName ));
Attachment attach = new Attachment(Server.MapPath(strFName ));
mssg.Attachments.Add(attach);
System.Net.Mail.SmtpClient client = new system.Net.Mail.SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("yourgmailid@gmail.com", "yourgmailpassword");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = mssg;
client.Send(mssg);
lblDisplayMessage.Text = "Mail Sent Successfully.";
}
Note: where fulAttach is file upload control ID.
Then it will send email along with attachment.
Thank You...
For this I am taking 3 TextBoxes(For ToAddress,Subject & Body),FileUpload control,label for displaying message and a Button control.
For working with emails you need to add the following namespace:
using System.Net.Mail;
Then code under Send Button Click Event:
protected void btnSendEmail_Click(object sender, EventArgs e)
{ MailMessage mssg = newMailMessage();
mssg.From = new MailAddress("yourgmailid@gmail.com");
mssg.To.Add(txtToAddtess.Text);
mssg.Subject = txtSubject.Text;
mssg.IsBodyHtml = true;
mssg.Body = txtBody.Text;
mssg.Priority = MailPriority.High;
string strFName = null;
strFName = Path.GetFileName(fulAttach.PostedFile.FileName);
fulAttach.PostedFile.SaveAs(Server.MapPath(strFName ));
Attachment attach = new Attachment(Server.MapPath(strFName ));
mssg.Attachments.Add(attach);
System.Net.Mail.SmtpClient client = new system.Net.Mail.SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("yourgmailid@gmail.com", "yourgmailpassword");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = mssg;
client.Send(mssg);
lblDisplayMessage.Text = "Mail Sent Successfully.";
}
Note: where fulAttach is file upload control ID.
Then it will send email along with attachment.
Thank You...
Great Work Sir