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,多给分
11 个解决方案
#1
网上很多的
#2
没有引用Classes,Services。
#4
private static void sendMail(string name, string password)
{
string smtpServer = "smtp.gmail.com";//服务器
int smtpPort = 587;//端口
string userAccount = "";//用户名
string userPassword = "";//密码
string userName = "";//发件人名称
string EmailAddress = "";//发件邮箱
string strto = "";//目的邮箱
string strSubject = "";//主题
string strBody = "";//内容
System.Net.Mail.SmtpClient client = new SmtpClient(smtpServer, smtpPort);
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential(userAccount, userPassword);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailAddress fromEmal = new MailAddress(EmailAddress, userName);
MailAddress toEmail = new MailAddress(strto);
System.Net.Mail.MailMessage message = new MailMessage(fromEmal, toEmail);
message.Subject = strSubject;
message.Body = strBody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
#5
private void button1_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com";
client.Port = 25;
NetworkCredential nc = new NetworkCredential();
nc.UserName = "保密";//mail.163.com上的帐号
nc.Password = "保密";//密码
client.Credentials = nc;
MailMessage msg = new MailMessage();
msg.Subject = "welcome";
msg.Body = "test ok";
msg.From = new MailAddress("yangglemu@163.com");
msg.To.Add(new MailAddress("591811930@qq.com"));
client.Send(msg);
}
看看你邮箱满了没有,我这不让发了
#6
哪位高手给发个,不盛感激
#7
帮你顶
#8
mark
#9
第一种办法是在你自己机器上装一个smtp服务器软件,然后用System.Net.Mail或jmail等通过你自己的smtp服务器发信。免费的smtp server和c#邮件发送程序都很多了,就不需要发给你了吧。
第二种办法是通过DNS查找邮件地址的MX记录,直接将邮件发到目标接收方(相当于实现了最简单的smtp服务器)。这类第三方组件也不少,下面这两个都可以用,文档也比较全,缺点是都是商用的license
http://www.emailarchitect.net/easendmail/
http://www.rebex.net/mail.net/features-smtp.aspx
顺便提下,如果你要用自己的电脑发邮件,最好你的电脑要有固定外网地址,并且要申请一个域名,将MX记录指向你的机器,发信时用这个域名作为发件人地址。因为现在一般的邮件服务器都有用域名反向查找来核对发件人地址,如果不这样做,是很难突破大多数邮件服务商的反垃圾邮件安全措施的。
第二种办法是通过DNS查找邮件地址的MX记录,直接将邮件发到目标接收方(相当于实现了最简单的smtp服务器)。这类第三方组件也不少,下面这两个都可以用,文档也比较全,缺点是都是商用的license
http://www.emailarchitect.net/easendmail/
http://www.rebex.net/mail.net/features-smtp.aspx
顺便提下,如果你要用自己的电脑发邮件,最好你的电脑要有固定外网地址,并且要申请一个域名,将MX记录指向你的机器,发信时用这个域名作为发件人地址。因为现在一般的邮件服务器都有用域名反向查找来核对发件人地址,如果不这样做,是很难突破大多数邮件服务商的反垃圾邮件安全措施的。
#10
对象没有引用
public void MailSend(string MailFrom,string MailTo,string MailPwd,string Mailtitle,string MailCon)
{
MailMessage MyMail = new MailMessage();
MyMail.From = new MailAddress(MailFrom + "@163.com", "");
MyMail.To.Add(new MailAddress(MailTo + "@163.com"));
MyMail.Subject = Mailtitle;
MyMail.Body = MailCon;
MyMail.IsBodyHtml = false;
SmtpClient smtpclient = new SmtpClient();
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Host = "smtp.163.com";
smtpclient.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
smtpclient.Send(MyMail);
}
jmail.Message Jmail = new jmail.Message();
DateTime t = DateTime.Now;
String Subject = "";
String body = "";
String FromEmail = "";
String ToEmail = "";
Jmail.Charset = "GB2312";
Jmail.ContentType = "text/html";
Jmail.AddRecipient(ToEmail, "", "");
Jmail.From = FromEmail;
Jmail.MailServerUserName = "";
Jmail.MailServerPassWord = "";
Jmail.Subject = Subject;
Jmail.ContentType="text/html";
Jmail.Body = body + t.ToString();
Jmail.Send("smtp.163.com", false);
Jmail.Close();
public void MailSend(string MailFrom,string MailTo,string MailPwd,string Mailtitle,string MailCon)
{
MailMessage MyMail = new MailMessage();
MyMail.From = new MailAddress(MailFrom + "@163.com", "");
MyMail.To.Add(new MailAddress(MailTo + "@163.com"));
MyMail.Subject = Mailtitle;
MyMail.Body = MailCon;
MyMail.IsBodyHtml = false;
SmtpClient smtpclient = new SmtpClient();
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Host = "smtp.163.com";
smtpclient.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
smtpclient.Send(MyMail);
}
jmail.Message Jmail = new jmail.Message();
DateTime t = DateTime.Now;
String Subject = "";
String body = "";
String FromEmail = "";
String ToEmail = "";
Jmail.Charset = "GB2312";
Jmail.ContentType = "text/html";
Jmail.AddRecipient(ToEmail, "", "");
Jmail.From = FromEmail;
Jmail.MailServerUserName = "";
Jmail.MailServerPassWord = "";
Jmail.Subject = Subject;
Jmail.ContentType="text/html";
Jmail.Body = body + t.ToString();
Jmail.Send("smtp.163.com", false);
Jmail.Close();
#11
标记,学习
#1
网上很多的
#2
没有引用Classes,Services。
#3
EMAIL发送系统(C#+基于SMTP认证) 2.0
http://www.host01.com/article/Net/00020007/0561316505792359.htm
http://www.host01.com/article/Net/00020007/0561316505792359.htm
#4
private static void sendMail(string name, string password)
{
string smtpServer = "smtp.gmail.com";//服务器
int smtpPort = 587;//端口
string userAccount = "";//用户名
string userPassword = "";//密码
string userName = "";//发件人名称
string EmailAddress = "";//发件邮箱
string strto = "";//目的邮箱
string strSubject = "";//主题
string strBody = "";//内容
System.Net.Mail.SmtpClient client = new SmtpClient(smtpServer, smtpPort);
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential(userAccount, userPassword);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailAddress fromEmal = new MailAddress(EmailAddress, userName);
MailAddress toEmail = new MailAddress(strto);
System.Net.Mail.MailMessage message = new MailMessage(fromEmal, toEmail);
message.Subject = strSubject;
message.Body = strBody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
#5
private void button1_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com";
client.Port = 25;
NetworkCredential nc = new NetworkCredential();
nc.UserName = "保密";//mail.163.com上的帐号
nc.Password = "保密";//密码
client.Credentials = nc;
MailMessage msg = new MailMessage();
msg.Subject = "welcome";
msg.Body = "test ok";
msg.From = new MailAddress("yangglemu@163.com");
msg.To.Add(new MailAddress("591811930@qq.com"));
client.Send(msg);
}
看看你邮箱满了没有,我这不让发了
#6
哪位高手给发个,不盛感激
#7
帮你顶
#8
mark
#9
第一种办法是在你自己机器上装一个smtp服务器软件,然后用System.Net.Mail或jmail等通过你自己的smtp服务器发信。免费的smtp server和c#邮件发送程序都很多了,就不需要发给你了吧。
第二种办法是通过DNS查找邮件地址的MX记录,直接将邮件发到目标接收方(相当于实现了最简单的smtp服务器)。这类第三方组件也不少,下面这两个都可以用,文档也比较全,缺点是都是商用的license
http://www.emailarchitect.net/easendmail/
http://www.rebex.net/mail.net/features-smtp.aspx
顺便提下,如果你要用自己的电脑发邮件,最好你的电脑要有固定外网地址,并且要申请一个域名,将MX记录指向你的机器,发信时用这个域名作为发件人地址。因为现在一般的邮件服务器都有用域名反向查找来核对发件人地址,如果不这样做,是很难突破大多数邮件服务商的反垃圾邮件安全措施的。
第二种办法是通过DNS查找邮件地址的MX记录,直接将邮件发到目标接收方(相当于实现了最简单的smtp服务器)。这类第三方组件也不少,下面这两个都可以用,文档也比较全,缺点是都是商用的license
http://www.emailarchitect.net/easendmail/
http://www.rebex.net/mail.net/features-smtp.aspx
顺便提下,如果你要用自己的电脑发邮件,最好你的电脑要有固定外网地址,并且要申请一个域名,将MX记录指向你的机器,发信时用这个域名作为发件人地址。因为现在一般的邮件服务器都有用域名反向查找来核对发件人地址,如果不这样做,是很难突破大多数邮件服务商的反垃圾邮件安全措施的。
#10
对象没有引用
public void MailSend(string MailFrom,string MailTo,string MailPwd,string Mailtitle,string MailCon)
{
MailMessage MyMail = new MailMessage();
MyMail.From = new MailAddress(MailFrom + "@163.com", "");
MyMail.To.Add(new MailAddress(MailTo + "@163.com"));
MyMail.Subject = Mailtitle;
MyMail.Body = MailCon;
MyMail.IsBodyHtml = false;
SmtpClient smtpclient = new SmtpClient();
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Host = "smtp.163.com";
smtpclient.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
smtpclient.Send(MyMail);
}
jmail.Message Jmail = new jmail.Message();
DateTime t = DateTime.Now;
String Subject = "";
String body = "";
String FromEmail = "";
String ToEmail = "";
Jmail.Charset = "GB2312";
Jmail.ContentType = "text/html";
Jmail.AddRecipient(ToEmail, "", "");
Jmail.From = FromEmail;
Jmail.MailServerUserName = "";
Jmail.MailServerPassWord = "";
Jmail.Subject = Subject;
Jmail.ContentType="text/html";
Jmail.Body = body + t.ToString();
Jmail.Send("smtp.163.com", false);
Jmail.Close();
public void MailSend(string MailFrom,string MailTo,string MailPwd,string Mailtitle,string MailCon)
{
MailMessage MyMail = new MailMessage();
MyMail.From = new MailAddress(MailFrom + "@163.com", "");
MyMail.To.Add(new MailAddress(MailTo + "@163.com"));
MyMail.Subject = Mailtitle;
MyMail.Body = MailCon;
MyMail.IsBodyHtml = false;
SmtpClient smtpclient = new SmtpClient();
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Host = "smtp.163.com";
smtpclient.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
smtpclient.Send(MyMail);
}
jmail.Message Jmail = new jmail.Message();
DateTime t = DateTime.Now;
String Subject = "";
String body = "";
String FromEmail = "";
String ToEmail = "";
Jmail.Charset = "GB2312";
Jmail.ContentType = "text/html";
Jmail.AddRecipient(ToEmail, "", "");
Jmail.From = FromEmail;
Jmail.MailServerUserName = "";
Jmail.MailServerPassWord = "";
Jmail.Subject = Subject;
Jmail.ContentType="text/html";
Jmail.Body = body + t.ToString();
Jmail.Send("smtp.163.com", false);
Jmail.Close();
#11
标记,学习