· 8 years ago · May 01, 2018, 08:06 AM
1TcpClient tcpclient = new TcpClient();
2
3 // HOST NAME POP SERVER and gmail uses port number 995 for POP
4
5 //tcpclient.Connect("pop.gmail.com", 995);
6 tcpclient.Connect("smtp.gmail.com", 465);
7 // This is Secure Stream // opened the connection between client and POP Server
8 System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
9 // authenticate as client
10 //sslstream.AuthenticateAsClient("pop.gmail.com");
11 sslstream.AuthenticateAsClient("smtp.gmail.com");
12 //bool flag = sslstream.IsAuthenticated; // check flag
13 // Asssigned the writer to stream
14 System.IO.StreamWriter sw = new StreamWriter(sslstream);
15 // Assigned reader to stream
16 System.IO.StreamReader reader = new StreamReader(sslstream);
17 // refer POP rfc command, there very few around 6-9 command
18 sw.WriteLine("EHLO " + "smtp.gmail.com");
19 sw.Flush();
20 sw.WriteLine("AUTH LOGIN/r/n");
21 sw.Flush();
22 sw.WriteLine("******@gmail.com/r/n");
23 sw.Flush();
24 // sent to server
25 sw.WriteLine("***********/r/n");
26 sw.Flush();
27 //// this will retrive your first email
28 //sw.WriteLine("RETR 1");
29 //sw.Flush();
30 //// close the connection
31 //sw.WriteLine("Quit ");
32 //sw.Flush();
33 sw.WriteLine("MAIL FROM:<" + "******@gmail.com" + ">rn");
34 sw.Flush();
35 sw.WriteLine("RCPT TO:<" + "*******@***.com" + ">rn");
36 sw.Flush();
37 sw.WriteLine("DATArn");
38 sw.Flush();
39 sw.WriteLine("Subject: Email testrn");
40 sw.Flush();
41 sw.WriteLine("Test 1 2 3rn");
42 sw.Flush();
43 sw.WriteLine(".rn");
44 sw.Flush();
45 sw.WriteLine("QUITrn");
46 sw.Flush();
47
48 string str = string.Empty;
49 string strTemp = string.Empty;
50 while ((strTemp = reader.ReadLine()) != null)
51 {
52 // find the . character in line
53 if (strTemp == ".")
54 {
55 break;
56 }
57 if (strTemp.IndexOf("-ERR") != -1)
58 {
59 break;
60 }
61 str += strTemp;
62 }
63 }
64
65"250-smtp.gmail.com at your service, [151.238.124.27]rn250-SIZE 35882577rn250-8BITMIMErn250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTHrn250-ENHANCEDSTATUSCODESrn250-PIPELININGrn250-CHUNKINGrn250 SMTPUTF8rn451 4.5.0 SMTP protocol violation, see RFC 2821 x17-v6sm5346253edx.53 - gsmtprn"