/// <summary> /// send QUIT command and disconnect from server. /// </summary> /// <param name="InSockEx"></param> public void Disconnect(SocketExchange InSockEx) { // send quit message. InSockEx.ExpectedResponseCodes = new ExpectedResponseCodes(SmtpConstants.Quit); InSockEx.SendReceive("QUIT" + SmtpConstants.CrLf); InSockEx.ThrowIfUnexpectedResponse( ); SignalDisconnectedEvent(new MailEventArgs(MailEvent.Disconnected)); InSockEx.CloseConnection( ); }
// ------------------------------ SendMail -------------------------------- public void SendMail(MailMessage msg) { SocketExchange sockEx = null; try { sockEx = Connect( ); // 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)); Disconnect(sockEx); sockEx = null; } // close the connection. finally { if (sockEx != null) { SignalDisconnectedEvent(new MailEventArgs(MailEvent.Disconnected)); sockEx.CloseConnection( ); } } }
// ------------------------------ 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( ); } }