发送邮件的几种方法

转自:http://blog.csdn.net/Python/archive/2006/09/03/1163211.aspx

使用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();
}

 相关链接:http://www.51cto.com/art/200509/3306.htm

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值