C#的SOCKET连接问题 请问C#中如何编写SOCKET连接,如有现成代码望给予提供,谢谢!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private bool SendEmail() { //连接网络 try { tc = new TcpClient(mailserver, mailserverport); } catch (Exception e) { errmsg = e.ToString(); return false; } ns = tc.GetStream(); SMTPCodeAdd(); //验证网络连接是否正确 if (RightCodeHT[RecvResponse().Substring(0, 3)] == null) { errmsg = "网络连接失败"; return false; } string[] SendBuffer; string SendBufferstr; //进行SMTP验证 if (ESmtp) { SendBuffer = new String[4]; SendBuffer[0] = "EHLO " + mailserver + enter; SendBuffer[1] = "AUTH LOGIN" + enter; SendBuffer[2] = Base64Encode(username) + enter; SendBuffer[3] = Base64Encode(password) + enter; if (!Dialog(SendBuffer, "SMTP服务器验证失败,请核对用户名和密码。")) return false; } else { SendBufferstr = "HELO " + mailserver + enter; if (!Dialog(SendBufferstr, "")) return false; } // SendBufferstr = "MAIL FROM:<" + From + ">" + enter; if (!Dialog(SendBufferstr, "发件人地址错误,或不能为空")) return false; // SendBuffer = new string[recipientmaxnum]; for (int i = 0; i < Recipient.Count; i++) { SendBuffer[i] = "RCPT TO:<" + Recipient[i].ToString() + ">" + enter; } if (!Dialog(SendBuffer, "收件人地址有误")) return false; SendBufferstr = "DATA" + enter; if (!Dialog(SendBufferstr, "")) return false; SendBufferstr = "From:" + FromName + "<" + From + ">" + enter; SendBufferstr += "To:=?" + Charset.ToUpper() + "?B?" + Base64Encode(RecipientName) + "?=" + "<" + Recipient[0] + ">" + enter; SendBufferstr += "CC:"; for (int i = 0; i < Recipient.Count; i++) { SendBufferstr += Recipient[i].ToString() + "<" + Recipient[i].ToString() + ">,"; } SendBufferstr += enter; if (Charset == "") { SendBufferstr += "Subject:" + Subject + enter; } else { SendBufferstr += "Subject:" + "=?" + Charset.ToUpper() + "?B?" + Base64Encode(Subject) + "?=" + enter; } SendBufferstr += "X-Priority:" + priority + enter; SendBufferstr += "X-MSMail-Priority:" + priority + enter; SendBufferstr += "Importance:" + priority + enter; SendBufferstr += "X-Mailer: Huolx.Pubclass" + enter; SendBufferstr += "MIME-Version: 1.0" + enter; SendBufferstr += "Content-Type: multipart/mixed;" + enter;//内容格式和分隔符 SendBufferstr += " boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\"" + enter; SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter; if (Html) { SendBufferstr += "Content-Type: text/html;" + enter; } else { SendBufferstr += "Content-Type: text/plain;" + enter; } if (Charset == "") { SendBufferstr += " charset=\"gb2312\"" + enter; } else { SendBufferstr += " charset=\"" + Charset.ToLower() + "\"" + enter; } // SendBufferstr += "Content-Transfer-Encoding: base64"+enter; SendBufferstr += "Content-Transfer-Encoding: base64" + enter + enter; SendBufferstr += Base64Encode(Body) + enter + enter; if (Attachments.Count != 0) { foreach (string filepath in Attachments) { SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter; SendBufferstr += "Content-Type: application/octet-stream" + enter; SendBufferstr += " name=\"=?" + Charset.ToUpper() + "?B?" + Base64Encode(filepath.Substring(filepath.LastIndexOf("\\") + 1)) + "?=\"" + enter; SendBufferstr += "Content-Transfer-Encoding: base64" + enter; SendBufferstr += "Content-Disposition: attachment;" + enter; SendBufferstr += " filename=\"=?" + Charset.ToUpper() + "?B?" + Base64Encode(filepath.Substring(filepath.LastIndexOf("\\") + 1)) + "?=\"" + enter + enter; SendBufferstr += GetStream(filepath) + enter + enter; } } SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770--" + enter + enter; // SendBufferstr += "------=_NextPart_821375_13095514.1180514691160--" + enter + enter; SendBufferstr += enter + "." + enter; if (!Dialog(SendBufferstr, "错误信件信息")) return false; SendBufferstr = "QUIT" + enter; if (!Dialog(SendBufferstr, "断开连接时错误")) return false; ns.Close(); tc.Close(); return true; } 一个异步Sockethttp://blog.csdn.net/LeoMaya/archive/2007/08/06/1728518.aspx streamwriter如何写覆盖 .net 与 java 的byte互通 C#操作SQL Server的效率问题 无法将类型为“System.String”的对象强制转换为类型“System.Byte[]”。 Microsoft Visual Studio 2005中使用multipage奇怪的问题 超级郁闷问题,解决马上给分!!! 如何画窗体的标题和边框? 诚心请有经验的朋友指导一下 关于DotNetOpenAuth 的问题 c#调用c++问题 现在学C#有前途吗,欢迎大家来探讨? 高分请教 存储过程里面的SQL语句问题 文件写入和编码问题
private bool SendEmail()
{
//连接网络
try
{
tc = new TcpClient(mailserver, mailserverport);
}
catch (Exception e)
{
errmsg = e.ToString();
return false;
} ns = tc.GetStream();
SMTPCodeAdd(); //验证网络连接是否正确
if (RightCodeHT[RecvResponse().Substring(0, 3)] == null)
{
errmsg = "网络连接失败";
return false;
}
string[] SendBuffer;
string SendBufferstr; //进行SMTP验证
if (ESmtp)
{
SendBuffer = new String[4];
SendBuffer[0] = "EHLO " + mailserver + enter;
SendBuffer[1] = "AUTH LOGIN" + enter;
SendBuffer[2] = Base64Encode(username) + enter;
SendBuffer[3] = Base64Encode(password) + enter;
if (!Dialog(SendBuffer, "SMTP服务器验证失败,请核对用户名和密码。"))
return false;
}
else
{
SendBufferstr = "HELO " + mailserver + enter;
if (!Dialog(SendBufferstr, ""))
return false;
} //
SendBufferstr = "MAIL FROM:<" + From + ">" + enter;
if (!Dialog(SendBufferstr, "发件人地址错误,或不能为空"))
return false; //
SendBuffer = new string[recipientmaxnum];
for (int i = 0; i < Recipient.Count; i++)
{ SendBuffer[i] = "RCPT TO:<" + Recipient[i].ToString() + ">" + enter; }
if (!Dialog(SendBuffer, "收件人地址有误"))
return false; SendBufferstr = "DATA" + enter;
if (!Dialog(SendBufferstr, ""))
return false; SendBufferstr = "From:" + FromName + "<" + From + ">" + enter; SendBufferstr += "To:=?" + Charset.ToUpper() + "?B?" + Base64Encode(RecipientName) + "?=" + "<" + Recipient[0] + ">" + enter;
SendBufferstr += "CC:";
for (int i = 0; i < Recipient.Count; i++)
{
SendBufferstr += Recipient[i].ToString() + "<" + Recipient[i].ToString() + ">,";
}
SendBufferstr += enter;
if (Charset == "")
{
SendBufferstr += "Subject:" + Subject + enter;
}
else
{
SendBufferstr += "Subject:" + "=?" + Charset.ToUpper() + "?B?" + Base64Encode(Subject) + "?=" + enter;
} SendBufferstr += "X-Priority:" + priority + enter;
SendBufferstr += "X-MSMail-Priority:" + priority + enter;
SendBufferstr += "Importance:" + priority + enter;
SendBufferstr += "X-Mailer: Huolx.Pubclass" + enter;
SendBufferstr += "MIME-Version: 1.0" + enter; SendBufferstr += "Content-Type: multipart/mixed;" + enter;//内容格式和分隔符
SendBufferstr += " boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\"" + enter;
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter; if (Html)
{
SendBufferstr += "Content-Type: text/html;" + enter;
}
else
{
SendBufferstr += "Content-Type: text/plain;" + enter;
} if (Charset == "")
{
SendBufferstr += " charset=\"gb2312\"" + enter;
}
else
{
SendBufferstr += " charset=\"" + Charset.ToLower() + "\"" + enter;
}
// SendBufferstr += "Content-Transfer-Encoding: base64"+enter;
SendBufferstr += "Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr += Base64Encode(Body) + enter + enter;
if (Attachments.Count != 0)
{
foreach (string filepath in Attachments)
{
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter;
SendBufferstr += "Content-Type: application/octet-stream" + enter;
SendBufferstr += " name=\"=?" + Charset.ToUpper() + "?B?" + Base64Encode(filepath.Substring(filepath.LastIndexOf("\\") + 1)) + "?=\"" + enter;
SendBufferstr += "Content-Transfer-Encoding: base64" + enter;
SendBufferstr += "Content-Disposition: attachment;" + enter;
SendBufferstr += " filename=\"=?" + Charset.ToUpper() + "?B?" + Base64Encode(filepath.Substring(filepath.LastIndexOf("\\") + 1)) + "?=\"" + enter + enter;
SendBufferstr += GetStream(filepath) + enter + enter;
}
}
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770--" + enter + enter;
// SendBufferstr += "------=_NextPart_821375_13095514.1180514691160--" + enter + enter;
SendBufferstr += enter + "." + enter; if (!Dialog(SendBufferstr, "错误信件信息"))
return false;
SendBufferstr = "QUIT" + enter;
if (!Dialog(SendBufferstr, "断开连接时错误"))
return false;
ns.Close();
tc.Close();
return true;
}