socket发信

阿牛138588 2008-10-15 10:23:48
发信出错了。
我用的是sohu的信箱(用outlook收发正常),发现一个很奇怪的现象,用户名及密码随便填,sohu好像不进行验证。
然后到填发信地址时出错,不知道哪错了

如果谁有能用的socket写的Email收发程序欢迎交流。满分送上


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;
}


下续..
...全文
315 45 打赏 收藏 转发到动态 举报
写回复
用AI写文章
45 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿牛138588 2009-02-06
  • 打赏
  • 举报
回复
再顶吧,唉
pvgyetg 2008-10-27
  • 打赏
  • 举报
回复
学习中,UP
阿牛138588 2008-10-26
  • 打赏
  • 举报
回复
再顶一下咯
阿牛138588 2008-10-25
  • 打赏
  • 举报
回复
没人来了吗?
slimfeng 2008-10-25
  • 打赏
  • 举报
回复
顶一下
阿牛138588 2008-10-22
  • 打赏
  • 举报
回复
      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";
}

返回:

220 smtp
.sohu.com ES
MTP Pos
tfix
9

250-smtp.sohu.com
250-PIPE
LINING
250-SIZE 1024000
0
250
-VRFY
250-ET
RN
250-A

这是什么意思,汗!
gxlqssjf 2008-10-21
  • 打赏
  • 举报
回复
调试一下,看看哪里出错了,不可能不进行相关操作的
htjuw 2008-10-21
  • 打赏
  • 举报
回复
http://www.cndw.com/tech/net/200602153995.asp
Optione 2008-10-20
  • 打赏
  • 举报
回复
mark
Optione 2008-10-20
  • 打赏
  • 举报
回复
mark
htjuw 2008-10-20
  • 打赏
  • 举报
回复
哈哈,和我的需求一样啊啊

http://topic.csdn.net/u/20081018/16/9e3636e3-d41e-408d-98d9-471b4b56cae9.html

你要是解决了,我一并再给你200分!
heguo 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bejon 的回复:]
不喜欢用现成的
[/Quote]

就喜欢用现成的.
tianjinldl 2008-10-20
  • 打赏
  • 举报
回复
代码太长了,帮顶
guest78978 2008-10-20
  • 打赏
  • 举报
回复
学习,帮顶
XPingguo 2008-10-20
  • 打赏
  • 举报
回复
关注下
试了楼主的代码,总是提示密码出错!
ximi82878 2008-10-20
  • 打赏
  • 举报
回复
什么情况,顶完再看
netddayup 2008-10-20
  • 打赏
  • 举报
回复
问题解决了么?
阿牛138588 2008-10-20
  • 打赏
  • 举报
回复
后来重新自己用socket写了
估计是发送的命令不对,但依照网上的esmpt协议发送命令,没正确加重复
b3727180 2008-10-20
  • 打赏
  • 举报
回复
学习
悔说话的哑巴 2008-10-20
  • 打赏
  • 举报
回复
学习中!
加载更多回复(25)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧