C #. NET source code for sending emails

Source: Internet
Author: User
Tags mailmessage smtpclient

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Net. Mail;
Using System. Net;

Namespace MyQuery. Utils
{
/// <Summary>
/// Encapsulate Mail Processing
/// By Jia Shiyi 2011-6-3
/// </Summary>
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;

/// <Summary>
/// Set email sending Parameters
/// </Summary>
/// <Param name = "host"> smtp server address or name </param>
/// <Param name = "port"> smtp Service port is generally 25 </param>
/// <Param name = "isUserCredentials"> whether authentication is required </param>
/// <Param name = "account"> User to be authenticated </param>
/// <Param name = "password"> password of the user to be authenticated </param>
Public static void SetParameters (string host, int port, bool isUserCredentials, string account, string password)
{
SmtpHost = host;
SmptPort = port;
SmtpIsUserCredentials = isUserCredentials;
SmtpCredentialAccount = account;
SmtpCredentialPassword = password;
}
/// <Summary>
/// Configure the parameters for sending emails
/// </Summary>
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 ");
}
}
/// <Summary>
/// No exception is thrown when an error occurs when sending an email.
/// </Summary>
/// <Param name = "receivers"> recipient </param>
/// <Param name = "title"> title/topic </param>
/// <Param name = "content"> letter content </param>
/// <Param name = "sender"> If the sender is empty, the system configuration is used. </param>
Public static void SendMail (string receivers, string title, string content, string sender)
{
If (! String. IsNullOrEmpty (receivers ))
{
// Initialization parameters
SetParameters ();
If (! String. IsNullOrEmpty (smtpHost ))
{
Try
{
SmtpClient smtp = new SmtpClient (smtpHost, smptPort );
If (smtpIsUserCredentials)
{
Smtp. usedefacrecredentials = true;
Smtp. Credentials = new NetworkCredential (smtpCredentialAccount, smtpCredentialPassword );;
}
Smtp. DeliveryMethod = SmtpDeliveryMethod. Network;
If (String. IsNullOrEmpty (sender ))
{
Sender = smtpCredentialAccount;
}
Foreach (string receiver er in DataHelper. GetStrings (receivers ))
{
MailMessage msg = new MailMessage (sender, Cycler, title, content );
Msg. BodyEncoding = Encoding. UTF8;
Msg. SubjectEncoding = Encoding. UTF8;
Msg. IsBodyHtml = true;
Smtp. Send (msg );
Msg. Dispose ();
}
}
Catch {}
}
}
}
}
}

Author: Jia Shiyi"

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.