C # sends mail through an SMTP server Mailhelper

Source: Internet
Author: User
Tags mailmessage smtpclient

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.