// --------------------------- ConnectAndLogin ----------------------------- public void ConnectAndLogin( ) { // connect to the mail server. mSockEx = new SocketExchange( m_credential.Server, Port, Logger); if (UseSecureConnection == true) { mSockEx.SecureConnect( ); } else { mSockEx.Connect( ); } SignalConnectedEvent(new MailEventArgs(MailEvent.Connected)); // receive initial connection response from mail server. mSockEx.ExpectedResponseCodes = new ExpectedResponseCodes(PopConstants.Ok); mSockEx.Receive( ); mSockEx.ThrowIfUnexpectedResponse( ); // send login details ... MailServerLogin( ); }
// --------------------------- OpenInBox ----------------------------- public SocketExchange OpenInbox( ) { // connect to the mail server. SocketExchange sockEx = new SocketExchange( m_credential.Server, Port, Logger); if (UseSecureConnection == true) { sockEx.SecureConnect( ); mSecureSocket = sockEx.ConnectedSecureSocket; } else { sockEx.Connect( ); m_socket = sockEx.ConnectedSocket; } SignalConnectedEvent(new MailEventArgs(MailEvent.Connected)); // receive initial connection response from mail server. sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(PopConstants.Ok); sockEx.Receive( ); sockEx.ThrowIfUnexpectedResponse( ); // send login details ... MailServerLogin(sockEx); return(sockEx); }
// ------------------------------ SendMail -------------------------------- public void SendMailOld(MailMessage msg) { SocketExchange sockEx = null; // connect to the mail server. sockEx = new SocketExchange(ServerName, Port, DetailLogger); try { if (UseSecureConnection == true) { sockEx.SecureConnect( ); } else { sockEx.Connect( ); } SignalConnectedEvent(new MailEventArgs(MailEvent.Connected)); // receive the initial server hello message. sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.ServerReady); sockEx.Receive( ); sockEx.ThrowIfUnexpectedResponse( ); // hello message exchange. sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.Ok); if (ServerRequiresAuthentication == true) { sockEx.SendReceive("EHLO " + Dns.GetHostName() + SmtpConstants.CrLf); } else { sockEx.SendReceive("HELO " + Dns.GetHostName() + SmtpConstants.CrLf); } sockEx.ThrowIfUnexpectedResponse( ); // got a 250, but did not get a 250-AUTH. receive more. if ((sockEx.ResponseCode == SmtpConstants.Ok) && (sockEx.ResponseMessageContains("AUTH") == false)) { LogMessage(NetworkRole.Client, "AUTH not received. Receive more."); sockEx.SleepThenReadMoreAvailableData(300); } // authentication loop sockEx.ReadMoreDelay = 0; if (ServerRequiresAuthentication == true) { int okCount = 0; while (true) { sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.Ok, SmtpConstants.Challenge); sockEx.SendReceive("AUTH LOGIN" + SmtpConstants.CrLf); sockEx.ThrowIfUnexpectedResponse( ); if (sockEx.ResponseCode == SmtpConstants.Challenge) { AuthenticateToServer(sockEx); break; } else if (sockEx.ResponseCode == SmtpConstants.Ok) { ++okCount; continue; } } } // send the MAIL FROM message to the server. This is the start of the sending // of the email message. sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.Challenge, SmtpConstants.Ok); sockEx.SendReceive("MAIL FROM: <" + msg.From.Address + ">" + SmtpConstants.CrLf); sockEx.ThrowIfUnexpectedResponse( ); if (sockEx.ResponseCode == SmtpConstants.Challenge) { AuthenticateToServer(sockEx); } // send "rcpt to" message to the mail server to validate the recipients. ServerValidateRecipients(sockEx, msg.ToRecipients); ServerValidateRecipients(sockEx, msg.CCRecipients); ServerValidateRecipients(sockEx, msg.BCCRecipients); // send the DATA message to the server. sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.DataReady); sockEx.SendReceive("DATA" + SmtpConstants.CrLf); sockEx.ThrowIfUnexpectedResponse( ); // send the message itself. SignalStartDataSendEvent(new MailEventArgs(MailEvent.StartDataSend)); sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.Ok); string dataMessage = msg.ToString( ) + SmtpConstants.CrLf + "." + SmtpConstants.CrLf; sockEx.SendReceive(dataMessage); sockEx.ThrowIfUnexpectedResponse( ); SignalEndDataSendEvent(new MailEventArgs(MailEvent.EndDataSend)); // send quit message. sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.Quit); sockEx.SendReceive("QUIT" + SmtpConstants.CrLf); sockEx.ThrowIfUnexpectedResponse( ); } // close the connection. finally { SignalDisconnectedEvent(new MailEventArgs(MailEvent.Disconnected)); sockEx.CloseConnection( ); } }
/// <summary> /// Connect and login to the smtp server. /// </summary> /// <returns></returns> public SocketExchange Connect( ) { // connect to the mail server. SocketExchange sockEx = new SocketExchange(ServerName, Port, DetailLogger); if (UseSecureConnection == true) { sockEx.SecureConnect( ); } else { sockEx.Connect( ); } SignalConnectedEvent(new MailEventArgs(MailEvent.Connected)); // receive the initial server hello message. sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.ServerReady); sockEx.Receive( ); sockEx.ThrowIfUnexpectedResponse( ); // hello message exchange. sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.Ok); if (ServerRequiresAuthentication == true) { sockEx.SendReceive("EHLO " + Dns.GetHostName() + SmtpConstants.CrLf); } else { sockEx.SendReceive("HELO " + Dns.GetHostName() + SmtpConstants.CrLf); } sockEx.ThrowIfUnexpectedResponse( ); // got a 250, but did not get a 250-AUTH. receive more. if ((sockEx.ResponseCode == SmtpConstants.Ok) && (sockEx.ResponseMessageContains("AUTH") == false)) { LogMessage(NetworkRole.Client, "AUTH not received. Receive more."); sockEx.SleepThenReadMoreAvailableData(300); } // authentication loop sockEx.ReadMoreDelay = 0; if (ServerRequiresAuthentication == true) { int okCount = 0; while (true) { sockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.Ok, SmtpConstants.Challenge); sockEx.SendReceive("AUTH LOGIN" + SmtpConstants.CrLf); sockEx.ThrowIfUnexpectedResponse( ); if (sockEx.ResponseCode == SmtpConstants.Challenge) { AuthenticateToServer(sockEx); break; } else if (sockEx.ResponseCode == SmtpConstants.Ok) { ++okCount; continue; } } } return(sockEx); }