E-mail发送,基于smtp
using System;
using System.Collections;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
public class ESmtpMail
{
protected static readonly string CHARSET = "UTF-8";
protected static Hashtable RESP_CODE = null;
protected TcpClient m_Tcp = null;
protected NetworkStream m_Stream = null;
static ESmtpMail()
{
RESP_CODE = new Hashtable();
RESP_CODE.Add(200, "(nonstandard success response, see rfc876)");
RESP_CODE.Add(211, "System status, or system help reply ");
RESP_CODE.Add(214, "Help message ");
RESP_CODE.Add(220, "<domain> Service ready ");
RESP_CODE.Add(221, "<domain> Service closing transmission channel ");
RESP_CODE.Add(250, "Requested mail action okay, completed ");
RESP_CODE.Add(251, "User not local; will forward to <forward-path> ");
RESP_CODE.Add(354, "Start mail input; end with <CRLF>.<CRLF> ");
RESP_CODE.Add(421, "<domain> Service not available, closing transmission channel ");
RESP_CODE.Add(450, "Requested mail action not taken: mailbox unavailable ");
RESP_CODE.Add(451, "Requested action aborted: local error in processing ");
RESP_CODE.Add(452, "Requested action not taken: insufficient system storage ");
RESP_CODE.Add(500, "Syntax error, command unrecognised ");
RESP_CODE.Add(501, "Syntax error in parameters or arguments ");
RESP_CODE.Add(502, "Command not implemented ");
RESP_CODE.Add(503, "Bad sequence of commands ");
RESP_CODE.Add(504, "Command parameter not implemented ");
RESP_CODE.Add(521, "<domain> does not accept mail (see rfc1846) ");
RESP_CODE.Add(530, "Access denied (a Sendmailism) ");
RESP_CODE.Add(550, "Requested action not taken: mailbox unavailable ");
RESP_CODE.Add(551, "User not local; please try <forward-path> ");
RESP_CODE.Add(552, "Requested mail action aborted: exceeded storage allocation ");
RESP_CODE.Add(553, "Requested action not taken: mailbox name not allowed ");
RESP_CODE.Add(554, "Transaction failed ");
}
/// <summary>
/// Receive Data from SMTP server
/// </summary>
/// <returns></returns>
protected string ReceiveData()
{
string strBuf = null;
byte[] aryBuf = new byte[1024];
int nRevSize = m_Stream.Read(aryBuf, 0, aryBuf.Length);
if (nRevSize > 0)
strBuf = Encoding.Default.GetString(aryBuf).Substring(0, nRevSize);
int RplCode = Convert.ToInt32(strBuf.Substring(0, 3));
if (RplCode >= 400)
{
if (RESP_CODE[RplCode] == null)
{
throw new Exception("unknow error when ReceiveData");
}
else
{
throw new Exception(RESP_CODE[RplCode].ToString());
}
}
return strBuf;
}
/// <summary>
/// Send Data to SMTP server
/// </summary>
/// <param name="strBuf"></param>
protected void SendData(ref string strBuf)
{
byte[] aryBuf = Encoding.ASCII.GetBytes(strBuf);
m_Stream.Write(aryBuf, 0, aryBuf.Length);
m_Stream.Flush();
ReceiveData();
}
/// <summary>
/// Send a email to SMTP server
/// </summary>
/// <param name="strEmail"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <returns></returns>
public bool Send(string strEmail
, string strSubject
, string strBody)
{
try
{
// create a TCP connection
m_Tcp = new TcpClient(Services.Config.SMTPServerIP
, Services.Config.SMTPServerPort
);
m_Tcp.SendTimeout = Services.Config.SMTPTimeout;
m_Tcp.ReceiveTimeout = Services.Config.SMTPTimeout;
m_Tcp.NoDelay = true;
string strBuf = null;
m_Stream = m_Tcp.GetStream();
ReceiveData();
// EHLO
strBuf = null;
strBuf = String.Format("EHLO {0}\r\n", Services.Config.SMTPServerIP);
SendData(ref strBuf);
// AUTH LOGIN
strBuf = String.Format("AUTH LOGIN\r\n");
SendData(ref strBuf);
// Username
strBuf = String.Format("{0}\r\n"
, Classes.Convert.StringToBase64(Services.Config.SMTPUsername)
);
SendData(ref strBuf);
// Password
strBuf = String.Format("{0}\r\n"
, Classes.Convert.StringToBase64(Services.Config.SMTPPassword)
);
SendData(ref strBuf);
// MAIL FROM:
strBuf = String.Format("MAIL FROM: <{0}>\r\n", Services.Config.SenderEmail);
SendData(ref strBuf);
// RCPT TO:
strBuf = String.Format("RCPT TO: <{0}>\r\n", strEmail);
SendData(ref strBuf);
// DATA
strBuf = String.Format("DATA\r\n");
SendData(ref strBuf);
// Reply-To
strBuf += String.Format("Reply-To: <{0}>\r\n", Services.Config.ReplyEmail);
// From
strBuf += String.Format("From: \"{0}\" <{1}>\r\n"
, Services.Config.SenderName
, Services.Config.SenderEmail
);
// to
strBuf += String.Format("To: <{0}>\r\n", strEmail);
// subject
strBuf += String.Format("Subject: {0}\r\n", strSubject);
// X-Priority
strBuf += String.Format("X-Priority: Normal\r\n");
// X-MSMail-Priority
strBuf += String.Format("X-MSMail-Priority: Normal\r\n");
// Importance
strBuf += String.Format("Importance: Normal\r\n");
// X-Mailer
strBuf += String.Format("X-Mailer: BHM\r\n");
// MIME-Version
strBuf += String.Format("MIME-Version: 1.0\r\n");
// limit
strBuf += String.Format("--=====001_Dragon303406132050_=====\r\n");
// Content-Type
strBuf += String.Format("Content-Type: text/plain;\r\n");
// charset
strBuf += String.Format("charset: \"{0};\"\r\n", CHARSET);
// Content-Transfer-Encoding
strBuf += String.Format("Content-Transfer-Encoding: base64\r\n\r\n");
// body
strBuf += String.Format("{0}\r\n", Classes.Convert.StringToBase64(strBody));
// END
strBuf += String.Format("\r\n.\r\n");
SendData(ref strBuf);
System.Threading.Thread.Sleep(Services.Config.SMTPTimeout);
strBuf = String.Format("QUIT\r\n");
SendData(ref strBuf);
return true;
}
catch (System.Exception e)
{
return false;
}
finally
{
if (m_Stream != null)
m_Stream.Close();
if (m_Tcp != null)
m_Tcp.Close();
}
}
}
}
错误提示:当前上下文中不存在名称“Services”,“Classes”,在线等,急求
如果谁有个C#写的E-mail发送程序,用自己的主机当作服务器,不借助外部服务器,发一个,591811930@qq.com,多给分