C # EmailHelper. cs mail sending module code

Source: Internet
Author: User
Tags mailmessage smtpclient

A c # mail module, EmailHelper. the cs code is copied from a C # Toolkit, in ASP. NET or C # project, this class is very convenient, you can specify the mail title, mail body, recipient, HTML format and other parameters, the Code is as follows:

01 using System;

02 using System. Collections. Generic;

03 using System. Linq;

04 using System. Text;

05 namespace CLB. Utility. Tools

06 {

07 public class EmailHelper

08 {

09 // <summary>

10 // send an email

11 /// <summary>

12 /// <param name = "subject"> email title </param>

13 /// <param name = "body"> email body </param>

14 /// <param name = "to"> recipient </param>

15 /// <param name = "Ishtml"> whether it is in html format </param>

16 public static bool sendmail (string subject, string body, string to, bool Ishtml)

17 {

18 using (System. Net. Mail. MailMessage msg = new System. Net. Mail. MailMessage ())

19 {

20 msg. To. Add ();

21 msg. From = new System. Net. Mail. MailAddress ("email address", "title", System. Text. Encoding. UTF8 );

22 msg. Subject = subject; // mail title

23 msg. SubjectEncoding = System. Text. Encoding. UTF8; // email title Encoding

24 msg. Body = body; // mail content

25 msg. BodyEncoding = System. Text. Encoding. UTF8; // email Content Encoding

26 msg. IsBodyHtml = Ishtml; // whether it is an HTML email

27 msg. Priority = System. Net. Mail. MailPriority. High; // Mail Priority

28 System. Net. Mail. SmtpClient client = new System. Net. Mail. SmtpClient ();

29 client. Credentials = new System. Net. NetworkCredential ("email address", "password ");

30 client. Host = "Domain Name ";

31 object userState = msg;

32 try

33 {

34 client. Send (msg );

35 return true;

36}

37 catch

38 {

39 return false;

40}

41}

42}

43}

44}

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.