使用.net 2.0 的 using System.Net命名空间下的Mail.SmtpClient发送邮件。
如果不使用本机SMTP服务器可以不配置代码可以借鉴我的另一篇文章:http://tb.blog.csdn.net/TrackBack.aspx?PostId=1586465
1.准备SMTP服务器。以xp为例,安装windows组件SMTP Service。
2.设置SMTP服务器。在IIS中右击“默认 SMTP虚拟服务器”,在“安全”选项卡中添加用户,如“Administrator”(没权限会报错哦!)
3.之后就可以编码拉
后台按钮事件:
private
const
string
FromAddress
=
"
Mail@LiCeRForDream.com
"
;
protected void Page_Load( object sender, EventArgs e)
... {
}
protected void Button1_Click( object sender, EventArgs e)
... {
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
//收件人
message.To.Add(txtMailAddress.Text);
//抄送
foreach(string mailAddress in txtCopyAddress.Text.Split(','))
...{
if (!string.IsNullOrEmpty(mailAddress))
...{
message.CC.Add(mailAddress);
}
}
//主题
message.Subject = txtSubject.Text;
message.From = new System.Net.Mail.MailAddress(FromAddress);
message.Body = txtMailContents.Text;
//发送附件
if (fulAttachment.HasFile)
...{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(fulAttachment.PostedFile.FileName);
message.Attachments.Add(attachment);
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");//SMTP服务器地址
try
...{
smtp.Send(message);
lblReturnMessage.Text = "Mail Send Success!";
}
catch (System.Net.Mail.SmtpException ex)
...{
lblReturnMessage.Text = "Send Error:" + ex.Message;
}
}
protected void Page_Load( object sender, EventArgs e)
... {
}
protected void Button1_Click( object sender, EventArgs e)
... {
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
//收件人
message.To.Add(txtMailAddress.Text);
//抄送
foreach(string mailAddress in txtCopyAddress.Text.Split(','))
...{
if (!string.IsNullOrEmpty(mailAddress))
...{
message.CC.Add(mailAddress);
}
}
//主题
message.Subject = txtSubject.Text;
message.From = new System.Net.Mail.MailAddress(FromAddress);
message.Body = txtMailContents.Text;
//发送附件
if (fulAttachment.HasFile)
...{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(fulAttachment.PostedFile.FileName);
message.Attachments.Add(attachment);
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");//SMTP服务器地址
try
...{
smtp.Send(message);
lblReturnMessage.Text = "Mail Send Success!";
}
catch (System.Net.Mail.SmtpException ex)
...{
lblReturnMessage.Text = "Send Error:" + ex.Message;
}
}
在程序开头不要忘记了引用哦
using
System.Net;
前台页面:
<
form
id
="form1"
runat
="server"
>
< div >
< asp:Label ID ="Label1" runat ="server" Text ="Email Address:" Width ="120px" ></ asp:Label >
< asp:TextBox ID ="txtMailAddress" runat ="server" Width ="300px" ></ asp:TextBox >
< asp:RequiredFieldValidator ID ="RequiredFieldValidator1" runat ="server" ControlToValidate ="txtMailAddress"
ErrorMessage ="Please Type Mail Address" Display ="Dynamic" ></ asp:RequiredFieldValidator >
< asp:RegularExpressionValidator ID ="RegularExpressionValidator1" runat ="server" ControlToValidate ="txtMailAddress"
ErrorMessage ="Invalidate mail address!" ValidationExpression ="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*" Display ="Dynamic" ></ asp:RegularExpressionValidator >< br />
< asp:Label ID ="Label4" runat ="server" Text ="Copy To:" Width ="120px" ></ asp:Label >
< asp:TextBox ID ="txtCopyAddress" runat ="server" Width ="300px" ></ asp:TextBox >< br />
< asp:Label ID ="Label3" runat ="server" Text ="Subject Line:" Width ="120px" ></ asp:Label >
< asp:TextBox ID ="txtSubject" runat ="server" Width ="300px" ></ asp:TextBox >
< asp:RequiredFieldValidator ID ="RequiredFieldValidator2" runat ="server" ControlToValidate ="txtSubject"
ErrorMessage ="Please Type Suject Line" ></ asp:RequiredFieldValidator >< br />
< asp:Label ID ="Label2" runat ="server" Text ="Email Contents:" Width ="120px" ></ asp:Label >
< asp:TextBox ID ="txtMailContents" runat ="server" Rows ="5" TextMode ="MultiLine" Width ="300px" ></ asp:TextBox >
< br />
< asp:Label ID ="Label5" runat ="server" Text ="Attachment :" Width ="120px" ></ asp:Label >
< asp:FileUpload ID ="fulAttachment" runat ="server" />< br />
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="Send" />
< asp:Label ID ="lblReturnMessage" runat ="server" Text ="Label" ></ asp:Label ></ div >
</ form >
< div >
< asp:Label ID ="Label1" runat ="server" Text ="Email Address:" Width ="120px" ></ asp:Label >
< asp:TextBox ID ="txtMailAddress" runat ="server" Width ="300px" ></ asp:TextBox >
< asp:RequiredFieldValidator ID ="RequiredFieldValidator1" runat ="server" ControlToValidate ="txtMailAddress"
ErrorMessage ="Please Type Mail Address" Display ="Dynamic" ></ asp:RequiredFieldValidator >
< asp:RegularExpressionValidator ID ="RegularExpressionValidator1" runat ="server" ControlToValidate ="txtMailAddress"
ErrorMessage ="Invalidate mail address!" ValidationExpression ="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*" Display ="Dynamic" ></ asp:RegularExpressionValidator >< br />
< asp:Label ID ="Label4" runat ="server" Text ="Copy To:" Width ="120px" ></ asp:Label >
< asp:TextBox ID ="txtCopyAddress" runat ="server" Width ="300px" ></ asp:TextBox >< br />
< asp:Label ID ="Label3" runat ="server" Text ="Subject Line:" Width ="120px" ></ asp:Label >
< asp:TextBox ID ="txtSubject" runat ="server" Width ="300px" ></ asp:TextBox >
< asp:RequiredFieldValidator ID ="RequiredFieldValidator2" runat ="server" ControlToValidate ="txtSubject"
ErrorMessage ="Please Type Suject Line" ></ asp:RequiredFieldValidator >< br />
< asp:Label ID ="Label2" runat ="server" Text ="Email Contents:" Width ="120px" ></ asp:Label >
< asp:TextBox ID ="txtMailContents" runat ="server" Rows ="5" TextMode ="MultiLine" Width ="300px" ></ asp:TextBox >
< br />
< asp:Label ID ="Label5" runat ="server" Text ="Attachment :" Width ="120px" ></ asp:Label >
< asp:FileUpload ID ="fulAttachment" runat ="server" />< br />
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="Send" />
< asp:Label ID ="lblReturnMessage" runat ="server" Text ="Label" ></ asp:Label ></ div >
</ form >
4.OK,Finished.简单测试通过。(hotmail发不过去=,=!)