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 {}
}
}
}
}
}
Share: