Net mail class

Source: Internet
Author: User
Tags mailmessage smtpclient

First, let's talk about how to send an email in. net .. Net has prepared classes related to sending emails for us. It is very convenient to call them directly. Below is an email notification class I wrote myself:

Public class emailnotificationservice {

 

/// <Summary>

/// Construct an email notification service instance.

/// </Summary>

/// <Param> SMTP Server IP address </param>

/// <Param> whether to use SSL to connect to the SMTP server </param>

/// <Param> SMTP server port </param>

/// <Param> User name used to log on to the SMTP server </param>

/// <Param> logon password </param>

Public emailnotificationservice (

String smtpservice,

Bool enablessl,

Int port,

String loginname,

String password ){

 

This. m_smtpservice = smtpservice;

This. m_loginname = loginname;

This. m_password = password;

This. m_enablessl = enablessl;

This. m_port = port;

}

 

Private readonly string m_smtpservice;

Private readonly string m_loginname;

Private readonly string m_password;

Private readonly bool m_enablessl;

Private readonly int m_port;

 

/// <Summary>

/// Send an email notification to the specified email address.

/// </Summary>

/// <Param> name displayed in the "sender" column </param>

/// <Param> Target email address </param>

/// <Param> email title </param>

/// <Param> email content </param>

Public void sendto (string sendername, string address, String title, string content ){

Mailmessage mail = new mailmessage ();

Mail. to. Add (Address );

Mail. From = new mailaddress (this. m_loginname, sendername, encoding. utf8 );

Mail. Subject = title;

Mail. Body = content;

Mail. bodyencoding = encoding. utf8;

Mail. isbodyhtml = false;

Mail. Priority = mailpriority. normal;

 

Smtpclient SMTP = new smtpclient ();

SMTP. Credentials = new networkcredential (this. m_loginname, this. m_password );

SMTP. Host = This. m_smtpservice;

SMTP. enablessl = This. m_enablessl;

SMTP. Port = This. m_port;

 

SMTP. Send (Mail );

}

}

 

In use, first construct an emailnotificationservice class, and then call the sendto method. For example:

 

Emailicationicationservice mailnotificationservice = new emailnotificationservice ("smtp.gmail.com", true, 587, "LoginName@gmail.com", "loginpassword ");

Mailnotificationservice. sendto ("sendername", "TargetAddress@qq.com", "title", "content ");

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.