SendMail.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendMail.aspx.cs" Inherits="Admin_SendMail" %>
<!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 id="Head1" runat="server">
<title>SendMail</title>
</head>
<body>
<form id="form1" runat="server">
<table border="0" align="center" cellpadding="0" cellspacing="5" >
<tr>
<td width="100">收件人:</td>
<td width="600"><asp:TextBox id="Addressee" runat="server" Width="600"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Addressee can't be empty!" ControlToValidate="Subject" Font-Bold="True"
Display="Dynamic" ForeColor="Black"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="re" runat="server" ErrorMessage="The format inaccuracy of Email!" ControlToValidate="Addressee" Font-Bold="True"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Black"></asp:RegularExpressionValidator> </td>
</tr>
<tr>
<td>抄送人:</td>
<td><asp:TextBox id="Copy" runat="server" Width="600"></asp:TextBox></td>
</tr>
<tr>
<td>密送人:</td>
<td><asp:TextBox id="secret" runat="server" Width="600"></asp:TextBox></td>
</tr>
<tr>
<td>主题:</td>
<td><asp:TextBox id="Subject" runat="server" Width="600"></asp:TextBox>
<asp:RequiredFieldValidator id="ru" runat="server" ErrorMessage="Subject can't be empty!" ControlToValidate="Subject" Font-Bold="True"
Display="Dynamic" ForeColor="Black"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>附件:</td>
<td><asp:TextBox id="Attachment" runat="server" Width="600"></asp:TextBox></td>
</tr>
<tr>
<td>正文:</td>
<td><asp:TextBox ID="Body" runat="server" TextMode="MultiLine" Rows="18" Width="600"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Send" runat="server" Text="Send" />
<asp:Button ID="Refurbish" runat="server" Text="Refurbish"></asp:Button> </td>
</tr>
</table>
</form>
</body>
</html>
SendMail.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.Mail; //核心代码开始
using System.Windows.Forms;
//以下暂无调试
public partial class Admin_SendMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public bool AddRecipient(string str) //抄送的人
{
str = str.Trim();
if (str == null || str == "" || str.IndexOf("@") == -1)
return true;
if (RecipientNum < 10)
{
Recipient.Add(RecipientNum, str);
RecipientNum++;
return true;
}
else
{
errmsg += "收件人过多";
return false;
}
}
public bool AddRecipientBCC(string str) //密送的人
{
if (str == null || str.Trim() == "")
return true;
if (RecipientBCCNum < 10)
{
RecipientBCC.Add(RecipientBCCNum, str);
RecipientBCCNum++;
return true;
}
else
{
errmsg += "收件人过多";
return false;
}
}
public void SendMailUseGmail(object sender, EventArgs e)
{
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// 创建邮件消息
objMailMessage = new MailMessage();
//发件人EMAIL
objMailMessage.From = "ujtrading@ujtrading.com";//源邮件地址
//收件人EMAIL
String Addressee = this.Addressee.Text;
objMailMessage.To = Addressee; //目的邮件地址
//objMailMessage.To = "shirley@ujtrading.com";
//邮件主题
String Subject = this.Subject.Text;
objMailMessage.Subject = Subject; //发送邮件的标题
//objMailMessage.Subject = "test";
//邮件内容
String Body = this.Body.Text;
objMailMessage.Body = Body;//发送邮件的内容
// 创建一个附件对象
String Attachment = this.Attachment.Text;
if (this.Attachment.Text.ToString() != "")
{
objMailAttachment = new MailAttachment(Attachment);//发送邮件的附件 c:\\test.txt
objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中
}
//接着利用SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本
//基本权限
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "ujtrading");
//密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "789456");
//如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied
//SMTP地址
SmtpMail.SmtpServer = "smtp.ujtrading.com";
//开始发送邮件
try
{
SmtpMail.Send(objMailMessage);
//简单一点儿可以client.Send(msg);
MessageBox.Show("Send mail success!","Messige");
this.Addressee.Text = "";
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "Send mail fail!");
}
//核心代码结束
}
protected void Refurbish_Click(object sender, EventArgs e)
{
this.Addressee.Text = "";
this.Subject.Text = "";
this.Body.Text = "";
this.Attachment.Text = "";
}
}
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com