您的位置:首页 > 其它

如何将网页表单中提交的信息自动发送到指定邮箱?

2006-03-02 09:38 661 查看
看到了.NET 提供System.Web.Mail和System.Net.Mail两个NameSpace,但是用哪一个,两者有何区别,目前不清楚,正在学习中……

=========================================
2006-3-11 更新

这个问题得到了初步解决!下面是code:

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
string mailfrom = Request.Form["txtFrom"];
string mailto = Request.Form["txtTo"];
string mailsub = Request.Form["txtSub"];
string mailbody = Request.Form["txaBody"];

if ( !Page.IsPostBack )
{
try
{

Mess(mailfrom, mailto, mailsub, mailbody);

ErrorMessage.Text = "succeed!";
Info.Text = "From:"+mailfrom;
Info1.Text = "To:" + mailto;
Info2.Text = "Sub:" + mailsub;
Info3.Text = "Content:" + mailbody;
}
catch
{
ErrorMessage.Text = "Sorry,Error!";
}
}

}

protected void Mess(string from,string to,string subject,string body)
{

SmtpClient Client = new SmtpClient("mail.zcom.com.cn");
Client.DeliveryMethod = SmtpDeliveryMethod.Network; //用到了System.Net 命名空间
Client.Credentials = new NetworkCredential("yourname@domain","password");

MailMessage Message = new MailMessage(from,to,subject,body);
Message.BodyEncoding = System.Text.Encoding.UTF8;
//Message.BodyEncoding = System.Text.Encoding.Unicode;
//Message.SubjectEncoding = System.Text.Encoding.Unicode;

Client.Send(Message);

Message.Dispose();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>邮件测试</title>
</head>
<body>
<form id="form1" runat="server" >
<div>
<asp:Label ID="ErrorMessage" runat="server"></asp:Label><br />
<asp:Label ID="Info" runat="server"></asp:Label><br />
<asp:Label ID="Info1" runat="server"></asp:Label><br />
<asp:Label ID="Info2" runat="server"></asp:Label><br />
<asp:Label ID="Info3" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

从Form 中获得信息,然后显示发送是否成功!但是发送中文,收到的都是乱码,如果有知道解决办法的朋友请告知,感谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐