Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net.Mail;
Using System.Net;
Namespace Myquery.utils
{
<summary>
Encapsulating Message Handling
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 Send Message Parameters
</summary>
<param name= "host" >SMTP server address or name </param>
<param name= "Port" >SMTP service port is typically 25</param>
<param name= "Isusercredentials" > Need certification </param>
<param name= "Account" > Need to authenticate users </param>
<param name= "Password" > user's password when required for authentication </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>
Set send message parameter fetch configuration
</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>
Send mail Send mail error does not throw exception
</summary>
<param name= "receivers" > Recipient </param>
<param name= "title" > title/Subject </param>
<param name= "Content" > Letter Contents </param>
<param name= "Sender" > Sender empty Access system configuration </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. 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 {}
}
}
}
}
}