110,538
社区成员
发帖
与我相关
我的任务
分享
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace EMail
{
public enum MailPriority
{
High=3,
Normal=2,
Low=1
}
public class ESMTPSendMail
{
private string _Server = string.Empty;
private int _Port = 25;
private string _UserName = string.Empty;
private string _Password = string.Empty;
private string _From = string.Empty;
private string _To = string.Empty;
private string _FormName = string.Empty;
private string _ToName = string.Empty;
private string _Subject = string.Empty;
private string _Body = string.Empty;
private bool _IsHeml = false;
private int _Priority = (int)MailPriority.High;
private List<string> _Attachments = new List<string>();
public string Server
{
get { return this._Server; }
set { this._Server = value; }
}
public int Port
{
get { return this._Port; }
set { this._Port=value; }
}
public string UserName
{
get { return this._UserName; }
set { this._UserName = value; }
}
public string Password
{
get { return this._Password; }
set { this._Password = value; }
}
public string From
{
get { return this._From; }
set { this._From = value; }
}
public string To
{
get { return this._To; }
set { this._To = value; }
}
public string FormName
{
get { return this._FormName; }
set { this._FormName = value; }
}
public string ToName
{
get { return this._ToName; }
set { this._ToName = value; }
}
public string Subject
{
get { return this._Subject; }
set { this._Subject = value; }
}
public string Body
{
get { return this._Body; }
set { this._Body = value; }
}
public bool IsHeml
{
get { return this._IsHeml; }
set { this._IsHeml = value; }
}
public int Priority
{
get { return this._Priority; }
set { this._Priority = value; }
}
public List<string> Attachments
{
get { return this._Attachments; }
set { this._Attachments = value; }
}
public ESMTPSendMail()
{
}
private void WriteString(NetworkStream networkStream, string msg)
{
if (!msg.EndsWith("\r\n")) msg += "\r\n";
byte[] commandByte = Encoding.UTF8.GetBytes(msg);
int start = 0;
int length = commandByte.Length;
int page = 0;
int size = 75;
int count = size;
try
{
if (length > 75)
{
if (length % size != 0)
{
page = length / size + 1;
}
else
{
page = length / size;
}
for (int i = 0; i < page; i++)
{
start = i * size;
if (i == page - 1)
{
count = length - i * size;
}
networkStream.Write(commandByte, start, count);
}
}
else
{
networkStream.Write(commandByte, 0, commandByte.Length);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "ERR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private string ReadString(NetworkStream networkStream)
{
string sp = string.Empty;
byte[] msgByte = new byte[1024];
int size = networkStream.Read(msgByte, 0, msgByte.Length);
if (size > 0)
{
sp = Encoding.UTF8.GetString(msgByte);
}
return sp;
}
private bool Command(NetworkStream networkStream, string command, string state)
{
string sp = string.Empty;
bool success = false;
try
{
WriteString(networkStream,command);
sp=ReadString(networkStream);
if(sp.IndexOf(state)!=-1)
{
success=true;
}
}
catch (Exception exception)
{
success=false;
}
return success;
}
private string ToBase64(string msg)
{
byte[] msgByte = Encoding.UTF8.GetBytes(msg);
msg = Convert.ToBase64String(msgByte);
return msg;
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
button1.Enabled = false;
Socket socket = this.GetSocket("smtp.sohu.com", 25);
if (socket == null)
{
this.richTextBox1.AppendText("与服务器建立失败!请检查网络\n");
}
richTextBox1.AppendText(SendMsg(socket, "ehlo l", false));
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, "auth login", false));
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, "bejon", true));
Application.DoEvents();
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, "8331789", true));
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, "mail from:bejon@sohu.com ", false));
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, "rcpt to:bejon@sohu.com", false));
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, "data", false));
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, "这是个测试 ", false));
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, ".", false));
Application.DoEvents();
richTextBox1.AppendText(SendMsg(socket, "quit", false));
Application.DoEvents();
button1.Enabled = true;
}
Socket GetSocket(string serverAddress, int port)
{
IPHostEntry hostEntry = null;
try
{
hostEntry = Dns.GetHostEntry(serverAddress);
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint endPoint = new IPEndPoint(address, port);
Socket tempSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.SendTimeout = 5000;
tempSocket.Connect(endPoint);
tempSocket.ReceiveBufferSize *= 2;
if (tempSocket.Connected)
return tempSocket;
}
return null;
}
catch
{
return null;
}
}
string SendMsg(Socket socket, string msg, bool needBase64)
{
msg += "\r\n";
byte[] buffer = Encoding.Default.GetBytes(msg);
if (needBase64)
{
string tempString = Convert.ToBase64String(buffer);
buffer = Convert.FromBase64String(tempString);
}
socket.Send(buffer);
byte[] rezultByte = new byte[socket.ReceiveBufferSize];
socket.Receive(buffer);
string rezult = Encoding.Default.GetString(buffer);
return rezult+"\r\n";
}