How to use C#.NET to realize the function of sending email
This article mainly explains "how to use C#.NET to send email function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use C#.NET to send email function" bar!
The copy code is as follows:
Using System;using System.Collections.Generic;using System.Text;using System.Net.Mail;using System.Net;namespace MyQuery.Utils {/ encapsulated mail processing / by Jia Shiyi 2011-6-3 / public static class MailHelper {private static string smtpHost = null; private static int smptPort = 25; private static bool smtpIsUserCredentials = false; private static string smtpCredentialAccount = null Private static string smtpCredentialPassword = null / set the sending email parameter / smtp server address or name / smtp service port is generally 25 / whether or not to authenticate / the user's password when authentication is required / the password of the user when authentication is required (string host, int port, bool isUserCredentials) String account, string password) {smtpHost = host SmptPort = port; smtpIsUserCredentials = isUserCredentials; smtpCredentialAccount = account; smtpCredentialPassword = password } / private static void setParameters () {if (String.IsNullOrEmpty (smtpHost)) {smtpHost = WebHelper.GetAppConfig ("SmtpHost"); smptPort = DataHelper.GetIntValue (WebHelper.GetAppConfig ("SmptPort"), 25) SmtpIsUserCredentials = Constants.TRUE_ID.Equals (WebHelper.GetAppConfig ("SmtpIsUserCredentials")); smtpCredentialAccount = WebHelper.GetAppConfig ("SmtpCredentialAccount"); smtpCredentialPassword = WebHelper.GetAppConfig ("SmtpCredentialPassword") } / send email error will not throw exception / recipient / title / subject / / letter content / if the sender is empty, take the system configuration public static void SendMail (string receivers, string title, string content) String sender) {if (! String.IsNullOrEmpty (receivers)) {/ / initialization parameter setParameters () If (! String.IsNullOrEmpty (smtpHost)) {try {SmtpClient smtp = new SmtpClient (smtpHost, smptPort); if (smtpIsUserCredentials) {smtp.UseDefaultCredentials = true Smtp.Credentials = new NetworkCredential (smtpCredentialAccount, smtpCredentialPassword);;} smtp.DeliveryMethod = SmtpDeliveryMethod.Network; if (String.IsNullOrEmpty (sender)) {sender = smtpCredentialAccount } foreach (string receiver in DataHelper.GetStrings (receivers)) {MailMessage msg = new MailMessage (sender, receiver, title, content); msg.BodyEncoding = Encoding.UTF8; msg.SubjectEncoding = Encoding.UTF8 Msg.IsBodyHtml = true; smtp.Send (msg); msg.Dispose () }} catch {}} Thank you for reading. This is the content of "how to use C#.NET to send email". After the study of this article, I believe that we have a deeper understanding of how to use C#.NET to achieve the function of sending mail, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!