C # EmailHelper.cs message send module code

Source: Internet
Author: User
Tags bool mailmessage smtpclient

A C # send mail module, EmailHelper.cs code, is copied from a C # toolkit, in asp.net or C # project to add send mail function, this class is very convenient, when used to specify the message title, message body, recipient, HTML format and other parameters can be, The code is as follows:

01using System;

02using System.Collections.Generic;

03using System.Linq;

04using System.Text;

05namespace CLB. Utility.tools

269

Modified public class Emailhelper

08 {

///<summary>

10///Send mail

One///<summary>

///<param name= "Subject" > Message title </param>

/// <param name= "Body" > message main </param>

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

/// <param name= "ishtml" > is HTML format </param>

public static bool SendMail (string subject, string body, string to, bool ishtml)

17 {

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

19 {

Msg. To.add (to);

Msg. from = new System.Net.Mail.MailAddress ("email address", "title", System.Text.Encoding.UTF8);

Msg. Subject = subject;//message Header

Msg. subjectencoding = system.text.encoding.utf8;//message header encoding

Msg. BODY = body;//message Content

Msg. bodyencoding = system.text.encoding.utf8;//message content encoding

Msg. isbodyhtml = ishtml;//is an HTML message

Msg. Priority = system.net.mail.mailpriority.high;//Message priority

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

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

Client. Host = "domain name";

Object userState = msg;

Try

33 {

Client. Send (msg);

return true;

36}

Panax Notoginseng Catch

38 {

return false;

40}

41}

42}

43}

44}

Related Article

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.