C # sends mail through an SMTP server MailHelper.cs
Using system;using system.data;using system.configuration;using system.web;using system.web.security;using System.web.ui;using system.web.ui.webcontrols;using system.web.ui.webcontrols.webparts;using System.web.ui.htmlcontrols;using system.net;using system.io;using system.text;using System.Net.Mail;///<summary >///Mailhelper Summary description///</summary>public class mailhelper{public Mailhelper () {} public string add Res_mail = Common.getconfig ("Mail", "mailconfig.xml"); public string addres_smtp = Common.getconfig ("SMTP", "Mailconfig.xml"); public string addres_mailpwd = Common.getconfig ("Mailpwd", "Mailconfig.xml"); Send mail public static bool SendEmail (string useremail, string subject, string content) {sendsms SMS = new Sen DSms (); BOOL BFG = false; try {writelog ("c://log//smtp//", "Email:" + sms.addres_mail + "Send password:" + sms.addres_mailpwd + "Send SMTP:" + SMS.ADDRES_SMTP + "Email:" + useremail + "\ r \ n");MailMessage msg = new MailMessage (); Msg. Isbodyhtml = true; Msg. from = new MailAddress (Sms.addres_mail, "cross-border portal-Password retrieval", encoding.getencoding ("Utf-8")); Msg. To.add (UserEmail); Msg. bodyencoding = encoding.getencoding ("Utf-8"); Msg. subjectencoding = encoding.getencoding ("Utf-8"); Msg. Subject = Subject; Msg. Body = content; Add Attachment string sfile = "Attachment address"; Mail. Attachments.Add (New Attachment (sfile)); SmtpClient mail = new SmtpClient ();//Instance mail. Host = SMS.ADDRES_SMTP; Send to host mail. Port = 25; Mail. useDefaultCredentials = true; Mail. Credentials = new NetworkCredential (Sms.addres_mail, sms.addres_mailpwd); Mail. Send (msg); BFG = true; } catch (Exception ex) {Writelog ("c://log//error//", "error message message:" + ex. Message + "Source:" + ex. Source + "StackTrace:" + ex. StacktraCe + "\ r \ n"); } return BFG; }///<summary>///write Log///</summary> public static void Writelog (string LogPath, String msg) {try {string tofilefoder = LogPath; String Tofilefullpath = LogPath + "" + DateTime.Now.ToString ("yyyyMMdd") + ". Log "; DirectoryInfo di = new DirectoryInfo (Tofilefoder); if (!di. Exists) {di. Create (); } System.IO.StreamWriter SW = System.IO.File.AppendText (Tofilefullpath); Sw. WriteLine (msg); Sw. Close (); } Catch {}}}
C # sends mail through an SMTP server Mailhelper