![]() |
#2
FenteLi2007-11-25 22:52
这个程序是我自己写的,是通过Soket进行连接的,由于时间仓促所以只写了发送邮件;源程序如下,在这里我只转载了源代码:
using System; using System.Collections.Generic; using System.Text; using using using namespace FTPTest { class MailNotice { //////////////////////////////////////////////////////////////////////////////////////////// //类名: SendMail //功能: 主要实现了邮件的发送功能,是通过SMTP协议实现的 //基本需求: 需要IO,Net,Net.Sockets命名空间的支持; //说明: 邮件发送的内容是固定的,硬编码在了代码中,可根据需要自动修改 //来源: [url]http://www.[/url] 学习.NET的殿堂 //////////////////////////////////////////////////////////////////////////////////////////// private IPAddress IPAdd; private IPEndPoint IPep; private TcpClient client; private NetworkStream stream; public MailNotice() { IPAdd = Dns.GetHostEntry("smtp.).AddressList[0]; IPep = new IPEndPoint(IPAdd, 25); client = new TcpClient(); try { client.Connect(IPep); } catch (Exception ep) { throw new IOException("邮件服务器连接失败:" + ep.Message); } stream = client.GetStream(); } ~MailNotice() { if (client.Connected == true) { client.Close(); } } //发送命令 private void SendCommand(string Command) { if (stream.CanWrite) { Byte[] SendByte = Encoding.UTF8.GetBytes(Command); try { stream.Write(SendByte, 0, SendByte.Length); } catch (Exception exp) { throw new IOException("发送" + Command + "命令出现异常:" + exp.Message); } } else { throw new IOException("邮件命令发送失败!请检查网络连接。"); } } //接受数据 private string ReceiveEcho() { if (stream.CanRead) { Byte[] GetBytes = new Byte[client.ReceiveBufferSize]; try { stream.Read(GetBytes, 0, (int)client.ReceiveBufferSize); } catch (Exception exp) { throw new IOException("接受数据的时候出现了异常:" + exp.Message); } string Echo = Encoding.UTF8.GetString(GetBytes); return Echo; } else { throw new IOException("接受数据失败!请检查网络连接。"); } } //下面的方法将ascii转换成base64编码 private string AsciiToBase64(string AsciiMsg) { byte[] AsciiBytes = new byte[512]; byte[] Base64Bytes = new byte[512]; string AsciiMsg1 = null; string Base64Msg = null; switch (AsciiMsg.Length % 3) { case 1: { AsciiMsg1 = AsciiMsg + "\0\0"; break; } case 2: { AsciiMsg1 = AsciiMsg + "\0"; break; } default: { AsciiMsg1 = AsciiMsg; break; } } for (int i = 0; i < AsciiMsg1.Length / 3; i++) { string AsciiMsg2 = AsciiMsg1.Substring(i * 3, 3); AsciiBytes = System.Text.Encoding.ASCII.GetBytes(AsciiMsg2); System.Security.Cryptography.ToBase64Transform transformer = new System.Security.Cryptography.ToBase64Transform(); transformer.TransformBlock(AsciiBytes, 0, AsciiBytes.Length, Base64Bytes, 0); string str = System.Text.Encoding.ASCII.GetString(Base64Bytes, 0, Base64Bytes.Length); str = str.Substring(0, 4); Base64Msg += str; } return Base64Msg; } //下面函数实现了,邮件的发送。 public void Send() { string EmailBody = "";//这里是邮件的内容 EmailBody += "Hello All,\r\n"; EmailBody += "http://www.这个是我的主页啊,请多多光顾哦!\r\n"; string User = "test";//登陆SMTP服务器用户名,比如你自己的邮箱是[email]test@[/email] 密码是test string Pass = "test";//登陆密码 string From = "381616212@发件人地址 string [] To=new string[2]; To[0]="381616212@如果有多个收件人,可以这样写 //To[1] = "dan@收件人地址 //To[2] = "andy.speed@ //To[3] = "leopold.li@ //To[1] = "melinda.xia@ //To[5] = "leesli@ string Subject = "FAILURE NOTICE";//邮件的主题 string Data = EmailBody;//将当前时间作为邮件的正文 string strCommand = ""; string Response = ""; Response = ReceiveEcho(); //第一次返回信息中如果包含了220就说明服务器连接成功,就可以向服务器发送标识 if (Response.IndexOf("220") < 0) { throw new IOException("连接服务器失败:" + Response); } strCommand = "HELO " + User + "\r\n"; SendCommand(strCommand); //发送标识成功返回信息中就会包含250,发送验证命令 Response = ""; Response = ReceiveEcho(); if (Response.IndexOf("250") < 0) { throw new IOException("命令" + strCommand + "发送失败!"); } strCommand = "AUTH LOGIN\r\n"; SendCommand(strCommand); //返回信息包含334说明验证命令发送成功,发用户名 Response = ""; Response = ReceiveEcho(); if (Response.IndexOf("334") < 0) { throw new IOException("命令" + strCommand + "发送失败!"); } strCommand = AsciiToBase64(User) + "\r\n";//用户名和密码必须以base64编码发送 SendCommand(strCommand); //返回信息包含334说明用户名验证成功,发密码 Response = ""; Response = ReceiveEcho(); if (Response.IndexOf("334") < 0) { throw new IOException("用户名" + strCommand + "验证失败!"); } strCommand = AsciiToBase64(Pass) + "\r\n";//用户名和密码必须以base64编码发送 SendCommand(strCommand); //返回信息包含235说明密码验证成功,发发件地址 Response = ""; Response = ReceiveEcho(); if (Response.IndexOf("235") < 0) { throw new IOException("密码" + strCommand + "验证失败!"); } strCommand = "MAIL FROM:<" + From + ">\r\n"; SendCommand(strCommand); //返回信息包含250说明mail命令发送成功,发rcpt Response = ""; Response = ReceiveEcho(); if (Response.IndexOf("250") < 0) { throw new IOException("命令" + strCommand + "发送失败!"); } for (int i = 0; i < 1; i++) { strCommand = "RCPT TO:<" + To[i] + ">\r\n"; SendCommand(strCommand); //返回信息包含250说明rcpt to命令发送成功,发data Response = ""; Response = ReceiveEcho(); if (Response.IndexOf("250") < 0) { throw new IOException("命令" + strCommand + "发送失败!"); } } strCommand = "DATA\r\n"; SendCommand(strCommand); //返回信息包含354说明Data命令发送成功,发数据 Response = ""; Response = ReceiveEcho(); if (Response.IndexOf("354") < 0) { throw new IOException("命令" + strCommand + "发送失败!"); } strCommand = "Subject:" + Subject + "\r\n"; strCommand += "To:" + To + "\r\n"; strCommand += "Cc:\r\n\r\n" + Data + "\r\n\r\n.\r\n"; SendCommand(strCommand); //返回信息包含250说明邮件发送成功 Response = ""; Response = ReceiveEcho(); if (Response.IndexOf("250") < 0) { throw new IOException("命令" + strCommand + "发送失败!"); } Console.WriteLine("恭喜!邮件发送成功!"); } } } |
如题目,从网上找了一些,都感觉不太好用,
请问谁有.NET的源码啊?
小弟先在这谢谢大家了~~