您所在的位置: 首页 >> 查看信息 >> .net 2.0 发送Email的可行性方法! 分享家:Addthis中文版

.net 2.0 发送Email的可行性方法!

以下两个例子经过站长亲自测试通过。可惜郁闷的是不能用QQ 163 263等邮件发送。希望日后有人帮满解决!

例子1
------------------------------------------------------------------------------------------------------
// using System.Net.Mail;
Mailmessageage message = new Mailmessageage();
message.From = new MailAddress("User@gmail.com", "Your DisplayName");
message.To.Add(new MailAddress("To@gmail.com")); // the email you want to send email to
message.Subject = "A test email"

message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = "this is just a simple test!<br> Jack"
message.Priority = MailPriority.High;

SmtpClient client = new SmtpClient("smtp.gmail.com", 587); // 587;//Gmail使用的端口
client.Credentials = new System.Net.NetworkCredential("User@gmail.com", "×××××"); // Your user name & password
client.EnableSsl = true; //经过ssl加密
object userState = message;
try
{
client.Send(message);
Response.Write("邮件发送到" + message.To.ToString() + "<br>");
}
catch (Exception ee)
{
Response.Write(ee.messageage + "<br>" + ee.InnerException.messageage);
}

例子2
------------------------------------------------------------------------------------------------------


// using System.Net.Mail;
MailMessage message = new MailMessage();
message.From = new MailAddress("User@gmail.com");

message.To.Add(new MailAddress("to@gmail.com"));

message.Subject = "This is my subject"
message.Body = "This is the content"
SmtpClient client = new SmtpClient();
client.EnableSsl = true;

try
{
client.Send(message);
Response.Write("邮件发送到" + message.To.ToString() + "<br>");
}
catch (Exception ee)
{
Response.Write(ee.Message );
}

//In web.config

<system.net>
<mailSettings>
<smtp from="from@gmail.com">
<network host="smtp.gmail.com" port="587" userName="User@gmail.com" password="your pwd" />
<!-- if has 'defaultCredentials="true"' , using Gmail can not send success -->
</smtp>
</mailSettings>
</system.net>

来源: 湖南IT人才网 - PHPChina湖南省PHP人才网站
作者:hhsacsb
浏览:8131

2009/6/9 17:58:16
搜索文章
 

最新文章列表

热门文章列表