First of all add one namespace
- using System.Net.Mail
then enter following code . make sure that following code has attached one text file with folder name you have to give you folder name and file name.
");
strbody.Append("
");
strbody.Append("
");
string result = SendEMailMessage("test@gmail.com", "test", "test1@gmail.com", strbody.ToString(), "Testing");if (result.ToString() == "Mail sent successfully") ScriptManager.RegisterStartupScript(Page, Page.GetType(), "str", "alert('Mail Sent Successfully.');", true);else ScriptManager.RegisterStartupScript(Page, Page.GetType(), "str", "alert('Mail Sending fail.');", true);
public static string SendEMailAttachment(string EMailFrom, string EMailName, string EMailTo, string EmailBody, string EmailSubject) { string SmtpServer = "smtp.gmail.com"; string smtpusername = "yourgmailemailid@gmail.com"; string smtppassword = "password"; string flah = ""; SmtpClient SmtpMail = new SmtpClient(); MailMessage message = new MailMessage(); System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "OrderText\\Order.txt"); message.Attachments.Add(attach); message.IsBodyHtml = true; try { SmtpMail.Host = SmtpServer; SmtpMail.UseDefaultCredentials = false; SmtpMail.Credentials = new System.Net.NetworkCredential(smtpusername, smtppassword);//Fetching the credentials MailAddress fromAddress = new MailAddress(EMailFrom, EMailName);// Set the sender address of the mail message message.From = fromAddress; message.Sender = fromAddress; // Set the recepient address of the mail message message.To.Add(EMailTo); // Set the subject of the mail message message.Subject = EmailSubject; // Set the format of the mail message body as HTML message.Body = EmailBody; message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; SmtpMail.Send(message); if (message.Attachments.Count > 0) { attach.Dispose(); } flah = "Mail sent successfully"; return flah; } catch (Exception ex) { flah = "Mail could not be sent successfully " + ex.Message; return flah; }How to call this mail function StringBuilder strbody = new StringBuilder();strbody.Append("your content");strbody.Append("your content");string result = SendEMailMessage("test@gmail.com", "test", "test1@gmail.com", strbody.ToString(), "Testing");if (result.ToString() == "Mail sent successfully") ScriptManager.RegisterStartupScript(Page, Page.GetType(), "str", "alert('Mail Sent Successfully.');", true);else ScriptManager.RegisterStartupScript(Page, Page.GetType(), "str", "alert('Mail Sending fail.');", true); |
Comments
Post a Comment