This article introduces C #. NET send the instance code of the mail, need friend can refer to, want to be helpful to you
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>
///Encapsulation Mail 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>
///settings Send message Parameters
///</summary>
///<param name= "host" >SMTP server address or name </param>
///<param name= "port" >SMTP service port is generally 25</param>
///<param name= "isusercredentials" > Need certification </param>
///<param name= "account" > Need to authenticate users </param>
<param name= "Password" > user's password </param>
when required for authentication
public static void Setparameters (string host, int port, bool isusercredentials, string account, String PASSW ORD)
{
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 an exception
///</summary>
<param name= "receivers" > Recipient </param>
///<param name= "title" > Title/Theme </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 {}
}
}
}
}
}