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 ");