Introduction
This article lists code to send a mail with an attachment file.
This Code For Succesfully Run............
This article lists code to send a mail with an attachment file.
Background
We send mail using System.Net.Mail
.
Using the code
string ToAddress = TextBox3.Text; const string FromAddress = YourGmailId; //ForExample:tejasbhalani@gmail.com |
string Subject = TextBox6.Text;
String Body = TextBox4.Text;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(ToAddress);
mail.From = new MailAddress(FromAddress);
mail.Subject = Subject;
mail.Body = Body;
if (FileUpload1.PostedFile != null)
{
HttpPostedFile attFile = FileUpload1.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
FileNameToAttache = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
mail.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
}
}
mail.Priority = System.Net.Mail.MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(FromAddress, "YourGmailPassword");
client.Port = 587; // Gmail works on this portclient.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layertry
{
client.Send(mail);
}
catch (Exception error)
{
throw error;
}
|
This Code For Succesfully Run............
0 comments:
Post a Comment