asp.net发邮件示例

所属分类: 网络编程 / ASP.NET 阅读数: 454
收藏 0 赞 0 分享

复制代码 代码如下:

protected void SendMail()
{
try
{
string CreaterName = "";
string examiner = "";
List<string> mailList = GetMailList(ref CreaterName, ref examiner);

MailEntity me = new MailEntity();
MailEntity me_1 = new MailEntity();

me.AddresserMail = ConfigurationManager.AppSettings["AddresserMail"].ToString();
me.AddresserName = ConfigurationManager.AppSettings["AddresserName"].ToString();
me.AddresserPwd = ConfigurationManager.AppSettings["AddresserPwd"].ToString();

me_1.AddresserMail = ConfigurationManager.AppSettings["AddresserMail_1"].ToString();
me_1.AddresserName = ConfigurationManager.AppSettings["AddresserName_1"].ToString();
me_1.AddresserPwd = ConfigurationManager.AppSettings["AddresserPwd_1"].ToString();

string strPostfix = ConfigurationManager.AppSettings["Postfix"].ToString();
string isSend = ConfigurationManager.AppSettings["isSend"].ToString();
StringBuilder ss = new StringBuilder();
if (isSend == "1")
{
if (mailList.Count > 0)
{
foreach (string mail in mailList)
{
int count = mail.IndexOf("@");
string postfix = mail.Substring(count + 1);

string[] pf = strPostfix.Split(new char[] { '|' });
int pCount = 0;
foreach (string p in pf)
{
if (postfix == p)
{
pCount++;
break;
}
}
if (pCount > 0)
{
me.AddresseeMail = mail.ToString();
}
else
{
me_1.AddresseeMail = mail.ToString();
}
string Body = "";

try
{
using (StreamReader sr = new StreamReader(HttpRuntime.BinDirectory + "/../tpl/tpl_Debt.htm", System.Text.Encoding.Default))
{
Body = sr.ReadToEnd();
}
}
catch (Exception ex)
{
lbMessage.Text = "*** 读取模板出错(reading template failed)!" + ex.Message;
lbMessage.ForeColor = System.Drawing.Color.Red;
return;
}

// --------------------------------------------------------------------------
me.ClientHost = ConfigurationManager.AppSettings["clientHost"].ToString();
me.MailEncoding = System.Text.Encoding.GetEncoding("GB2312");
me.MailPriority = System.Net.Mail.MailPriority.Normal;
me.IsBodyHtml = true;
me.Subject = "[" + CreaterName + "]创建的借款单";
//--------------------------------------------------------------------------
me_1.ClientHost = ConfigurationManager.AppSettings["clientHost_1"].ToString();
me_1.MailEncoding = System.Text.Encoding.GetEncoding("GB2312");
me_1.MailPriority = System.Net.Mail.MailPriority.Normal;
me_1.IsBodyHtml = true;
me_1.Subject = "[" + CreaterName + "]创建的借款单";

Body = Body.Replace("DTTPL_COMM_MODULE_NAME_ExpenseName", CreaterName);//借款人
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Type", ddlDebtType_Add.SelectedItem.Text);//借款类型
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Money", txbMoney_Add.Text);//金额
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Mender", txtMender.Text);//审核人
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Time", tbxDebtTime_Add.Text);//借款时间
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Advance", tbAdvance.Text);//代/垫款单位
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_DebtPathWay", ddlDebtPathWay.SelectedItem.Text);//借款方式
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Remark", txbMemo_Add.Text);//借款事由
// Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Mend", "");//审核意见
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Memo", "");//备注
string url = HttpContext.Current.Request.Url.AbsoluteUri;
url = url.Replace("http://", "");
int index = url.IndexOf("/");
url = "http://" + url.Substring(0, index + 1);
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_LINK", url + "Affair/SelectWorkEvent.aspx?typeid=249");


me.Body = Body;
me_1.Body = Body;

if (me.AddresseeMail == null && me.CopyToAddresseeMail == null && me_1.AddresseeMail == null && me_1.CopyToAddresseeMail == null)
{
lbMessage.Text = "*** 借款单保存成功! 但责任人/关注人因在数据库没有存邮箱地址,邮件未曾发送!";
lbMessage.ForeColor = System.Drawing.Color.Red;
return;
}
if (me.AddresseeMail != null || me.CopyToAddresseeMail != null)
{
bool temp = false;
for (int y = 0; y < 3; y++)
{
try
{
if (MailServer.SendMail(me))
{
temp = true;
break;
}
}
catch (Exception ex)
{
continue;
}

}
if (!temp)
{
if (me.AddresseeMail != null)
{
ss.Append(me.AddresseeMail.Trim());
}
if (me.CopyToAddresseeMail != null && me.CopyToAddresseeMail.Trim() != "")
{
ss.Append(",");
ss.Append(me.CopyToAddresseeMail.Trim());
}
}
me.AddresseeMail = null;
}
if (me_1.AddresseeMail != null || me_1.CopyToAddresseeMail != null)
{
bool temp = false;
for (int y = 0; y < 3; y++)
{
try
{
if (MailServer.SendMail(me_1))
{
temp = true;
break;
}
}
catch (Exception ex)
{
continue;
}
}
if (!temp)
{
if (ss.ToString().Trim() != "")
{
ss.Append(",");
if (me_1.AddresseeMail != null)
ss.Append(me_1.AddresseeMail.Trim());
if (me_1.CopyToAddresseeMail != null && me_1.CopyToAddresseeMail.Trim() != "")
{
ss.Append(",");
ss.Append(me_1.CopyToAddresseeMail.Trim());
}
}
else
{
if (me_1.AddresseeMail != null)
ss.Append(me_1.AddresseeMail.Trim());
if (me_1.CopyToAddresseeMail != null && me_1.CopyToAddresseeMail.Trim() != "")
{
ss.Append(",");
ss.Append(me_1.CopyToAddresseeMail.Trim());
}
}
}
me_1.AddresseeMail = null;
}
}
}
}
}
catch (Exception ex)
{
lbMessage.Text = "*** 发送失败(the email is not sent)!" + ex.ToString();
lbMessage.ForeColor = System.Drawing.Color.Red;
}
}

更多精彩内容其他人还在看

使用.NET Core实现饿了吗拆红包功能

这篇文章主要介绍了使用.NET Core实现饿了吗拆红包功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux服务器下利用Docker部署.net Core项目的全过程

这篇文章主要给大家介绍了关于在Linux服务器下利用Docker部署.net Core项目的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.Net学习笔记之Layui多图片上传功能

这篇文章主要给大家介绍了关于.Net学习笔记之Layui多图片上传功能的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.Net具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET第一次访问慢的完美解决方案(MVC,Web Api)

这篇文章主要给大家介绍了关于ASP.NET第一次访问慢的完美解决方案(MVC,Web Api)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

aspnetcore 实现简单的伪静态化功能

这篇文章主要介绍了aspnetcore 实现简单的伪静态化功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

.NET生成水印更好的方法实例代码

这篇文章主要给大家介绍了关于.NET中生成水印更好的方法,文中通过示例代码介绍的非常详细,对大家学习或者使用.NET具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.NET生成动态验证码的完整步骤

这篇文章主要给大家介绍了关于.NET生成动态验证码的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.NET具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

WCF中使用nettcp协议进行通讯的方法

这篇文章主要给大家介绍了关于WCF中使用nettcp协议进行通讯的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WCF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

WCF如何绑定netTcpBinding寄宿到控制台应用程序详解

这篇文章主要给大家介绍了关于WCF如何绑定netTcpBinding寄宿到控制台应用程序的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WCF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.Net项目中NLog的配置和使用实例详解

这篇文章主要给大家介绍了关于.Net项目中NLog的配置和使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.Net具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多