C # EmailHelper.cs message send module code

Source: Internet
Author: User
Tags 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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.