温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:ASP.net邮件发送源码(51aspx原创)
当前文件路径:JH1GEZ9ACZA70/51aspxMail/Default.aspx.cs

1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Web.Util; 12
using System.Web.Mail; 13
14
namespace _51aspxMail 15
{ 16
public partial class _Default : System.Web.UI.Page 17
{ 18
protected void Page_Load(object sender, EventArgs e) 19
{ 20
if (!IsPostBack) 21
{ 22
format.Items.Add(new ListItem("文本", "0")); 23
format.Items.Add(new ListItem("HTML", "1")); 24
format.Items[0].Selected = true; 25
26
fromMail.Text = "test@51aspx.com"; //发送方邮件 27
fromMail.Enabled = false; 28
} 29
} 30
31
private bool SendMail(string fromMail, string toMail, string ccMail, string bccMail, string subject, string body, string sendMode) 32
{ 33
try 34
{ 35
MailMessage myMail = new MailMessage(); 36
myMail.From = fromMail; 37
myMail.To = fromMail; 38
myMail.Cc = ccMail; 39
myMail.Bcc = bccMail; 40
myMail.Subject = subject; 41
myMail.Body = body; 42
myMail.BodyFormat = sendMode == "0" ? MailFormat.Text : MailFormat.Html; 43
44
//附件 45
string ServerFileName = ""; 46
if (this.upfile.PostedFile.ContentLength != 0) 47
{ 48
string upFileName = this.upfile.PostedFile.FileName; 49
string[] strTemp = upFileName.Split('.'); 50
string upFileExp = strTemp[strTemp.Length - 1].ToString(); 51
ServerFileName = Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp); 52
this.upfile.PostedFile.SaveAs(ServerFileName); 53
myMail.Attachments.Add(new MailAttachment(ServerFileName)); 54
} 55
56
57
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1); 58
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "ok2008"); //发送方邮件帐户 59
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456"); //发送方邮件密码 60
61
SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1); 62
SmtpMail.Send(myMail); 63
64
return true; 65
} 66
catch 67
{ 68
return false; 69
} 70
} 71
72
protected void send_Click(object obj, EventArgs e) 73
{ 74
bool flag = SendMail(fromMail.Text, toMail.Text, ccMail.Text, bccMail.Text, subject.Text, body.Text, format.SelectedValue); 75
if (flag == true) 76
{ 77
Response.Write("<script>alert('发送成功!');</script>"); 78
} 79
else 80
{ 81
Response.Write("<script>alert('发送失败!');</script>"); 82
} 83
84
85
} 86
} 87
}





}
}