邮件发送类,支持HTML格式,支持优先级设置。通过SOCKET类实现的
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Blood.Com.ClassLib
{
/// <summary>
/// TcpClient派生类,用来进行SMTP服务器的连接工作
/// </summary>
public class SMTPClient : TcpClient
{
public bool isConnected()
{
return Active;
}
public void SendCommandToServer(string Command)
{
NetworkStream ns = this.GetStream() ;
byte[] WriteBuffer ;
WriteBuffer = new byte[1024] ;
WriteBuffer = Encoding.Default.GetBytes(Command) ;
ns.Write(WriteBuffer,0,WriteBuffer.Length);
return ;
}
public string GetServerResponse()
{
int StreamSize ;
string ReturnValue = "" ;
byte[] ReadBuffer ;
NetworkStream ns = this.GetStream() ;
ReadBuffer = new byte[1024] ;
StreamSize = ns.Read(ReadBuffer,0,ReadBuffer.Length);
if (StreamSize==0)
{
return ReturnValue ;
}
else
{
ReturnValue = Encoding.Default.GetString(ReadBuffer);
return ReturnValue;
}
}
public bool DoesStringContainSMTPCode(string s,string SMTPCode)
{
return(s.IndexOf(SMTPCode,0,10)==-1)?false:true;
}
} //结束类
/// <summary>
/// 发送邮件类
/// </summary>
public class SMTPMail
{
/// <summary>
/// 错误反馈信息
/// </summary>
private string strErrMessage = null;
/// <summary>
/// SMTP服务器反馈的信息
/// </summary>
private string strResponse;
/// <summary>
/// 构造函数
/// </summary>
public SMTPMail()
{
strErrMessage = "";
strResponse = "";
}
/// <summary>
/// 取得错误反馈信息
/// </summary>
public string ErrorMessage
{
get
{
return strErrMessage ;
}
}
/// <summary>
/// 取得SMTP服务器反馈的信息
/// </summary>
public string ServerResponse
{
get
{
return strResponse;
}
}
/// <summary>
/// 邮件发送优先级
/// </summary>
public enum Prioritys
{
/// <summary>
/// 最高级别
/// </summary>
HIGH = 1,
/// <summary>
/// 默认级别
/// </summary>
NORMAL = 3,
/// <summary>
/// 最低级别
/// </summary>
LOW = 5
}
public void SendMail(string SmtpHost,int Port,string From,string DisplayFromName,string To,string DisplayToName,Prioritys Priority,bool Html,string Base,string Subject,string Message)
{
try
{
string strResponseNumber;
SMTPClient smtpcMail = new SMTPClient();
smtpcMail.Connect(SmtpHost,Port);
bool bolConnect = smtpcMail.isConnected();
//判断是否进行了连接
if (!bolConnect)
{
strErrMessage = "Smtp服务器连接失败...";
return;
}
//读取反馈信息
strResponseNumber = smtpcMail.GetServerResponse();
if (smtpcMail.DoesStringContainSMTPCode(strResponseNumber,"220"))
{
this.strResponse += strResponseNumber;
}
else
{
this.strErrMessage = "连接失败" + strResponseNumber;
return ;
}
string[] strSendBuffer = new string[6];
string[] strResponseCode = {"220","250","251","354","221"}; // success codes from SMTP server
string strData = "";
strData = string.Concat("HELO ",SmtpHost);
strData = string.Concat(strData,"\r\n");
strSendBuffer[0] = strData ;
strData = "";
strData = string.Concat("MAIL FROM: ","<" + From + ">");
strData = string.Concat(strData,"\r\n");
strSendBuffer[1] = strData;
strData = "";
strData = string.Concat("RCPT TO: ","<" + To + ">");
strData = string.Concat(strData,"\r\n");
strSendBuffer[2] = strData;
strData = "" ;
strData = string.Concat("DATA","\r\n");
strSendBuffer[3] = strData ;
strData = "" ;
strData = string.Concat("From: ",DisplayFromName + "<" + From + ">");
strData = string.Concat(strData,"\r\n" );
strData = string.Concat(strData,"To: " );
strData = string.Concat(strData,DisplayToName + "<" + To + ">");
strData = string.Concat(strData,"\r\n" );
strData = string.Concat(strData,"Subject: " );
strData = string.Concat(strData,Subject);
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"MIME-Version: 1.0" );
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"X-Priority: " + Priority);
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"X-MSMail-Priority: " + Priority);
strData = string.Concat(strData,"\r\n");
if(Html == true)
{
strData = string.Concat(strData,"Content-Type: text/html;" );
}
else
{
strData = string.Concat(strData,"Content-Type: text/plain;" );
}
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"charset=\"iso-8859-1\"" );
strData = string.Concat(strData,"\r\n");
if(Html == true)
{
strData = string.Concat(strData,"Content-Transfer-Encoding: text/html;" );
}
else
{
strData = string.Concat(strData,"Content-Transfer-Encoding: text/plain;" );
}
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"Content-Base: \"" + Base + "\"" );
strData = string.Concat(strData,"\r\n" + "\r\n");
strData = string.Concat(strData,Message);
strData = string.Concat(strData,"\r\n.\r\n");
strSendBuffer[4] = strData;
strData = "" ;
strData = string.Concat(strData,"QUIT\r\n");
strSendBuffer[5] = strData;
int i = 0 ;
while(i < strSendBuffer.Length)
{
smtpcMail.SendCommandToServer(strSendBuffer[i]);
strResponseNumber = smtpcMail.GetServerResponse();
for(int j=0;j<strResponseCode.Length;j++)
{
if (smtpcMail.DoesStringContainSMTPCode(strResponseNumber,strResponseCode[j]))
{
this.strResponse += strResponseNumber;
this.strResponse += "<br>";
break;
}
else
{
if(j==strResponseCode.Length-1)
{
this.strErrMessage += strResponseNumber;
this.strErrMessage += strSendBuffer[i];
return;
}
}
}
i++ ;
} // 结束循环
}
catch(SocketException err)
{
this.strErrMessage += err.Message + " " + err.StackTrace;
}
catch(Exception e)
{
this.strErrMessage += e.Message + " " + e.StackTrace;
}
} //结束邮件发送方法
} // 结束类
} // 结束Namespace
邮件发送前台页面
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Test.SendMailSample" %>
<HTML>
<HEAD>
<title>邮件发送例子</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">TD {
FONT-SIZE: 9pt; FONT-FAMILY: "宋体"
}
.TextBox {
BORDER-RIGHT: #000000 thin dashed; BORDER-TOP: #000000 thin dashed; BORDER-LEFT: #000000 thin dashed; BORDER-BOTTOM: #000000 thin dashed
}
</style>
</HEAD>
<body text="#000000" bgColor="#ffffff">
<form runat="server">
<table style="BORDER-COLLAPSE: collapse" borderColor="#111111" cellSpacing="0" cellPadding="3" width="500" align="center" border="1">
<tr>
<td align="middle" colSpan="2">邮件发送例子(SOCKET类)</td>
</tr>
<asp:panel id="pelMessage" Visible="False" Runat="server">
<TR>
<TD align="middle" colSpan="2">
<asp:Label id="lblMessage" runat="server"></asp:Label></TD>
</TR>
</asp:panel>
<tr>
<td align="right" width="150">SMTP服务器:</td>
<td width="350"><asp:textbox id="txtSmtpHost" runat="server" CssClass="TextBox" MaxLength="50"></asp:textbox><asp:requiredfieldvalidator id="rfvSmtpHost" runat="server" ErrorMessage="*" ControlToValidate="txtSmtpHost" Display="Dynamic"></asp:requiredfieldvalidator></td>
</tr>
<tr>
<td align="right" width="150">SMTP服务器端口:</td>
<td width="350"><asp:textbox id="txtPort" runat="server" CssClass="TextBox" MaxLength="6"></asp:textbox><asp:requiredfieldvalidator id="rfvPort" runat="server" ErrorMessage="*" ControlToValidate="txtPort" Display="Dynamic"></asp:requiredfieldvalidator><asp:regularexpressionvalidator id="revPort" runat="server" ErrorMessage="只能输入数字" ValidationExpression="\d{1,}" ControlToValidate="txtPort" Display="Dynamic"></asp:regularexpressionvalidator></td>
</tr>
<tr>
<td align="right" width="150">邮件发送者:</td>
<td width="350"><asp:textbox id="txtFrom" runat="server" CssClass="TextBox" MaxLength="50"></asp:textbox><asp:requiredfieldvalidator id="rfvFrom" runat="server" ErrorMessage="*" ControlToValidate="txtFrom" Display="Dynamic"></asp:requiredfieldvalidator>
<asp:regularexpressionvalidator id="revFrom" runat="server" ErrorMessage="邮件格式错误" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtFrom" Display="Dynamic"></asp:regularexpressionvalidator></td>
</tr>
<tr>
<td align="right" width="150">发送者显示名称:</td>
<td width="350"><asp:textbox id="txtFromName" runat="server" CssClass="TextBox" MaxLength="20"></asp:textbox></td>
</tr>
<tr>
<td align="right" width="150">邮件接收者:</td>
<td width="350"><asp:textbox id="txtTo" runat="server" CssClass="TextBox" MaxLength="50"></asp:textbox><asp:requiredfieldvalidator id="rfvTo" runat="server" ErrorMessage="*" ControlToValidate="txtTo" Display="Dynamic"></asp:requiredfieldvalidator>
<asp:regularexpressionvalidator id="revTo" runat="server" ErrorMessage="邮件格式错误" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtTo" Display="Dynamic"></asp:regularexpressionvalidator></td>
</tr>
<tr>
<td align="right" width="150">接收者显示名称:</td>
<td width="350"><asp:textbox id="txtToName" runat="server" CssClass="TextBox" MaxLength="20"></asp:textbox></td>
</tr>
<tr>
<td align="right" width="150">邮件发送优先级:</td>
<td width="350"><asp:dropdownlist id="ddlPriority" runat="server">
<asp:ListItem Value="高">高</asp:ListItem>
<asp:ListItem Value="默认" Selected="True">默认</asp:ListItem>
<asp:ListItem Value="低">低</asp:ListItem>
</asp:dropdownlist></td>
</tr>
<tr>
<td align="right" width="150">发送HTML格式的邮件:</td>
<td width="350"><asp:dropdownlist id="ddlHtml" runat="server">
<asp:ListItem Value="是" Selected="True">是</asp:ListItem>
<asp:ListItem Value="否">否</asp:ListItem>
</asp:dropdownlist></td>
</tr>
<tr>
<td align="right" width="150">邮件主题:</td>
<td width="350"><asp:textbox id="txtSubject" runat="server" CssClass="TextBox" MaxLength="50"></asp:textbox>
<asp:RequiredFieldValidator id="rfvSubject" runat="server" ErrorMessage="*" ControlToValidate="txtSubject" Display="Dynamic"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right" width="150">邮件内容:</td>
<td width="350">
<asp:textbox id="txtMessage" runat="server" Rows="15" TextMode="MultiLine" CssClass="TextBox" Columns="40"></asp:textbox>
<asp:RequiredFieldValidator id="rfvMessage" runat="server" ErrorMessage="*" ControlToValidate="txtMessage" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr align="middle">
<td colSpan="2">
<asp:Button id="btnSendMail" runat="server" Text="发送邮件" CssClass="TextBox" BackColor="Silver" ForeColor="Blue"></asp:Button>
</td>
</tr>
</table>
</form>
</body>
</HTML>
邮件发送Codebehind
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Blood.Com.ClassLib;
namespace Test
{
/// <summary>
/// 邮件发送例子
/// </summary>
public class SendMailSample : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DropDownList ddlPriority;
protected System.Web.UI.WebControls.DropDownList ddlHtml;
protected System.Web.UI.WebControls.TextBox txtSmtpHost;
protected System.Web.UI.WebControls.TextBox txtPort;
protected System.Web.UI.WebControls.TextBox txtFrom;
protected System.Web.UI.WebControls.TextBox txtFromName;
protected System.Web.UI.WebControls.TextBox txtTo;
protected System.Web.UI.WebControls.TextBox txtToName;
protected System.Web.UI.WebControls.TextBox txtSubject;
protected System.Web.UI.WebControls.TextBox txtMessage;
protected System.Web.UI.WebControls.RegularExpressionValidator revPort;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvSmtpHost;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvPort;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvFrom;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvTo;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvSubject;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvMessage;
protected System.Web.UI.WebControls.Button btnSendMail;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.RegularExpressionValidator revFrom;
protected System.Web.UI.WebControls.RegularExpressionValidator revTo;
protected System.Web.UI.WebControls.Panel pelMessage;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSendMail.Click += new System.EventHandler(this.btnSendMail_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSendMail_Click(object sender, System.EventArgs e)
{
if(IsPostBack)
{
SMTPMail clsMail = new SMTPMail();
string strSmtpHost = txtSmtpHost.Text;
int intPort = Int32.Parse(txtPort.Text);
string strFrom = txtFrom.Text;
string strFromName = txtFromName.Text;
string strTo = txtTo.Text;
string strToName = txtToName.Text;
SMTPMail.Prioritys Priority = SMTPMail.Prioritys.NORMAL;
bool bolHtml;
string strSubject = txtSubject.Text;
string strMessage = txtMessage.Text;
if(ddlPriority.SelectedItem.Text == "默认")
{
Priority = SMTPMail.Prioritys.NORMAL;
}
else if(ddlPriority.SelectedItem.Text == "高")
{
Priority = SMTPMail.Prioritys.HIGH;
}
else if(ddlPriority.SelectedItem.Text == "低")
{
Priority = SMTPMail.Prioritys.LOW;
}
if(ddlHtml.SelectedItem.Text == "是")
{
bolHtml = true;
}
else
{
bolHtml = false;
}
clsMail.SendMail(strSmtpHost,intPort,strFrom,strFromName,strTo,strToName,Priority,bolHtml,"",strSubject,strMessage);
if(clsMail.ErrorMessage !="")
{
pelMessage.Visible = true;
lblMessage.Text = clsMail.ErrorMessage;
lblMessage.ForeColor = System.Drawing.Color.Red;
}
else
{
pelMessage.Visible = true;
lblMessage.Text = "邮件发送成功";
lblMessage.ForeColor = System.Drawing.Color.Blue;
}
}
}
}
}
相关推荐
2. **SmtpClient 类**:负责实际的邮件发送工作。 - `UseDefaultCredentials` 属性:指定是否使用默认凭据进行身份验证。 - `Credentials` 属性:设置用于身份验证的用户名和密码。 - `DeliveryMethod` 属性:...
4. **邮件格式支持**:支持发送HTML邮件和纯文本邮件,同时允许在邮件中嵌入图片和其他资源,提供更丰富的邮件展示效果。 5. **附件支持**:可以方便地添加多个附件,支持各种文件类型,包括图片、文档、音频、视频...
- **优先级设置**:使用`jmail.Priority`设置邮件发送的优先级,值范围一般为1-5,数值越小表示优先级越高。 - **邮件发送**:最后,通过`jmail.Send`方法发送邮件。 ### 其他关键代码片段解析 - **跳转功能**:...
本话题主要关注的是如何利用易语言配合JMail组件来发送HTML格式的邮件,这在现代通信中非常常见,例如企业通知、电子报或营销邮件等。 首先,我们需要了解JMail组件。JMail是Java环境下用于发送邮件的库,它可以...
- `JMail.ContentType = "text/html"`:设置邮件内容类型为HTML,以便支持富文本格式。 - `JMail.Priority = 1`:设置邮件优先级,1代表普通优先级。 - `JMail.From` 和 `JMail.FromName`:指定发件人邮箱地址和...
2. **SMTP客户端类**:在.NET框架中,`System.Net.Mail`命名空间提供了邮件发送相关的类和接口。其中`SmtpClient`类是用于通过SMTP服务器发送邮件的主要类。 3. **邮件消息对象**:`MailMessage`类用于表示邮件消息...
我们这里在发送邮件的时候只是设置了邮件收,发件人,邮件主题和邮件正文部门,在.net里面还可以设置发送邮件的文本格式,优先级等.我们这里就不说明了,相信看看就会明白的. (message.Priority;设置优先级.mssage....
10. **优化点**: 工具类还可以扩展更多功能,比如支持HTML格式的邮件内容、添加抄送和密送、设置邮件优先级、添加CC和BCC等。同时,为了提高效率,可以考虑添加邮件队列,批量发送邮件,以及使用线程池来异步处理...
PHPMailer支持多种邮件发送协议,包括SMTP、SENDMAIL和PHP's mail()函数,因此你可以根据服务器配置选择最适合的发送方式。 PHPMailer的核心特性包括: 1. **SMTP验证**:支持SMTP身份验证,可以使用用户名和密码...
3. **添加HTML内容**:如果需要发送HTML格式的邮件,可以使用AlternateView类。 ```csharp string htmlBody = "<html><body><h1>这是HTML邮件内容</h1></body></html>"; AlternateView htmlView = AlternateView....
6. **邮件格式**: `MailMessage`类允许设置邮件的各种属性,如HTML或纯文本格式的正文,附件,以及邮件的优先级等。可以使用`AlternateView`添加HTML和纯文本两种版本的邮件内容,以便在不同邮件客户端中提供良好的...
通过Jmail,"飞翔263邮件发送系统"能够方便地发送HTML格式的邮件,包含附件,甚至可以设置邮件的优先级和读回执,满足了站长多样化的邮件发送需求。 除了核心功能外,该系统还可能包含了如日志记录、错误处理、邮件...
echo "邮件发送失败. 错误信息: " . $mail->ErrorInfo; } else { echo "邮件已发送"; } ``` 7. **错误处理**: - 如在发送过程中遇到问题,`send()` 方法会返回 `false`,并且你可以通过 `$mail->ErrorInfo` ...
首先,我们来讨论邮件发送的基本原理。在C#中,我们可以利用System.Net.Mail命名空间下的类来实现邮件的发送。主要涉及的类有SmtpClient和MailMessage。 1. **SmtpClient类**:这是用于配置SMTP(Simple Mail ...
邮件发送模块应遵循反垃圾邮件的最佳实践,例如限制发送速率、避免使用常见的垃圾邮件词汇、设置合理的退订选项等。 9. 邮件模板和个性化: 对于批量发送邮件的场景,模块可以支持邮件模板,允许插入动态变量以实现...
在.NET框架中,C#提供了一组用于发送电子邮件的类,这些类位于`System.Net.Mail`命名空间下。其中,`MailMessage`类是核心...开发者可以根据实际需求,利用其丰富的属性和方法来实现各种功能,满足不同的邮件发送场景。
总结来说,C#的邮件发送功能依赖于System.Net.Mail命名空间,通过MailMessage和SmtpClient类实现。开发者可以通过理解这两个类的使用方法,结合实际需求,构建出符合业务逻辑的邮件发送功能。如果使用了“C#发送邮件...
这个类库不仅支持发送HTML内容的电子邮件,还提供了丰富的功能,如添加附件、设置SMTP服务器进行身份验证、处理抄送和密送等。与PHP内置的`mail()`函数相比,PHPMailer更易于使用,功能更全面,能够更好地应对复杂的...
【邮件发送功能的C++封装类——SMailer详解】 在C++编程中,实现邮件发送功能通常涉及到网络编程和电子邮件协议的理解。SMailer是一个精心设计的C++类库,它提供了一个简单易用的接口来处理邮件的发送,包括HTML...
10. **使用第三方库**:除了.NET框架自带的邮件发送功能,还有许多第三方库,如MailKit和MimeKit,提供更强大、灵活的邮件处理能力,包括支持各种复杂的邮件格式和协议。 通过理解以上知识点,你可以利用给定的"C#...