ASP.NET发送电子邮件代码
<table align="center" border="0" cellpadding="4" cellspacing="1" width="600" bgcolor="#cccccc">
<tr>
<td colspan="2" bgcolor="#f0f0f0" align="center">
电子邮件发送测试程序</td>
</tr>
<tr>
<td bgcolor="#f0f0f0" align="right" width="150">发送人:</td>
<td bgcolor="#ffffff" align="left"><asp:TextBox ID="fromMail" runat="server" Width="300" /></td>
</tr>
<tr>
<td bgcolor="#f0f0f0" align="right">收件人:</td>
<td bgcolor="#ffffff" align="left"><asp:TextBox ID="toMail" runat="server" Width="300" /></td>
</tr>
<tr>
<td bgcolor="#f0f0f0" align="right">抄送人:</td>
<td bgcolor="#ffffff" align="left"><asp:TextBox ID="ccMail" runat="server" Width="300" /></td>
</tr>
<tr>
<td bgcolor="#f0f0f0" align="right">暗送人:</td>
<td bgcolor="#ffffff" align="left"><asp:TextBox ID="bccMail" runat="server" Width="300" /></td>
</tr>
<tr>
<td bgcolor="#f0f0f0" align="right">主 题:</td>
<td bgcolor="#ffffff" align="left"><asp:TextBox ID="subject" runat="server" Width="300" /></td>
</tr>
<tr>
<td bgcolor="#f0f0f0" align="right">附 件:</td>
<td bgcolor="#ffffff" align="left"><input type="file" id="upfile" runat="server" /></td>
</tr>
<tr>
<td bgcolor="#f0f0f0" align="right">内 容:</td>
<td bgcolor="#ffffff" align="left"><asp:TextBox ID="body" TextMode="multiLine" runat="server" Width="300" Height="200" /></td>
</tr>
<tr>
<td bgcolor="#f0f0f0" align="right">格 式:</td>
<td bgcolor="#ffffff" align="left"><asp:RadioButtonList ID="format" runat="server"/>
<asp:Button ID="bt" runat="server" Text="确定发送" OnClick="bt_Click" /></td>
</tr>
</table>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
post.aspx.cs
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
//using System.Net.Mail;
//using System.Net;
using System.Web.Util;
using System.Web.Mail;
public partial class BookPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bt_Click(object sender, EventArgs e)
{
bool flag = SendMail(fromMail.Text, toMail.Text, ccMail.Text, bccMail.Text, subject.Text, body.Text, format.SelectedValue);
if (flag == true)
{
Response.Write("<script>alert('发送成功!');</script>");
}
else
{
Response.Write("<script>alert('发送失败!');</script>");
}
}
private bool SendMail(string fromMail, string toMail, string ccMail, string bccMail, string subject, string body, string sendMode)
{
try
{
MailMessage myMail = new MailMessage();
myMail.From = fromMail;
myMail.To = fromMail;
myMail.Cc = ccMail;
myMail.Bcc = bccMail;
myMail.Subject = subject;
myMail.Body = body;
myMail.BodyFormat = sendMode == "0" ? MailFormat.Text : MailFormat.Html;
//附件
string ServerFileName = "";
if (this.upfile.PostedFile.ContentLength != 0)
{
string upFileName = this.upfile.PostedFile.FileName;
string[] strTemp = upFileName.Split('.');
string upFileExp = strTemp[strTemp.Length - 1].ToString();
ServerFileName = Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp);
this.upfile.PostedFile.SaveAs(ServerFileName);
myMail.Attachments.Add(new MailAttachment(ServerFileName));
}
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "huangxinxin"); //发送方邮件帐户
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "huangxinxin"); //发送方邮件密码
SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
SmtpMail.Send(myMail);
return true;
}
catch
{
return false;
}
}