2014년 1월 22일 수요일

[강좌#10]C#을 이용하여 POP3(메일읽기) 구현하기[오라클/자바/빅데이터/SQL/튜닝/안드로이드/아이폰/닷넷/C#/WPF/BigData/하둡/JAVA/Spring Framework/JSP/JDBC/ASP.NET/교육/강좌]

[강좌#10]C#을 이용하여 POP3(메일읽기) 구현하기[오라클/자바/빅데이터/SQL/튜닝/안드로이드/아이폰/닷넷/C#/WPF/BigData/하둡/JAVA/Spring Framework/JSP/JDBC/ASP.NET/교육/강좌]

POP3 구현 하기

우리는 POP3 를 이용하여 메일 서버로부터 메일을 가져 올 수 있습니다 . POP3 환경에서 메일은 메일 서버에 존재 하며 일반적인 클라이언트 프로그램은 메일 서버로 접속 한 다음 메일 메시지를 서버에서 클라이언트로 복사 합니다 . 일반적으로 클라이언트가 POP3 서버의 TCP 110 번 포트로 연결 되면서 POP 세션이 이루어 지면서 클라이언트가 서버에 접속을 성공 하면 POP3 서버는 접속 축하 메시지를 돌려 준다 . 그러면 다음 명령어를 수행 할 수 있으며 클라이언트와 서버가 서로 응답을 주고 받은 다음 연결이 종료 되면서 세션도 끝나게 되는 것이다 .

----------
POP 명령
----------
일반적인 명령어 규약은 다음과 같습니다 .

명령어는 CRLF 시퀀스로 종료 된다 .
키워드는 공백 문자로 구분 된다 .
키워드는 3~4 글자로 이루어져 있다 .
응답은 최대 512 자까지 된다 .
“+OK” 는 긍정적인 응답을 가리 킨다 .
“-ERR” 은 부정적인 응답이나 오류가 발생 한 경우를 나타낸다 .


필수명령
설명
USER [name] 서버에 접속하는 사용자 명
PASS [password[ 서버에 접속하는 사용자의 비밀번호
QUIT 현재 세션의 종료
DELE [msg] 서버에서 메일을 삭제
RSET 현재 세션의 모든 변경 사항을 취소
STAT 서버에 존재하는 메시지의 개수를 돌려 준다 .
RETR [msg] 메시지의 컨텐츠를 가져 온다
LIST [msg] 파라미터로 넘어 오는 메시지에 대한 정보를 돌려 준다 . 예를들면 크기를 바이트 단위로 돌려준다 . 파라미터가 없다면 모든 메시지의 목록과 그 크기를 돌려 준다 .
NOOP
 
서버와 긍적적인 응답을 주고 받는 것 이외에 아무런 작업도 수행하지 않는다 .
TOP [msg] [n] 서버는 메시지의 헤더와 본문을 구분해 주는 빈 줄 , 그리고 메시지의 본문이 몇 개의 행으로 이루어 졌는지 알려 준다 . [msg] 에서 원하는 메시지의 번호를 지정하며 [n] 에서 가져오려는 메시지의 상위 행의 개수를 지정 한다 .
UIDL [msg] 인자가 주어 졌으면 서버는 지정된 메시지에 대한 정보를 담고 있는 행을 긍정적인 응답과 함께 표시 한다 . 이를 선택된 메시지에 대한 “ 고유 id” 목록이라 한다 . 메시지의 고유 id 는 서버에서 독자적으로 지정하는 문자열로 0x21 로부터 0x7e 까지 문자들 중의 하나로 이루어 진다 . 이는 메시지를 고유 하게 식별하기 위해 사용 한다 .


소스 분석 하기

• 아래는 POP3 메일 메시지의 핵심을 나타내는 클래스 이다 . POP3EmailMessage 라는 새로운 클래스 이다 .
public class POP3EmailMessage {
public long msgNumber; // 고유 메시지 번호
public long msgSize; // 메시지의 크기를 Byte 단위로
public bool msgReceived; // 메시지를 수신 했는지
public string msgContent; // 실제 내용을 담고 있는 문자열
}


• System.Net.Sockets.TcpClient 에서 파생된 클래스를 생성 한다 .
Public class POP3 : System.Net.Sockets.TcpClient {
• 서버에 연결 하기 위해서는 몇 가지 작업이 수행되어야 하는데 POP3 서버에 연결 한다음 사용자 명과 비밀번호를 넘겨 주어야 한다 .
아래의 ConnectPOP() 이라는 메소드가 그러한 역할을 하는 메소드 이다 .
public void ConnectPOP(string sServerName, string sUserName, string sPassWord) {
// 메시지와 서버의 결과 응답
string sMessage;
string sResult;

//TcpClient 의 Connect() 메소드 호출 , POP3 기본 포트는 110 번 이다 .
Connect(sServerName, 110);

// 결과를 받는다 .
sResult = Response();

// 응답이 +OK 인지 확인
if (sResult.Substring(0,3) != “+OK”) throw new POPException(sResult);
// 예전 연결을 가져와서 사용자명을 전송 한다 .
sMessage = “USER “ + sUSerName + “\r\n”;
Write(sMessage);

// 결과를 받는다 .
sResult = Response();



// 응답을 확인 한다 .
if (sResult.Substring(0,3) != “+OK”) throw new POPException(sResult);

// 비밀번호를 전송 한다 .
sMessage = “PASS “ + sUSerName + “\r\n”;
Write(sMessage);

// 결과를 받는다 .
sResult = Response();

// 응답을 확인 한다 .
if (sResult.Substring(0,3) != “+OK”) throw new POPException(sResult);





• 이번에는 연결을 종료하는 메소드를 살펴 보자 .
public void DisconnectPOP() {
string sMessage;
string sResult;
sMessage = “QUIT\r\n”;
Write(sMessage);

sResult = Response();

// 응답을 확인 한다 .
if (sResult.Substring(0,3) != “+OK”) throw new POPException(sResult);

}
• 이번에는 메시지 목록을 가지고 와 보자 . 서버에서 LIST 명령을 실행하면 편지함에 있는 메시지의 목록을 가져 올 수 있다 . ListMessages() 메소드에서 이 작업을 수행 한다 . POP3 에 LIST 명령을 전송하면 서버는 여러 행으로 이루어진 텍스트를 돌려 주는데 각 행은 메일 메시지를 가리키며 메시지 번호와 메일 메시지에 포함된 바이트 수를 알려 준다 .

public ArrayList ListMessage () {
string sMessage;
string sResult;
ArrayList returnValue = new ArrayList();
sMessage = “LIST\r\n”;
Write(sMessage);

sResult = Response();

// 응답을 확인 한다 .
if (sResult.Substring(0,3) != “+OK”) throw new POPException(sResult);

while(true) {
sResult = Response();

if (sResult == “.\r\n”) {
return returnValue;
}
else {
POP3EmailMessage oMailMessage = new POP3EmailMessage();
// 구분자를 정의
char[] sep = {‘ ‘};

// 데이터 배열을 나누기 위하여 Split 메소드를 정의
string[] values = sResult.Split();

// 데이터를 oMailMessage 에 둔다 .
oMailMessage.msgNumber = Int32.Parse(values[0]);
oMailMessage.msgSize = Int32.Parse(values[1]);
oMailMessage.msgReceived = false;
returnValue.Add(oMailMessage);
continue;
}
}
}
• 특정 메시지 가져오기 . POP3 서버로부터 전체 메시지를 가져오려면 해당 메시지의 msgNumber 값을 RETR 명령과 함께 사용해야 한다 .
public POP3EmailMessage RetrieveMessage (POP3EmailMessage msgRETR) {
string sMessage;
string sResult;
// 새로운 객체 인스턴스를 생성 하고 , 새로운 값을 설정 한다 .
POP3EmailMessage oMailMessage = new POP3EmailMessage();
oMailMessage.msgSize = msgRETR.msgSize;
oMailMessage.msgNumber = msgRETR.msgNumber;

// 메시지를 가져오기 위해 RETR 명령을 수행
sMessage = “RETR “ + msgRETR.msgNumber + “\r\n”;
Write(sMessage);

sResult = Response();

// 응답을 확인 한다 .
if (sResult.Substring(0,3) != “+OK”) throw new POPException(sResult);

// 메시지를 수신 하였으므로 수신 플래그를 true 로 바꾸자 .
oMailMessage.msgReceived = true;

// 마침표를 만날 때 까지 루프를 돌면서 본문 내용을 가져 오자 .
while(true) {
sResult = Response();
if (sResult == “.\r\n”) break;
else
oMailMessage.msgContent = sResult;
}
return oMailMessage;
}
• 이번에는 메시지를 삭제하여 보자
public void DeleteMessage (POP3EmailMessage msgDELE) {
string sMessage;
string sResult;
// 메시지를 삭제 하기 위해 DELE 명령을 수행
sMessage = “DELE “ + msgRETR.msgNumber + “\r\n”;
Write(sMessage);

sResult = Response();

// 응답을 확인 한다 .
if (sResult.Substring(0,3) != “+OK”) throw new POPException(sResult);
}
• 메시지를 입력 받아서 TCP 스트림에 쓰는 Write() 메소드를 만들어 보자 .
private void Write(string sMessage) {
// 데이터 인코딩을 사용
System.Text.ASCIIEncoding oEncodedData = new System.Text.ASCIIEncoding();
// 메시지를 TCP 네트워크 스트림에 쓰기 위하여 버퍼로 가져 온다 .
byte[] WriteBuffer = new byte[1024];

WriteBuffer = onEncodedData.GetByte(sMessage);

// 버퍼를 TCP 스트림으로 보냄
NetworkStream NetStream = GetStream();
NetStream.Write(WriteBuffer, 0, WriteBuffer.Length);
}
• Response() 메소드 , 이전의 Write 메소드에 대응하는 것이 Response() 메소드 이다 . 이 메소드를 통하여 POP3 서버의 응답을 읽을 수 있다 . 따라서 Write() 메소드의 결과를 확인 할 수 있는 메소드 인 것이다 .
public string Response() {
// 한글을 사용하기 위해 아래 처럼 KSC5601 인코딩을 이용
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("ks_c_5601-1987");
byte[] ServerBuffer = new byte[1024];
NetworkStream NetStream = GetStream();
Int count = 0;

// 서버의 네트워크 스트림으로부터 데이터를 일거 버퍼 ( 바이트배열 ) 에 기록
while(true) {
byte[] buff = new byte[2];
int bytes = NetStream.Read(buff, 0, 1);

if (bytes == 1) {
ServerBuffer[count] = buff[0];
Count++;

If (buff[0] == “\n”) break;
}
else break;
}

// 디코딩된 문자열을 반환
string returnValue = encode.GetString(ServerBuffer, 0, count);
return returnValue;
}
• 다음은 예외 처리용 클래스인 POPException 클래스 이다 .
public class POPException : System.ApplicationException {
public POPException(string str) : base(str) { }
}


UI 는 다음과 같습니다 .


테스트를 위해서는 POP3가 지원되는 서버의 계정이 있어야 겠죠^^

POPException.cs
using System;

namespace POPMailException
{
///

/// Summary description for POPException.
///


public class POPException : System.ApplicationException
{
public POPException(string str)
:base(str)
{
}
}
}

POP3.cs
using System;
using System.Net.Sockets;
using System.Collections;
using POPMailMessage;
using POPMailException;

namespace POPMail
{
public class Message
{
public string GetFrom(string messTop)
{
messTop = messTop.Remove(0,(messTop.IndexOf("\r\nFrom:") + 7));
messTop = messTop.Remove(messTop.IndexOf('\r'),((messTop.Length - messTop.IndexOf('\r')) - 1));
return messTop;
}
public string GetDate(string messTop)
{
messTop = messTop.Remove(0,(messTop.IndexOf("\r\nDate:") + 7));
messTop = messTop.Remove(messTop.IndexOf('\r'),(messTop.Length - messTop.IndexOf('\r')));
return messTop;
}
public string GetMessID(string messTop)
{
messTop = messTop.Remove(0,(messTop.IndexOf("\r\nMessage-ID: ") + 13));
messTop = messTop.Remove(messTop.IndexOf('\r'),(messTop.Length - messTop.IndexOf('\r')));
return messTop;
}
public string GetTo(string messTop)
{
messTop = messTop.Remove(0,(messTop.IndexOf("\r\nTo:") + 6));
messTop = messTop.Remove(messTop.IndexOf('\r'),(messTop.Length - messTop.IndexOf('\r')));
return messTop;
}
public string GetSubject(string messTop)
{
messTop = messTop.Remove(0,(messTop.IndexOf("\r\nSubject:") + 10));
messTop = messTop.Remove(messTop.IndexOf('\r'),(messTop.Length - messTop.IndexOf('\r')));
return messTop;
}
public string GetBody(string AllMessage)
{
AllMessage = AllMessage.Remove(0,(AllMessage.IndexOf("\r\n\r\n")));
return AllMessage;
}
}


public class POP3 : System.Net.Sockets.TcpClient
{
public void ConnectPOP(string sServerName, string sUserName, string sPassword)
{
//message and the server resulting response
string sMessage;
string sResult;

//call the connect method of the TcpClient class
//remember default port for server is 110
Connect(sServerName, 110);
//get result back
sResult = Response();

//check response to make sure it 뭩 +OK
if(sResult.Substring(0,3) != "+OK")
{
throw new POPException(sResult);
}

//got past connect, send username
sMessage = "USER " + sUserName + "\r\n";
//write sends data to the Tcp Connection
Write(sMessage);
sResult = Response();
//check response
if (sResult.Substring(0,3) != "+OK")
{
throw new POPException(sResult);
}

//now follow up with sending password in same manner
sMessage = "PASS " + sPassword + "\r\n";
Write(sMessage);
sResult = Response();
if (sResult.Substring(0,3) != "+OK")
{
throw new POPException(sResult);
}
}

public void DisconnectPOP()
{
string sMessage;
string sResult;

sMessage = "QUIT\r\n";
Write(sMessage);
sResult = Response();
if (sResult.Substring(0,3) != "+OK")
{
throw new POPException(sResult);
}
}

public ArrayList ListMessages()

{
//same sort of thing as in ConnectPOP and DisconnectPOP
string sMessage;
string sResult;

ArrayList returnValue = new ArrayList();

sMessage = "LIST\r\n";
Write(sMessage);

sResult = Response();
if (sResult.Substring(0, 3) != "+OK")
{
throw new POPException (sResult);
}

while (true)
{
sResult = Response();
if (sResult == ".\r\n")
{
return returnValue;
}
else
{
POP3EmailMessage oMailMessage = new POP3EmailMessage();
//define a separator
char[] sep = { ' ' };
//use the split method to break out array of data
string[] values = sResult.Split(sep);
//put data into oMailMessage object
oMailMessage.msgNumber = Int32.Parse(values[0]);
oMailMessage.msgSize = Int32.Parse(values[1]);
oMailMessage.msgReceived = false;
returnValue.Add(oMailMessage);
//add to message box display
continue;
}
}
}

public POP3EmailMessage RetrieveMessage(POP3EmailMessage msgRETR)
{
string sMessage;
string sResult;

//create new instance of object and set new values
POP3EmailMessage oMailMessage = new POP3EmailMessage ();
oMailMessage.msgSize = msgRETR.msgSize;
oMailMessage.msgNumber = msgRETR.msgNumber;

//call the RETR command to get the appropriate message
sMessage = "RETR " + msgRETR.msgNumber + "\r\n";
Write(sMessage);
sResult = Response();
if (sResult.Substring(0, 3) != "+OK")
{
throw new POPException (sResult);
}
//set the received flag equal to true since we got the message
oMailMessage.msgReceived = true;
//now loop to get the message text until we hit the ??end point
while (true)
{
sResult = Response();
if (sResult == ".\r\n")
{
break;
}
else
{
oMailMessage.msgContent += sResult;
}
}

return oMailMessage;
}

public void DeleteMessage(POP3EmailMessage msgDELE)

{
string sMessage;
string sResult;

sMessage = "DELE " + msgDELE.msgNumber + "\r\n";
Write(sMessage);
sResult = Response();
if (sResult.Substring(0, 3) != "+OK")
{
throw new POPException(sResult);
}
}

private void Write(string sMessage)
{
//used for Data Encoding
System.Text.ASCIIEncoding oEncodedData = new System.Text.ASCIIEncoding() ;

//now grab the message into a buffer for sending to the TCP network stream
byte[] WriteBuffer = new byte[1024] ;
WriteBuffer = oEncodedData.GetBytes(sMessage) ;

//take the buffer and output it to the TCP stream
NetworkStream NetStream = GetStream() ;
NetStream.Write(WriteBuffer,0,WriteBuffer.Length);
}


private string Response()
{
//System.Text.ASCIIEncoding oEncodedData = new System.Text.();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("ks_c_5601-1987");
byte []ServerBuffer = new Byte[1024];
NetworkStream NetStream = GetStream();
int count = 0;

//here we read from the server network stream and place data into
//the buffer (to later decode and return)
while (true)
{
byte []buff = new Byte[2];
int bytes = NetStream.Read( buff, 0, 1 );

if (bytes == 1)
{
ServerBuffer[count] = buff[0];
count++;
if (buff[0] == '\n')
{
break;
}
}
else
{
break;
}
}
//return the decoded ASCII string value
string ReturnValue = encode.GetString(ServerBuffer, 0, count );
return ReturnValue;
}



}
}

POP3EmailMessage.cs
using System;

namespace POPMailMessage
{
///

/// Summary description for POP3EmailMessage.
///


public class POP3EmailMessage
{
//define public members
public long msgNumber;
public long msgSize;
public bool msgReceived;
public string msgContent;
}

}

PopMail.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using POPMailMessage;

namespace POPMail
{
///

/// Summary description for Form1.
///


public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox Server;
private System.Windows.Forms.TextBox Username;
private System.Windows.Forms.TextBox Password;
private System.Windows.Forms.TextBox txtList;
private System.Windows.Forms.TextBox txtMessage;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox MessageNumber;
private System.Windows.Forms.TextBox txtSubject;
private System.Windows.Forms.Label label5;
///

/// Required designer variable.
///


private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///

/// Clean up any resources being used.
///


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///


private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.Server = new System.Windows.Forms.TextBox();
this.Username = new System.Windows.Forms.TextBox();
this.Password = new System.Windows.Forms.TextBox();
this.txtList = new System.Windows.Forms.TextBox();
this.txtMessage = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.MessageNumber = new System.Windows.Forms.TextBox();
this.txtSubject = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(10, 17);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 25);
this.label1.TabIndex = 0;
this.label1.Text = "POP3 Server:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(10, 43);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 25);
this.label2.TabIndex = 1;
this.label2.Text = "Username:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(10, 69);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 25);
this.label3.TabIndex = 2;
this.label3.Text = "Password:";
//
// Server
//
this.Server.Location = new System.Drawing.Point(115, 17);
this.Server.Name = "Server";
this.Server.Size = new System.Drawing.Size(259, 21);
this.Server.TabIndex = 3;
this.Server.Text = "";
//
// Username
//
this.Username.Location = new System.Drawing.Point(115, 43);
this.Username.Name = "Username";
this.Username.Size = new System.Drawing.Size(259, 21);
this.Username.TabIndex = 4;
this.Username.Text = "";
//
// Password
//
this.Password.Location = new System.Drawing.Point(115, 69);
this.Password.Name = "Password";
this.Password.PasswordChar = '*';
this.Password.Size = new System.Drawing.Size(259, 21);
this.Password.TabIndex = 5;
this.Password.Text = "";
//
// txtList
//
this.txtList.Location = new System.Drawing.Point(10, 129);
this.txtList.Multiline = true;
this.txtList.Name = "txtList";
this.txtList.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtList.Size = new System.Drawing.Size(364, 95);
this.txtList.TabIndex = 6;
this.txtList.Text = "";
this.txtList.WordWrap = false;
//
// txtMessage
//
this.txtMessage.Location = new System.Drawing.Point(10, 312);
this.txtMessage.Multiline = true;
this.txtMessage.Name = "txtMessage";
this.txtMessage.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtMessage.Size = new System.Drawing.Size(364, 152);
this.txtMessage.TabIndex = 7;
this.txtMessage.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(211, 233);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(163, 24);
this.button1.TabIndex = 8;
this.button1.Text = "Get Message";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(211, 95);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(163, 25);
this.button2.TabIndex = 9;
this.button2.Text = "Get List";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(10, 233);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(76, 17);
this.label4.TabIndex = 10;
this.label4.Text = "Number #:";
//
// MessageNumber
//
this.MessageNumber.Location = new System.Drawing.Point(96, 233);
this.MessageNumber.Name = "MessageNumber";
this.MessageNumber.Size = new System.Drawing.Size(48, 21);
this.MessageNumber.TabIndex = 11;
this.MessageNumber.Text = "1";
//
// txtSubject
//
this.txtSubject.Location = new System.Drawing.Point(8, 280);
this.txtSubject.Multiline = true;
this.txtSubject.Name = "txtSubject";
this.txtSubject.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtSubject.Size = new System.Drawing.Size(364, 24);
this.txtSubject.TabIndex = 12;
this.txtSubject.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 262);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(76, 17);
this.label5.TabIndex = 13;
this.label5.Text = "Subject";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 471);
this.Controls.Add(this.label5);
this.Controls.Add(this.txtSubject);
this.Controls.Add(this.MessageNumber);
this.Controls.Add(this.label4);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtMessage);
this.Controls.Add(this.txtList);
this.Controls.Add(this.Password);
this.Controls.Add(this.Username);
this.Controls.Add(this.Server);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "POP3 Client";
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///


[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button2_Click(object sender, System.EventArgs e)
{
POP3 oPOP = new POP3();
oPOP.ConnectPOP(Server.Text, Username.Text, Password.Text);

ArrayList MessageList = oPOP.ListMessages();
foreach (POP3EmailMessage POPMsg in MessageList)
{
txtList.Text = txtList.Text + "\r\n" + "Number: " + POPMsg.msgNumber.ToString() + " Size: " + POPMsg.msgSize.ToString() + " bytes";
}
oPOP.DisconnectPOP();

}

private void button1_Click(object sender, System.EventArgs e)
{
POP3 oPOP = new POP3();
oPOP.ConnectPOP(Server.Text, Username.Text, Password.Text);
POP3EmailMessage POPMsg = new POP3EmailMessage();
POPMsg.msgNumber = Convert.ToInt32(MessageNumber.Text);
POP3EmailMessage POPMsgContent = oPOP.RetrieveMessage(POPMsg);
txtMessage.Text = POPMsgContent.msgContent;

Message m = new Message();

string str = m.GetSubject(POPMsgContent.msgContent);
// 메일의 제목을 넣자 .
txtSubject.Text = str;

oPOP.DisconnectPOP();
}
}
}
 



  • 자바
  • 오라클/빅데이터
  • 아이폰/안드로이드
  • 닷넷/WPF
  • 표준웹/HTML5
  • 채용/취업무료교육
  • 초보자코스

  • 댓글 없음:

    댓글 쓰기