C#中用126smtp发送邮件.

这段代码展示了如何在C#应用程序中利用126SMTP服务发送一封包含HTML内容的邮件。当Button1被点击时,程序会验证输入,然后创建一个MailMessage对象,设置发件人、收件人、抄送人、主题和HTML正文,并通过SmtpMail类的SmtpServer属性指定SMTP服务器地址,最后发送邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private void Button1_Click(object sender, System.EventArgs e)
  {
  if(IsValid)
  {
   Label13.Text="Welcome "+ TextBox1.Text +" "+TextBox2.Text + ".You have successfully registered.";
   HttpCookie mycookie=new HttpCookie("WebShoppe","You have visited the WebShoppe Site.");
   mycookie.Expires=System.Convert.ToDateTime("12/12/2005");
   Response.Cookies.Add(mycookie);
   MailMessage msg = new MailMessage();
   StringWriter strwriter =new StringWriter();
   HtmlTextWriter htmltxtwriter =new HtmlTextWriter(strwriter);
   htmltxtwriter.RenderBeginTag("html"); 
   htmltxtwriter.RenderBeginTag("head");
   htmltxtwriter.RenderBeginTag("title");
   htmltxtwriter.Write("Thank You!");
   htmltxtwriter.RenderEndTag();
   htmltxtwriter.RenderEndTag();
   htmltxtwriter.RenderBeginTag("body");
   htmltxtwriter.WriteLine("Thank you for registering with WebShoppe Site!");
   htmltxtwriter.RenderEndTag();
   htmltxtwriter.RenderEndTag();
   msg.From ="pangtianbing@126.com"; //要用在126smtp中注册过的邮箱,并和下面代码中的注册名及密码要相符
   msg.To=TextBox12.Text;
   msg.Bcc="tangjingtian123@hotmail.com";
   msg.Subject="Thanks for registering";
   msg.Body=strwriter.ToString();
   msg.BodyFormat=MailFormat.Html;
   msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //确认需要认证 
   msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "pangtianbing"); //设置在smtp中使用的用户名 
   msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "**********"); //设置在smtp中使用的密码. 

   SmtpMail.SmtpServer="smtp.126.com";

   SmtpMail.Send(msg);
  }
  } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值