您的位置:首页 > 其它

发送邮件的几种方法

2006-09-03 00:35 441 查看
使用System.Web.Mail发送邮件:

public void SendMail()
{
MailMessage mail1 = new MailMessage();
mail1.Body="body here"; //邮件的正文
mail1.From="xxx@your.com"; //发信人的地址
mail1.To="yyy@their.com"; //收信人的地址
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1); //要求smtp认证
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","xxx"); //smtp认证的用户
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********"); //smtp认证的密码
SmtpMail.SmtpServer="mail.your.com"; //smtp服务器
SmtpMail.Send(mail1); //发送邮件
}

使用System.Net.Mail发送邮件:
发现用这种方式发邮件,如果发邮件的计算机开启了杀毒软件的邮件监控,会有错误提示,但能够发信成功。

1)第一种方式:先配置web.config文件
<system.net>
<mailSettings>
<smtp from="your@your.com">
<network host="smtp.your.com" port="25" userName="your" password="yourpass" />
</smtp>
</mailSettings>
</system.net>

然后通下面的程序发信:
private void SendMail()
{
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
message.To.Add("to@yourmail.com"); //收信人地址
message.SubjectEncoding = System.Text.Encoding.UTF8; //主题文字编码方式
message.BodyEncoding = System.Text.Encoding.UTF8; //内容文字编码方式

message.Subject = "A test for sending mail"; //主题
message.Body = "Thanks for your sending mail./n/n"; //内容

smtp.Send(message); //发信
message.Dispose();
}

2)第二种方式是不配置web.config文件,直接通过下面的程序发信:
private void SendMail()
{
SmtpClient smtp = new SmtpClient("mail.your.com", 25);
smtp.Credentials = new System.Net.NetworkCredential("from@your.com", "yourpass"); //提供发信smtp认证信息
MailAddress from = new MailAddress("from@your.com", "tiger", System.Text.Encoding.UTF8); //发信人信息
MailAddress to = new MailAddress("to@their.com"); //收信人信息
MailMessage message = new MailMessage(from, to);
message.Subject = "这里是主题";
message.Body = "正文内容";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
smtp.Send(message);
message.Dispose();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: