web.config
<httpRuntime requestValidationMode="2.0"/>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sendemail.aspx.cs" Inherits="发邮件.sendemail" ValidateRequest="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>发邮件</title>
<link href="kindeditor-4.1.5/themes/default/default.css" rel="stylesheet" type="text/css" />
<link href="kindeditor-4.1.5/plugins/code/prettify.css" rel="stylesheet" type="text/css" />
<script src="kindeditor-4.1.5/kindeditor.js" type="text/javascript"></script>
<script charset="utf-8" src="kindeditor-4.1.5/kindeditor.js"></script>
<script charset="utf-8" src="kindeditor-4.1.5/lang/zh_CN.js"></script>
<script charset="utf-8" src="kindeditor-4.1.5/plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function (K) {
var editor1 = K.create('#content1', {
cssPath: 'kindeditor-4.1.5/plugins/code/prettify.css',
uploadJson: 'kindeditor-4.1.5/asp.net/upload_json.ashx',
fileManagerJson: 'kindeditor-4.1.5/asp.net/file_manager_json.ashx',
allowFileManager: true,
afterCreate: function () {
var self = this;
K.ctrl(document, 13, function () {
self.sync();
K('form[name=example]')[0].submit();
});
K.ctrl(self.edit.doc, 13, function () {
self.sync();
K('form[name=example]')[0].submit();
});
}
});
prettyPrint();
});
</script>
</head>
<body>
<asp:Label ID="Label1" runat="server" Text="" Visible="false"></asp:Label>
<form id="example" runat="server">
<div align="center">
收件人:<asp:TextBox ID="txt_toman" runat="server" Width="428px"></asp:TextBox>
<br />
主题:<asp:TextBox ID="txt_title" runat="server" Width="441px"></asp:TextBox>
<br />
内容:<asp:TextBox ID="txt_content" runat="server" Height="66px"
TextMode="MultiLine" Width="194px"></asp:TextBox>
<br />
<textarea id="content1" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;" runat="server"></textarea>
<br />
附件:<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="btn_send" runat="server" οnclick="btn_send_Click"
Text=" Send " />
<asp:Label ID="lbl_mag" runat="server" ForeColor="Red"></asp:Label>
</div>
</form>
</body>
</html>
后台代码:
namespace 发邮件
{
public partial class sendemail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = Request.Form["content1"];
}
protected void btn_send_Click(object sender, EventArgs e)
{
bool flag = SendMail("发信人地址(邮箱)", "发信人xx", "发信人用户名", "用户名的密码", txt_title.Text,this.Label1.Text , txt_toman.Text, FileUpload1);
//txt_content.Text
if (flag)
{
lbl_mag.Text = "发送成功";
}
else
{
lbl_mag.Text = "糟糕,发送失败啦!";
}
}
/// <summary>
/// 简单邮件发送器
/// </summary>
/// <param name="user">发信人地址</param>
/// <param name="who">发信人</param>
/// <param name="name">发送用户名</param>
/// <param name="pwd">用户名密码</param>
/// <param name="title">邮件标题</param>
/// <param name="body">发送内容</param>
/// <param name="files">图片地址</param>
/// <param name="shoujian">收件人地址</param>
/// <param name="file">附件</param>
/// <returns>是否发送成功</returns>
public static bool SendMail(string user, string who, string name, string pwd, string title, string body, string shoujian, FileUpload file)
{
MailMessage Message = new MailMessage(
new MailAddress(user, //第一个是发信人的地址,
who, //第二个参数是发信人
Encoding.UTF8), //编码
new MailAddress(shoujian));//收信人邮箱
if (file.HasFile)
{
//添加附件
if (file.PostedFile != null)
{
Attachment attachment = new Attachment(file.PostedFile.InputStream, file.PostedFile.FileName);
Message.Attachments.Add(attachment);
}
}
Message.SubjectEncoding = Encoding.UTF8;
Message.Subject = title;//标题
Message.BodyEncoding = Encoding.UTF8;
Message.IsBodyHtml = true;
Message.Body = body; //主体
//for (int i = 0; i < files.Length; i++)
//{
// Attachment attachment = new Attachment(files[i]);
// Message.Attachments.Add(attachment);
// Message.Body += "<img src=\"kindeditor-4.1.5\\upload\\image\\20130704:" + attachment.ContentId + "\"/>";
//}
Message.Priority = MailPriority.High;
SmtpClient smtpClient = new SmtpClient("smtp.163.com", 25);
//邮件信箱服务器 //发送端口
smtpClient.Credentials = new System.Net.NetworkCredential(name, pwd);
//用户名 //用户名密码
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Timeout = 99999;
try
{
smtpClient.Send(Message);
return true;
}
catch (Exception)
{
return false;
}
}
}
}