私は、tcp 接続を開き、tcp 経由でリモートの gmail サーバーにメッセージを送信できる次のコードを作成します。
TextWriter writerlog = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "service.txt");
writerlog.WriteLine("Initializing service");
TcpClient tcpclient = new TcpClient(); // create an instance of TcpClient
try
{
writerlog.WriteLine("Starting Tcp Client");
writerlog.WriteLine("Connecting Tcp Client");
tcpclient.Connect("pop.gmail.com", 995); // HOST NAME POP SERVER and gmail uses port number 995 for POP
writerlog.WriteLine("Getting stream from client");
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()); // This is Secure Stream // opened the connection between client and POP Server
sslstream.AuthenticateAsClient("pop.gmail.com"); // authenticate as client
writerlog.WriteLine("Creating stream writer and stream reader");
StreamWriter sw = new StreamWriter(sslstream); // Asssigned the writer to stream
StreamReader reader = new StreamReader(sslstream); // Assigned reader to stream
writerlog.WriteLine("Init Authentication");
writerlog.WriteLine("Sending user");
SendCommand("USER xxxxxxxx@gmail.com", sw, reader); // refer POP rfc command, there very few around 6-9 command
writerlog.WriteLine("Sending password");
SendCommand("PASS yyyyyyyyy", sw, reader); // Sending password
writerlog.WriteLine("Retrieving last email");
writerlog.WriteLine("Email contents:" + SendCommand("RETR 1", sw, reader)); // this will retrive your first email
writerlog.WriteLine("Closing connection");
SendCommand("Quit", sw, reader);// close the connection
}
catch (Exception ex)
{
writerlog.WriteLine("Exception: " + ex.Message);
writerlog.WriteLine("StackTrace: " + ex.StackTrace);
writerlog.WriteLine("InnerExceptiion: " + ex.StackTrace);
}
finally
{
writerlog.WriteLine("Ending service");
tcpclient.Close();
writerlog.Close();
}
TCP メッセージを送信する方法:
protected string SendCommand(string cmdtext, StreamWriter writer, StreamReader reader)
{
writer.WriteLine(cmdtext);
writer.Flush();
return reader.ReadLine();
}
次の Pop3 コマンドのリストがあり、この行を使用して電子メールの内容を取得するために RETR を使用しました。
writerlog.WriteLine("メール内容:" + SendCommand("RETR 1", sw, reader));
しかし、私のログファイルでは次のようになっています:
メール内容:+OK送信PASS
USER - Takes one argument i.e., the email address of the user trying to connect to his/her mailbox. Example usage:
USER youremail@xyz.com
PASS - Takes one argument i.e., the password of the user. Example usage:
PASS yourpassword
STAT - Returns the number of emails in the mailbox and the number of bytes all the emails are taking on the server. Example usage:
STAT
TOP - Takes two arguments i.e., the sort number of the email on the server and the number of lines of text to retrieve from the body of the email. Example usage:
TOP 1 10
RETR - Takes one argument i.e., the sort number of the email on the server and returns all the headers and lines from the body of the email. Example usage:
RETR 1
DELE - Takes one argument i.e., the sort number of the email on the server and deletes it. Example usage:
DELE 1
RSET - Resets any DELE commands given above. The emails marked to be deleted by DELE command are unmarked. Example usage:
RSET
QUIT - Closes the user session with the server. Example usage:
QUIT
メールの内容を取得するために不足しているものを教えていただけますか?
前もって感謝します。
敬具。
ホセ。