您的位置:首页 > 编程语言 > ASP

Asp.net(c#)发送电子邮件

2007-06-06 11:26 661 查看
sm3-asp.net

书上的代码有点点问题:可能是使用内部的SMTP服务

由于我们使用公用的SMTP,而现在的SMTP都要进行验证,所以

我们加上 msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xmueduzzh"); //set your username here
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "x122333"); //set your password here
//SmtpMail.SmtpServer="lsg.moon.net";
//SmtpMail.SmtpServer="localhost";
SmtpMail.SmtpServer="smtp.163.com";

同时改变:

msg.From ="xmueduzzh@163.com";

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/2007");
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 ="xmueduzzh@163.com";
msg.To=TextBox12.Text;
msg.Bcc="xmueduzzh@163.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"); //basic authentication
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xmueduzzh"); //set your username here
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "x123213"); //set your password here
//SmtpMail.SmtpServer="lsg.moon.net";
//SmtpMail.SmtpServer="localhost";
SmtpMail.SmtpServer="smtp.163.com";
SmtpMail.Send(msg);
}
}
}
}

using System.web.Mail;

MailMessage msg = new MailMessage();
//发送方地址(如test@163.com)
msg.From = "zdyguilong@163.com";
//接收方地址(如test@163.com)
msg.To = "zdyguilong@163.com";
//正文内容类型
msg.BodyFormat = MailFormat.Html;
//正文内容编码
msg.BodyEncoding = System.Text.Encoding.Default;
//主题
msg.Subject = "张大宇向您问好";
//内容
msg.Body = "<html><head><META content=zh-cn http-equiv=Content-Language><meta http-equiv='Content-Type' content='text/html; charset=gb2312'><style type=text/css>A:link { FONT-SIZE: 9pt; TEXT-DECORATION: none; color: #000000}A:visited {FONT-SIZE: 9pt; TEXT-DECORATION: none; color: #666666}A:hover {COLOR: #ff6600; FONT-SIZE: 9pt; TEXT-DECORATION: underline}BODY {FONT-SIZE: 9pt} --></style></head><body><font color=red>用户名:" + UserName.Text.Trim() + "</font><br><font color=green>密码:" + strUserPsw.ToString() + "</font><br><b>验证地址:</b><a href='http://192.168.1.98/AllFiles/Member/CheckFromEmail.aspx?UserName=" + UserName.Text.Trim() + "'target=_blank>http://192.168.1.98/AllFiles/Member/CheckFromEmail.aspx?UserName=" + UserName.Text.Trim() + "</a>。如果您不能点击链接进行跳转,请把这个地址粘贴到浏览器的地址栏直接访问。<br><font color=red>此邮件不必回复,谢谢。</font></body></html>";
//设置为需要用户验证
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置验证用户名
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "zdyguilong");
//设置验证密码
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456");
//邮件服务器地址(如smtp.163.com)
SmtpMail.SmtpServer = "smtp.163.com";
//发送
SmtpMail.Send(msg);
//Response.Write("<script language='javascript'>alert('注册成功');window.location='../default.aspx'</script>");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: