asp net 2.0 邮件 代码
来源:互联网 发布:边界标志算法 编辑:程序博客网 时间:2024/06/13 17:28
页面代码如下
<body>
<form id="form1" runat="server">
<div>
<table border="2" style="z-index: 106; left: 0px; position: absolute; top: 0px; width: 393px;">
<tr>
<td style="width: 165px">
发件人:</td>
<td style="width: 123px">
<asp:TextBox ID="tb_From" runat="server"></asp:TextBox>
</td>
<td style="width: 526px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="发件人地址不正确!!!"
Style="z-index: 100; left: 0px; position: relative; top: 0px" ControlToValidate="tb_From" Display="Dynamic" ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*"></asp:RegularExpressionValidator></td>
</tr>
<tr>
<td style="width: 165px">
收件人:</td>
<td style="width: 123px">
<asp:TextBox ID="tb_To" runat="server" ></asp:TextBox>
</td>
<td style="width: 526px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="收件人地址不正确!!!" ControlToValidate="tb_To" Display="Dynamic" ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*"
></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td style="width: 165px; height: 41px;">
主题:</td>
<td style="width: 123px; height: 41px;">
<asp:TextBox ID="tb_Sub" runat="server"></asp:TextBox>
</td>
<td style="width: 526px; height: 41px"> </td>
</tr>
<tr>
<td style="width: 165px">
内容:</td>
<td style="width: 123px">
<asp:TextBox ID="tb_body" runat="server" Height="148px"></asp:TextBox>
</td>
<td style="width: 526px"> </td>
</tr>
<tr>
<td style="width:100px; height: 28px;" colspan="3">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="发送" />
<asp:Label ID="lab_Statu" runat="server" Width="213px"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
<form id="form1" runat="server">
<div>
<table border="2" style="z-index: 106; left: 0px; position: absolute; top: 0px; width: 393px;">
<tr>
<td style="width: 165px">
发件人:</td>
<td style="width: 123px">
<asp:TextBox ID="tb_From" runat="server"></asp:TextBox>
</td>
<td style="width: 526px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="发件人地址不正确!!!"
Style="z-index: 100; left: 0px; position: relative; top: 0px" ControlToValidate="tb_From" Display="Dynamic" ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*"></asp:RegularExpressionValidator></td>
</tr>
<tr>
<td style="width: 165px">
收件人:</td>
<td style="width: 123px">
<asp:TextBox ID="tb_To" runat="server" ></asp:TextBox>
</td>
<td style="width: 526px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="收件人地址不正确!!!" ControlToValidate="tb_To" Display="Dynamic" ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*"
></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td style="width: 165px; height: 41px;">
主题:</td>
<td style="width: 123px; height: 41px;">
<asp:TextBox ID="tb_Sub" runat="server"></asp:TextBox>
</td>
<td style="width: 526px; height: 41px"> </td>
</tr>
<tr>
<td style="width: 165px">
内容:</td>
<td style="width: 123px">
<asp:TextBox ID="tb_body" runat="server" Height="148px"></asp:TextBox>
</td>
<td style="width: 526px"> </td>
</tr>
<tr>
<td style="width:100px; height: 28px;" colspan="3">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="发送" />
<asp:Label ID="lab_Statu" runat="server" Width="213px"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
程序代码如下(c#):
protected void Button1_Click(object sender, EventArgs e)
...{
string mailServerName = ConfigurationSettings.AppSettings["mailserver"];
string mailFrom = tb_From.Text ;
string mailTo = tb_To.Text;
string mailSub = tb_Sub.Text; ;
string mailBody = tb_body.Text;
try
...{
using (MailMessage message =
new MailMessage(mailFrom, mailTo, mailSub, mailBody))
...{
// SmtpClient is used to send the e-mail
SmtpClient mailClient = new SmtpClient(mailServerName, 25);
// UseDefaultCredentials tells the mail client to use the
// Windows credentials of the account (i.e. user account)
// being used to run the application
mailClient.UseDefaultCredentials = true;
// Send delivers the message to the mail server
mailClient.Send(message);
}
lab_Statu.Text = "send success!!!";
}
catch (SmtpException ex)
...{
lab_Statu.Text = ex.Message;
}
catch (Exception ex)
...{
lab_Statu.Text = ex.Message;
}
}
...{
string mailServerName = ConfigurationSettings.AppSettings["mailserver"];
string mailFrom = tb_From.Text ;
string mailTo = tb_To.Text;
string mailSub = tb_Sub.Text; ;
string mailBody = tb_body.Text;
try
...{
using (MailMessage message =
new MailMessage(mailFrom, mailTo, mailSub, mailBody))
...{
// SmtpClient is used to send the e-mail
SmtpClient mailClient = new SmtpClient(mailServerName, 25);
// UseDefaultCredentials tells the mail client to use the
// Windows credentials of the account (i.e. user account)
// being used to run the application
mailClient.UseDefaultCredentials = true;
// Send delivers the message to the mail server
mailClient.Send(message);
}
lab_Statu.Text = "send success!!!";
}
catch (SmtpException ex)
...{
lab_Statu.Text = ex.Message;
}
catch (Exception ex)
...{
lab_Statu.Text = ex.Message;
}
}
邮件服务器的地址是在web.config中配置的
<appSettings>
<add key="mailserver" value="邮件服务器地址"/>
</appSettings>
<add key="mailserver" value="邮件服务器地址"/>
</appSettings>
还有可能就是遇到"邮箱不可用。 服务器响应为: 5.7.1 Unable to relay for "
解决的方法是:
在IIS中,右击“默认SMTP虚拟服务器”,选择“属性”,切换到“访问”页,点击“中继”按钮,在弹出框中选择“仅以下列表除外”,确定。
呵呵,这样应该就可以解决了~
- asp net 2.0 邮件 代码
- ASP.Net 2.0 发送邮件的代码
- ASP.Net 2.0 发送邮件的代码
- ASP.Net 2.0 发送邮件的代码
- ASP.Net 2.0 发送邮件的代码
- ASP.Net 2.0 发送邮件的代码
- asp.net 发送邮件代码
- asp.net 发送邮件代码
- asp.net 发送邮件代码
- asp.net发送邮件代码!
- asp.net 发送邮件代码
- asp.net 发送邮件代码
- asp.net 发送邮件代码
- ASP.NET发送邮件代码
- asp.net 发送邮件代码
- asp.net 发送邮件代码
- asp.net 发送邮件代码
- asp.net 发送邮件代码
- 附加和分离数据库
- 闪聊与Discuz整合插件介绍
- 从今天开始每天要花点时间读《代码大全2》了
- 一个合格的程序员该做的事(转载)
- WebService基于契约编程的基础-WS-Policy学习笔记
- asp net 2.0 邮件 代码
- asp.net皮膚樣式
- 我的技术生涯
- Matlab7.0学习笔记(8)Handle Graphics
- How to Use or Disable the Information Bar
- 用户登陆操作(ASP.NET)
- 补充:关于802.1X身份验证中CA证书的配置
- 中国家长的身上藏着十把刀
- Javascript 刷新框架及页面的方法总集