最近需要做个邮件提醒功能,使用公司的邮件服务器发送没问题,但我试着用 163 的邮箱去发,结果总是失败!
有待解决的有两个问题:一是 使用 其它服务器发送邮件,如 163的。二是 发送HTML 内容和附件
下面是使用异步发送和非异步的方法,这是部分代码说明发送邮件比较简单。横线下面是封的全部代码
/// <summary>
/// 发送邮件
/// </summary>
public bool Send()
{
MailMessage message = null;
try
{
message = new MailMessage(this.From, this.To, this.Subject, this.Body);
message.IsBodyHtml = true;
m_client.Timeout = this.Timeout;
message.Priority = System.Net.Mail.MailPriority.High;
m_client.Credentials = CredentialCache.DefaultNetworkCredentials;
m_client.Send(message);
return true;
}
catch (Exception ex)
{
Grass.Log.ExceptionsStack.RegisterError(ex);
return false;
}
finally
{
if (message != null)
message.Dispose();
}
}
#region 异步发送
/// <summary>
/// 异步发送邮件
/// </summary>
public void SendAsync()
{
MailMessage message = null;
try
{
message = new MailMessage(this.From, this.To, this.Subject, this.Body);
message.IsBodyHtml = true;
m_client.Timeout = this.Timeout;
message.Priority = System.Net.Mail.MailPriority.High;
m_client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
m_client.Credentials = CredentialCache.DefaultNetworkCredentials;
m_client.SendAsync(message, "send e-mail");
}
catch (Exception ex)
{
Grass.Log.ExceptionsStack.RegisterError(ex);
throw;
}
finally
{
//if (message != null)
// message.Dispose();
}
}
/// <summary>
/// 异步邮件发送完成事件
/// </summary>
public void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
if (this.OnSendComplete != null)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
if (e.Cancelled)
{
OnSendComplete(false, token, string.Format("[{0}] 发送取消.", token));
}
else if (e.Error != null)
{
OnSendComplete(false, token, string.Format("[{0}] 发送错误:{1}", token, e.Error.Message));
}
else
{
OnSendComplete(false, token, string.Format("[{0}] 发送成功.", token));
}
}
}
#endregion
--------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.ComponentModel;
namespace Grass.Framework.Mail
{
/// <summary>
/// 使用 SMTP(Simple Mail Transfer Protocol) 邮件协议发送邮件
/// </summary>
public class SmtpHelper:IMailHelper
{
/// <summary>
/// 使用 SMTP 协议发送 E-mail
/// </summary>
/// <param name="host">发件服务器</param>
public SmtpHelper(string host)
{
this.Host = host;
m_client = new SmtpClient(host);
}
/// <summary>
/// 使用 SMTP 协议发送 E-mail
/// </summary>
/// <param name="host">发件服务器(IP)</param>
/// <param name="prot">发件服务器端口号</param>
public SmtpHelper(string host, int prot)
{
this.Host = host;
this.Prot = prot;
m_client = new SmtpClient(host, prot);
}
private int m_timeout = 1000;
private SmtpClient m_client = null;
/// <summary>
/// 发件服务器
/// </summary>
public string Host { set; get; }
/// <summary>
/// 发件服务器端口号
/// </summary>
public int Prot { set; get; }
/// <summary>
/// 发件超时时间
/// </summary>
public int Timeout
{
set { m_timeout = value; }
get { return m_timeout; }
}
/// <summary>
/// 发送到,如果多个邮箱使用 半角逗号分隔开
/// </summary>
public string To { set; get; }
/// <summary>
/// 发送者
/// </summary>
public string From { set; get; }
/// <summary>
/// 主题
/// </summary>
public string Subject { set; get; }
/// <summary>
/// 发送内容
/// </summary>
public string Body { set; get; }
/// <summary>
/// 邮件发送成功后触发
/// </summary>
public event SendCompleteHander OnSendComplete;
/// <summary>
/// 发送邮件
/// </summary>
public bool Send()
{
MailMessage message = null;
try
{
message = new MailMessage(this.From, this.To, this.Subject, this.Body);
message.IsBodyHtml = true;
m_client.Timeout = this.Timeout;
message.Priority = System.Net.Mail.MailPriority.High;
m_client.Credentials = CredentialCache.DefaultNetworkCredentials;
m_client.Send(message);
return true;
}
catch (Exception ex)
{
Grass.Log.ExceptionsStack.RegisterError(ex);
return false;
}
finally
{
if (message != null)
message.Dispose();
}
}
#region 异步发送
/// <summary>
/// 异步发送邮件
/// </summary>
public void SendAsync()
{
MailMessage message = null;
try
{
message = new MailMessage(this.From, this.To, this.Subject, this.Body);
message.IsBodyHtml = true;
m_client.Timeout = this.Timeout;
message.Priority = System.Net.Mail.MailPriority.High;
m_client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
m_client.Credentials = CredentialCache.DefaultNetworkCredentials;
m_client.SendAsync(message, "send e-mail");
}
catch (Exception ex)
{
Grass.Log.ExceptionsStack.RegisterError(ex);
throw;
}
finally
{
//if (message != null)
// message.Dispose();
}
}
/// <summary>
/// 异步邮件发送完成事件
/// </summary>
public void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
if (this.OnSendComplete != null)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
if (e.Cancelled)
{
OnSendComplete(false, token, string.Format("[{0}] 发送取消.", token));
}
else if (e.Error != null)
{
OnSendComplete(false, token, string.Format("[{0}] 发送错误:{1}", token, e.Error.Message));
}
else
{
OnSendComplete(false, token, string.Format("[{0}] 发送成功.", token));
}
}
}
#endregion
}
}
转载请保留:http://www.iqingcao.com
分享到:
相关推荐
本文阐述了电子邮件的基本原理及用于发送邮件的SMTP协议、接收邮件的POP3协议,分析了该协议的工作原理和命令。该系统可以发送一个纯文本邮件,也可以发送一个带有附件的邮件或多附件支持多收信人的邮件,对局域网...
1.SMTP是工作在两种情况下:一是电子邮件从客户机传输到服务器;二是从某一个服务器传输到另一个 服务器 2.SMTP是个请求/响应协议,命令和响应都是基于ASCII文本,并以CR和LF符结束。响应包括一个表示返 回...
Mail Demon是一个简单轻巧的C#smtp服务器和邮件列表系统,用于发送无限制的电子邮件和文本消息。 通过专注于简单性,异步性和性能,即使在廉价Linux VPS上,您也可以轻松地每秒发送数千条消息。 内存使用率和CPU...
邮件消息的生成: 电子邮件模板可以根据收件人,主题,HTML和/或纯文本,附件甚至标题而完全个性化。 占位符从数据源中插入为大括号之间的变量名,例如: {MailboxAddress.Name}或带有格式参数的变量,例如{Date:...
7) 一些浏览器也包含电子邮件客户,使浏览器能够发送和接收电子邮件 8) 浏览器将它取回的每一个页面副本都放入本地磁盘的缓存中。 9) 当用户用鼠标点击某个选项时,浏览器首先检查磁盘的缓存。若缓存中保存了该项,...
本控件可以在Visual Studio系列...购买者请先与我们联系,通过银行汇款后通过QQ、 MSN 或电子邮件发送正式版控件。 本控件1.0版本价格是人民币:贰佰圆整 即¥200元整 本控件2.0版本价格是人民币:肆佰圆整 即¥400元整
使用.ashx文件处理IHttpHandler实现发送文本及二进制数据的方法 制作一个简单的多页Tab功能 一完美的关于请求的目录不存在而需要url重写的解决方案! 在C#中实现MSN消息框的功能 XmlHttp实现无刷新三联动ListBox 鼠标...
JavaScript的RegExp对象和String对象定义了使用正则表达式来执行强大的模式匹配和文本检索与替换函数的方法. 在JavaScript中,正则表达式是由一个RegExp对象表示的.当然,可以使用一个RegExp()构造函数来创建RegExp...
此应用程序是一个命令行应用程序,它允许通过拆分成段将任何大文件上传到任何电子邮件帐户。 与Peer2Mail文件共享技术不同,与普通的P2M应用程序相比存在一些差异: BackupToMail旨在将自己的数据(如备份)保留在...
软件名称:DapperMap地图控件 软件版本:1.0.9.2 建议分类:编程开发 是否beta版:否 软件作者:凌宇软件 作者邮件:lingyusoft@qq.com ...E-Mail: lingyusoft@qq.com 下载:http://www.lingyusoft.cn
以下语句中,正确制作电子邮件链接的是______。 A.<ahref:"xxx@yyy"> B.<mailhref="xxx@YYY"> C.<ahref:"mailto:xxx@YYY"> D.<mail>xxx@yyy</mail> 正确答案:C 您的答案: 本题解析: 暂无解析 16.下列...