asp.net发邮件示例
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;
}
}