会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
seagreen7
博客园
首页
新随笔
联系
订阅
管理
ASP.NET2.0发送电子邮件示例代码
一个国外的英文网站上有非常详细的代码说明:
相关的资料地址:
http://www.systemnetmail.com/
下面的代码是我参考资料写出的一个示例:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Net;
using
System.Net.Mail;
namespace
Email_Test.aspx
{
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
string
emailTitle
=
"
测试email
"
;
string
toEmail
=
"
seagreen7@yeah.net
"
;
string
mycontent
=
"
这是测试邮件内容
"
;
string
myResult
=
SendHtmlEmail(emailTitle, toEmail, mycontent);
if
(myResult
==
"
ok
"
)
{
this
.Label1.Text
=
"
恭喜,邮件已经成功发送给
"
+
toEmail; }
else
{
this
.Label1.Text
=
"
抱歉,邮件发送失败,请检查web.config文件的配置信息 system.net 节点。
"
; }
}
public
static
string
SendHtmlEmail(
string
EmailTitle,
string
destEmail,
string
EmailContent)
{
try
{
//
读取web.config中的邮件发送的配置信息
//
在这里的代码中,我们不需要设置SmtpClient类的任何属性,因为它们已经在Web.config文件中指定了
//
create the mail message
MailMessage mail
=
new
MailMessage();
//
set the addresses
mail.To.Add(destEmail);
//
set the content
mail.Subject
=
EmailTitle;
//
screen scrape the html
string
html
=
EmailContent;
mail.Body
=
html;
mail.IsBodyHtml
=
true
;
//
send the message
SmtpClient smtp
=
new
SmtpClient();
smtp.Send(mail);
}
catch
(Exception e)
{
return
"
fail<br>
"
+
e.ToString();
//
发送失败,返回fail
}
return
"
ok
"
;
//
发送成功,返回 ok
}
}
}
web.config的内容如下:
<
system.net
>
<
mailSettings
>
<!--
发送邮件设置,把这里的邮箱地址和密码设置成你自己的就ok了
-->
<
smtp from
=
"
yours@126.com
"
>
<
network host
=
"
smtp.126.com
"
port
=
"
25
"
userName
=
"
yours@126.com
"
password
=
"
123456
"
defaultCredentials
=
"
false
"
/>
</
smtp
>
</
mailSettings
>
</
system.net
>
posted on
2009-01-11 14:37
seagreen7
阅读(
396
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部