页面代码如下
<
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虚拟服务器”,选择“属性”,切换到“访问”页,点击“中继”按钮,在弹出框中选择“仅以下列表除外”,确定。
呵呵,这样应该就可以解决了~