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"