C#发送邮件(VS2008)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
namespace WebUI
{
    public partial class SendMail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                MailMessage mm = new MailMessage();
                MailAddress Fromma = new MailAddress("490261180@qq.com");
                MailAddress Toma = new MailAddress("122097184@qq.com", null);
                 mm.From= Fromma;
                 //收件人
                 mm.To.Add("122097184@qq.com");
                 //邮箱标题
                 mm.Subject = "Hello";
                //邮件内容
                 mm.Body = "大家好!";
                //内容的编码格式
                 mm.BodyEncoding = System.Text.Encoding.UTF8;
                 //mm.ReplyTo = Toma;
                 //mm.Sender =Fromma;
                 //mm.IsBodyHtml = false;
                 mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                 mm.CC.Add(Toma);
                 SmtpClient sc = new SmtpClient();
                 NetworkCredential nc=new NetworkCredential();
                 nc.UserName = "490261180@qq.com";//你的邮箱地址
                 nc.Password = "123456";//你的邮箱密码
                 sc.UseDefaultCredentials = true;
                 sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                 sc.Credentials = nc;
                 //如果这里报mail from address must be same as authorization user这个错误,是你的QQ邮箱没有开启SMTP,
                 //到你自己的邮箱设置一下就可以啦!在帐户下面,如果是163邮箱的话,下面该成smtp.163.com
                 sc.Host = "smtp.qq.com";
                 sc.Send(mm);
              
            }
            catch( Exception ex )
            {
                throw new Exception(ex.Message);
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值