System.Net.Mail.MailMessage meg = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient mailsender = new System.Net.Mail.SmtpClient();
try {
string su ="Your Message"
string attachfilepath = "";
if (File.Exists(apppath + "\\ErrorLogs.txt") == true) {
attachfilepath = apppath + "\\ErrorLogs.txt";
} else if (File.Exists(apppath + "\\ErrorLogs.txt") == true) {
attachfilepath = apppath + "\\ErrorLogs.txt";
}
Attachment atm = new Attachment(attachfilepath);
meg.From = new MailAddress("From Address", "SendeName");
meg.To.Add("sample@gmail.com");
meg.CC.Add("sample@gmail.com");
meg.Bcc.Add("sample@gmail.com");
meg.Subject = "This is text mail";
meg.Body = su;
meg.IsBodyHtml = true;
meg.Attachments.Add(atm);
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath + "/web.config");
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings");
if (mailSettings != null) {
mailsender.Host = mailSettings.Smtp.Network.Host;
mailsender.Port = mailSettings.Smtp.Network.Port;
if (Convert.ToBoolean(mailSettings.Smtp.Network.DefaultCredentials)) {
mailsender.UseDefaultCredentials = true;
}
if (Convert.ToBoolean(mailSettings.Smtp.Network.EnableSsl)) {
mailsender.EnableSsl = true;
}
mailsender.Credentials = new System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
}
mailsender.Send(meg);
} catch (Exception ex) {
ShowMessage(ex.Message);
} finally {
ShowMessage("Mail Successfully send");
}
for best coding try to give email address and password in web.config file
here the code for web.config
<pre lang="xml"> <system.net>
<mailSettings>
<smtp>
<network defaultCredentials="true" host="smtp.gmail.com" password="password" port="587" userName="emailaddress" enableSsl="true"/>
</smtp>
</mailSettings>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true"/>
</settings>
</system.net></pre>
try to change port 465 to 587