一、在 .net 2.0 中,发邮件变得非常简单。
public static void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false; //启用身份认证
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.Default;
message.IsBodyHtml = false; //是否采用html格式邮件
//message.Priority = MailPriority.High; //邮件优先级
//message.Attachments.Add(new Attachment(@"F:\我的文档\消息.txt")); //附件
//message.Body = new System.IO.StreamReader(@"F:\我的文档\ttt.txt", Encoding.Default).ReadToEnd(); 从文本文件中读取邮件内容
client.Send(message);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nick4/archive/2008/05/14/2444882.aspx
二、 using System.Net.Mail;
MailMessage mail = new MailMessage("xzhjq@163.com", "s_oriq@yahoo.com.cn");//发送者、接收者
mail.Subject = "1111111111";
mail.Body = "2222222222222";
SmtpClient sc = new SmtpClient("smtp.163.com");
sc.Credentials = new NetworkCredential("xzhjq@163.com", "*");//*为密码
sc.Send(mail);
Response.Write("success!");
.net2.0使用新的命名空间System.Net.Mail,取代了原来的System.Web.Mail。
以上代码在vs2005,winXP下测试通过。
下面是另外一个例子,比较详细:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("xzhjq@163.com", "soriq"); //发送方
msg.To.Add(new MailAddress("xzhjq@163.com", "guide")); //接收方
//msg.To.Add(new MailAddress("destination2@domain.com", "Addressee 2's Name")); //发送到多个用户
msg.Subject = "Message Subject"; //主题
msg.Body = "Mail body content"; //内容
msg.IsBodyHtml = true; //是否允许html
msg.Priority = MailPriority.High; //优先级
SmtpClient c = new SmtpClient("smtp.163.com");
c.Credentials = new NetworkCredential("xzhjq@163.com", "密码");
c.Send(msg);
using System;
using System.Data;
using System.Configuration;
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;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//asp.net2.0中使用smtpclient类
MailMessage m = new MailMessage();
m.Subject = subject.Text.ToString();
// using System.Text;
m.SubjectEncoding=Encoding.UTF8;
m.From = new MailAddress(from.Text.ToString());
m.To.Add(to.Text.ToString());/// m.to是只读属性
m.Body=Body.Text.ToString();
m.BodyEncoding= Encoding.UTF8;
m.IsBodyHtml=HtmlCB.Checked;
SmtpClient client = new SmtpClient();
client.Host = "127.0.0.1";///设置邮件服务器地址
client.Port = 25;///端口
client.DeliveryMethod = SmtpDeliveryMethod.Network;///邮件发送属性
client.UseDefaultCredentials = false;
client.Send(m);
Response.Write("发送成功!!!");
}
}