利用 System.Net.Mail 实现邮件发送功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SXT.Data;
using System.Net.Mail;
using System.ComponentModel;
namespace SXT.MSS.Winservice
{
/// <summary>
/// 邮件操作类
/// </summary>
public class SmtpMailService
{
#region 属性
//SMTP服务器
protected SmtpClient MySmtpClient { get; set; }
#endregion
#region
/// <summary>
/// 构造函数
/// </summary>
public SmtpMailService()
{
MySmtpClient = null;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="host">服务器</param>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <param name="port">端口</param>
public SmtpMailService(string host, string userName, string password, int port)
{
MySmtpClient = new SmtpClient(host);
MySmtpClient.Credentials = new System.Net.NetworkCredential(userName, password);
MySmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MySmtpClient.Port = port;
}
#endregion
#region 功能函数
/// <summary>
/// 创建邮件服务客户端
/// </summary>
/// <param name="host">服务器</param>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <param name="port">端口</param>
/// <returns></returns>
public bool CreateClient(string host, string userName, string password, int port)
{
try
{
MySmtpClient = new SmtpClient(host);
MySmtpClient.Credentials = new System.Net.NetworkCredential(userName, password);
MySmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MySmtpClient.Port = port;
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="smtpPort">服务器端口</param>
/// <param name="formEmale">发信人地址</param>
/// <param name="loginID">发信人邮件帐号</param>
/// <param name="pwd">帐号密码</param>
/// <param name="title">邮件标题</param>
/// <param name="content">邮件内容</param>
/// <param name="toEmale">收件人</param>
/// <returns></returns>
public bool SendMail(string formEmale, string title, string content, string toEmal)
{
if (MySmtpClient == null)
return false;
MailMessage myMail = new MailMessage();
myMail = new MailMessage();
myMail.From = new MailAddress(formEmale);
myMail.To.Add(new MailAddress(toEmal));
myMail.Subject = title;
myMail.Body = content;
myMail.IsBodyHtml = false;
myMail.Priority = MailPriority.Normal;
MySmtpClient.Send(myMail);
return true;
}
#endregion
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SXT.Data;
using System.Net.Mail;
using System.ComponentModel;
namespace SXT.MSS.Winservice
{
/// <summary>
/// 邮件操作类
/// </summary>
public class SmtpMailService
{
#region 属性
//SMTP服务器
protected SmtpClient MySmtpClient { get; set; }
#endregion
#region
/// <summary>
/// 构造函数
/// </summary>
public SmtpMailService()
{
MySmtpClient = null;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="host">服务器</param>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <param name="port">端口</param>
public SmtpMailService(string host, string userName, string password, int port)
{
MySmtpClient = new SmtpClient(host);
MySmtpClient.Credentials = new System.Net.NetworkCredential(userName, password);
MySmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MySmtpClient.Port = port;
}
#endregion
#region 功能函数
/// <summary>
/// 创建邮件服务客户端
/// </summary>
/// <param name="host">服务器</param>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <param name="port">端口</param>
/// <returns></returns>
public bool CreateClient(string host, string userName, string password, int port)
{
try
{
MySmtpClient = new SmtpClient(host);
MySmtpClient.Credentials = new System.Net.NetworkCredential(userName, password);
MySmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MySmtpClient.Port = port;
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="smtpPort">服务器端口</param>
/// <param name="formEmale">发信人地址</param>
/// <param name="loginID">发信人邮件帐号</param>
/// <param name="pwd">帐号密码</param>
/// <param name="title">邮件标题</param>
/// <param name="content">邮件内容</param>
/// <param name="toEmale">收件人</param>
/// <returns></returns>
public bool SendMail(string formEmale, string title, string content, string toEmal)
{
if (MySmtpClient == null)
return false;
MailMessage myMail = new MailMessage();
myMail = new MailMessage();
myMail.From = new MailAddress(formEmale);
myMail.To.Add(new MailAddress(toEmal));
myMail.Subject = title;
myMail.Body = content;
myMail.IsBodyHtml = false;
myMail.Priority = MailPriority.Normal;
MySmtpClient.Send(myMail);
return true;
}
#endregion