您的位置:首页 > 其它

Socket收发邮件

2009-07-17 11:34 190 查看
Socket收邮件

1/**//// <summary>
2 /// 发送邮件类
3 /// </summary>
4 public class SMTP
5 {
6 public SMTP(){}
7
8 public bool Send(string strSmtpServer,int nPort,string strSend,string strReceive
9 ,string strSubject,string strContent)
10 {
11 /**//// smtp服务器的IP地址  
12 string smtpserver=strSmtpServer;
13 TcpClient tcpc = new TcpClient();
14 try
15 {
16 tcpc.Connect(smtpserver, nPort);
17 StreamReader sr ;
18 string strCmd;
19 sr = new StreamReader(tcpc.GetStream(),Encoding.Default);
20
21 /**////服务器连接成功以后,首先向服务器发送HeLlo命令
22 strCmd="HELO shaozhd";
23 SenSmtpCmd(tcpc,strCmd);
24
25 /**////然后向服务器发送信件的成员的信箱
26 strCmd="mail from:"+ strSend;
27 SenSmtpCmd(tcpc,strCmd);
28
29 /**////向服务器发送收件人的信箱
30 strCmd="rcpt to:" + strReceive;
31 SenSmtpCmd(tcpc,strCmd);
32
33 /**////所有的准备工作都已经作好了,下面开始进行邮件的部分
34 strCmd="data";
35 SenSmtpCmd(tcpc,strCmd);
36
37 /**////邮件内容
38 strCmd="Date: 1234567\r\n";
39 strCmd=strCmd+"From: " + strSend +"\r\n";
40 strCmd=strCmd+"To: " + strReceive +"\r\n";
41 strCmd=strCmd+"Subject: " + strSubject +"\r\n\r\n";
42 strCmd=strCmd + strContent +"\r\n\r\n";
43 SenSmtpCmd(tcpc,strCmd);
44 strCmd="\r\n.\r\n";
45 SenSmtpCmd(tcpc,strCmd);
46
47 /**////最后 关闭与smtp 服务器的连接
48       tcpc.Close();
49 return true;
50  }
51 catch
52 {
53 return false;
54 }
55 }
56
57 /**//// <summary>
58 /// 发送SMTP命令
59 /// </summary>
60 /// <param name="tcpc"></param>
61 /// <param name="strCmd"></param>
62 void SenSmtpCmd(TcpClient tcpc,String strCmd)
63 {
64
65      byte[] arrCmd;
66   string strRet;
67   StreamReader sr;
68     Stream s;
69     s=tcpc.GetStream();
70     strCmd = strCmd + "\r\n";
71     arrCmd= Encoding.Default.GetBytes(strCmd.ToCharArray());
72     s=tcpc.GetStream();
73     s.Write(arrCmd, 0, strCmd.Length);
74
75 /**////以下用于程序调试,显示服务器回应信息
76     sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
77     strRet=sr.ReadLine();
78     return;
79 }
80 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: