C#实现SMTP邮件发送程序实例
程序员文章站
2023-12-16 09:50:46
通常来说邮件发送功能在网站应用程序中经常会用到,包括大家经常看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,本文展示了一个客户端d...
通常来说邮件发送功能在网站应用程序中经常会用到,包括大家经常看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,本文展示了一个客户端demo,希望对有需要的朋友有所帮助。运行效果如下图所示:
核心代码如下:
复制代码 代码如下:
using system;
using system.net;
using system.net.mail;
using system.text;
namespace hc.email
{
/// <summary>
/// 整站邮件服务类
/// </summary>
public class emailservice
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailto">收件人
/// <param name="subject">主题
/// <param name="body">内容
/// <returns></returns>
public static bool send(string mailto, string subject, string body)
{
return send(new[] {mailto}, null, subject, body, true, null);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailto">收件人
/// <param name="subject">主题
/// <param name="body">内容
/// <returns></returns>
public static bool send(string[] mailto, string subject, string body)
{
return send(mailto, null, subject, body, true, null);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailto">收件人
/// <param name="subject">主题
/// <param name="body">内容
/// <param name="attachmentspath">附件
/// <returns></returns>
public static bool send(string[] mailto, string subject, string body, string[] attachmentspath)
{
return send(mailto, null, subject, body, true, attachmentspath);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailto">收件人
/// <param name="mailccarray">抄送
/// <param name="subject">主题
/// <param name="body">内容
/// <param name="isbodyhtml">是否html
/// <param name="attachmentspath">附件
/// <returns></returns>
public static bool send(string[] mailto, string[] mailccarray, string subject,string body, bool isbodyhtml,
string[] attachmentspath)
{
try
{
var config = confighelper.getconfig<emailconfig>();
if (string.isnullorempty(config.host) ||string.isnullorempty(config.username) ||
string.isnullorempty(config.port) ||string.isnullorempty(config.password))
{
//todo:记录日志
return false;
}
var @from = new mailaddress(config.mailfrom); //使用指定的邮件地址初始化mailaddress实例
var message = new mailmessage(); //初始化mailmessage实例
//向收件人地址集合添加邮件地址
if (mailto != null)
{
foreach (string t in mailto)
{
message.to.add(t);
}
}
//向抄送收件人地址集合添加邮件地址
if (mailccarray != null)
{
foreach (string t in mailccarray)
{
message.cc.add(t);
}
}
//发件人地址
message.from = @from;
//电子邮件的标题
message.subject = subject;
//电子邮件的主题内容使用的编码
message.subjectencoding = encoding.utf8;
//电子邮件正文
message.body = body;
//电子邮件正文的编码
message.bodyencoding = encoding.default;
message.priority = mailpriority.high;
message.isbodyhtml = isbodyhtml;
//在有附件的情况下添加附件
if (attachmentspath != null && attachmentspath.length > 0)
{
foreach (string path in attachmentspath)
{
var attachfile = new attachment(path);
message.attachments.add(attachfile);
}
}
try
{
var smtp = new smtpclient
{
credentials = new networkcredential(config.username, config.password),
host = config.host,
port = convert.toint32(config.port)
};
//将邮件发送到smtp邮件服务器
smtp.send(message);
//todo:记录日志
return true;
}
catch (smtpexception ex)
{
//todo:记录日志
return false;
}
}
catch (smtpexception ex)
{
//todo:记录日志
return false;
}
}
}
}
using system.net;
using system.net.mail;
using system.text;
namespace hc.email
{
/// <summary>
/// 整站邮件服务类
/// </summary>
public class emailservice
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailto">收件人
/// <param name="subject">主题
/// <param name="body">内容
/// <returns></returns>
public static bool send(string mailto, string subject, string body)
{
return send(new[] {mailto}, null, subject, body, true, null);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailto">收件人
/// <param name="subject">主题
/// <param name="body">内容
/// <returns></returns>
public static bool send(string[] mailto, string subject, string body)
{
return send(mailto, null, subject, body, true, null);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailto">收件人
/// <param name="subject">主题
/// <param name="body">内容
/// <param name="attachmentspath">附件
/// <returns></returns>
public static bool send(string[] mailto, string subject, string body, string[] attachmentspath)
{
return send(mailto, null, subject, body, true, attachmentspath);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailto">收件人
/// <param name="mailccarray">抄送
/// <param name="subject">主题
/// <param name="body">内容
/// <param name="isbodyhtml">是否html
/// <param name="attachmentspath">附件
/// <returns></returns>
public static bool send(string[] mailto, string[] mailccarray, string subject,string body, bool isbodyhtml,
string[] attachmentspath)
{
try
{
var config = confighelper.getconfig<emailconfig>();
if (string.isnullorempty(config.host) ||string.isnullorempty(config.username) ||
string.isnullorempty(config.port) ||string.isnullorempty(config.password))
{
//todo:记录日志
return false;
}
var @from = new mailaddress(config.mailfrom); //使用指定的邮件地址初始化mailaddress实例
var message = new mailmessage(); //初始化mailmessage实例
//向收件人地址集合添加邮件地址
if (mailto != null)
{
foreach (string t in mailto)
{
message.to.add(t);
}
}
//向抄送收件人地址集合添加邮件地址
if (mailccarray != null)
{
foreach (string t in mailccarray)
{
message.cc.add(t);
}
}
//发件人地址
message.from = @from;
//电子邮件的标题
message.subject = subject;
//电子邮件的主题内容使用的编码
message.subjectencoding = encoding.utf8;
//电子邮件正文
message.body = body;
//电子邮件正文的编码
message.bodyencoding = encoding.default;
message.priority = mailpriority.high;
message.isbodyhtml = isbodyhtml;
//在有附件的情况下添加附件
if (attachmentspath != null && attachmentspath.length > 0)
{
foreach (string path in attachmentspath)
{
var attachfile = new attachment(path);
message.attachments.add(attachfile);
}
}
try
{
var smtp = new smtpclient
{
credentials = new networkcredential(config.username, config.password),
host = config.host,
port = convert.toint32(config.port)
};
//将邮件发送到smtp邮件服务器
smtp.send(message);
//todo:记录日志
return true;
}
catch (smtpexception ex)
{
//todo:记录日志
return false;
}
}
catch (smtpexception ex)
{
//todo:记录日志
return false;
}
}
}
}
完整实例代码点击此处本站下载。
希望本文所述对大家的c#程序设计有所帮助