In previous articles we have seen how to send a mail in asp.net,send HTML formated mail.In this post i will show how to send mail with attachment.Here the local file which in root directory of the application has been used as attachment for mail.This scenario will be place when we send error log files to our mail.
Note:place code in Button click event which has to be used for send mail functionality
Note:place code in Button click event which has to be used for send mail functionality
path_profile = AppDomain.CurrentDomain.BaseDirectory; path_profile = path_profile + @"Log\Adminmailfile.txt"; string ServerName = ConfigurationManager.AppSettings["smtpservername"].ToString(); string FromAddress = ConfigurationManager.AppSettings["fromaddress"].ToString(); string adminmail = ConfigurationManager.AppSettings["AdminMail"].ToString(); string User = ConfigurationManager.AppSettings["user"].ToString(); string PWD = ConfigurationManager.AppSettings["PWD"].ToString(); DataSet dsmonthname = clsdb.getmonthname(cmonth); //send message MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(ServerName); mail.From = new MailAddress(FromAddress); mail.To.Add(adminmail); mail.Subject = "Alert" + System.DateTime.Now; mail.Body = "Alert" + System.DateTime.Now; System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(path_profile); mail.Attachments.Add(attachment); SmtpServer.Credentials = new System.Net.NetworkCredential(User, PWD); SmtpServer.Send(mail);Vb:
path_profile = AppDomain.CurrentDomain.BaseDirectory path_profile = path_profile & "Log\Adminmailfile.txt" Dim ServerName As String = ConfigurationManager.AppSettings("smtpservername").ToString() Dim FromAddress As String = ConfigurationManager.AppSettings("fromaddress").ToString() Dim adminmail As String = ConfigurationManager.AppSettings("AdminMail").ToString() Dim User As String = ConfigurationManager.AppSettings("user").ToString() Dim PWD As String = ConfigurationManager.AppSettings("PWD").ToString() Dim dsmonthname As DataSet = clsdb.getmonthname(cmonth) 'send message Dim mail As New MailMessage() Dim SmtpServer As New SmtpClient(ServerName) mail.From = New MailAddress(FromAddress) mail.[To].Add(adminmail) mail.Subject = "Alert" & System.DateTime.Now mail.Body = "Alert" & System.DateTime.Now Dim attachment As System.Net.Mail.Attachment attachment = New System.Net.Mail.Attachment(path_profile) mail.Attachments.Add(attachment) SmtpServer.Credentials = New System.Net.NetworkCredential(User, PWD) SmtpServer.Send(mail)
No comments:
Post a Comment