温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:ASP.net邮件发送源码(51aspx原创)
当前文件:
JH1GEZ9ACZA70/51aspxMail/Default.aspx.cs,打开代码结构图
JH1GEZ9ACZA70/51aspxMail/Default.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.Web.Util;
using System.Web.Mail;
namespace _51aspxMail
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
format.Items.Add(new ListItem("文本", "0"));
format.Items.Add(new ListItem("HTML", "1"));
format.Items[0].Selected = true;
fromMail.Text = "test@51aspx.com"; //发送方邮件
fromMail.Enabled = false;
}
}
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", "ok2008"); //发送方邮件帐户
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456"); //发送方邮件密码
SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
SmtpMail.Send(myMail);
return true;
}
catch
{
return false;
}
}
protected void send_Click(object obj, 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>");
}
}
}
}

