/* * /// <summary> * /// Return true if this is among the supported AUTH * /// types. * /// </summary> * /// <param name="authtypes"></param> * /// <returns></returns> * public bool IsSupported(SmtpAuthType[] authtypes) * { * log.Debug("CHECKING SUPPORTED TYPES"); * for (int i=0; i<authtypes.Length; i++) * { * log.Debug("CHECKING IF "+authtypes[i]+"="+SmtpAuthType.Login); * if (authtypes[i]==SmtpAuthType.Login) * { * return true; * } * } * return false; * } */ /// <summary> /// Return the 235 response code if valid, otherwise /// return the error. /// </summary> /// <param name="smtpProxy"></param> /// <param name="supportedAuthTypes">the supported auth types</param> /// <returns></returns> public SmtpResponse Negotiate(ISmtpProxy smtpProxy, String[] supportedAuthTypes) { SmtpResponse response = smtpProxy.Auth("login"); //log.Debug("RESPONSE WAS "+response.ResponseCode+" "+response.Message); if (response.ResponseCode != 334) { return(response); } Base64Encoder encoder = Base64Encoder.GetInstance(); response = smtpProxy.SendString(encoder.EncodeString(this.UserName, this._charEncoding)); if (response.ResponseCode != 334) { return(response); } response = smtpProxy.SendString(encoder.EncodeString(this.Password, this._charEncoding)); if (response.ResponseCode != 334) { // here it's an error return(response); } else { // here it's ok. return(response); } }
public void TestSimpleSmtpNegotiation() { SmtpServer smtpserver = new SmtpServer("localhost"); EmailMessage emailMessage = GetTestHtmlAndTextMessage(); IMock mockSmtpProxy = new DynamicMock(typeof(ISmtpProxy)); mockSmtpProxy.ExpectAndReturn("Open", new SmtpResponse(220, "welcome to the mock object server"), null); mockSmtpProxy.ExpectAndReturn("Helo", new SmtpResponse(250, "helo"), Dns.GetHostName()); mockSmtpProxy.ExpectAndReturn("MailFrom", new SmtpResponse(250, "mail from"), emailMessage.FromAddress); foreach (EmailAddress rcpttoaddr in emailMessage.ToAddresses) { mockSmtpProxy.ExpectAndReturn("RcptTo", new SmtpResponse(250, "receipt to"), rcpttoaddr); } mockSmtpProxy.ExpectAndReturn("Data", new SmtpResponse(354, "data open"), null); mockSmtpProxy.ExpectAndReturn("WriteData", new SmtpResponse(250, "data"), emailMessage.ToDataString()); mockSmtpProxy.ExpectAndReturn("Quit", new SmtpResponse(221, "quit"), null); mockSmtpProxy.Expect("Close", null); ISmtpProxy smtpProxy = (ISmtpProxy)mockSmtpProxy.MockInstance; smtpserver.OverrideSmtpProxy(smtpProxy); emailMessage.Send(smtpserver); }
public RemindersController(ITimeApprovalService timeApprovalService, ISmtpProxy smtpProxy, IEmployeeRepository employeeRepository, IScheduledTaskRepo scheduleTaskRepo, ILogger logger) { _timeApprovalService = timeApprovalService; _smtpProxy = smtpProxy; _employeeRepository = employeeRepository; _scheduleTaskRepo = scheduleTaskRepo; _logger = logger; }
public ApproveTimeCommand(ITimeApprovalService timeApprovalService, ITimeService timeService, IEmployeeRepository employeeService, ISmtpProxy smtpProxy, IConfiguration configuration, IJobsRepository jobService) { this.timeApprovalService = timeApprovalService; this.timeService = timeService; this.employeeService = employeeService; this.smtpProxy = smtpProxy; this.configuration = configuration; this.jobService = jobService; }
public void TestFailedSmtpNegotiationWithAuth() { SmtpServer smtpserver = new SmtpServer("localhost"); smtpserver.SmtpAuthToken = new SmtpAuthToken("test", "test"); Base64Encoder encoder = Base64Encoder.GetInstance(); String base64Username = encoder.EncodeString(smtpserver.SmtpAuthToken.UserName, System.Text.Encoding.ASCII); String base64Password = encoder.EncodeString(smtpserver.SmtpAuthToken.Password, System.Text.Encoding.ASCII); EmailMessage emailMessage = GetTestHtmlAndTextMessage(); IMock mockSmtpProxy = new DynamicMock(typeof(ISmtpProxy)); mockSmtpProxy.ExpectAndReturn("Open", new SmtpResponse(220, "welcome to the mock object server"), null); EhloSmtpResponse ehloResponse = new EhloSmtpResponse(); ehloResponse.AddAvailableAuthType("login"); ehloResponse.Message = "OK"; ehloResponse.ResponseCode = 250; mockSmtpProxy.ExpectAndReturn("Ehlo", ehloResponse, Dns.GetHostName()); mockSmtpProxy.ExpectAndReturn("Auth", new SmtpResponse(554, "Unrecognized auth type"), "login"); ISmtpProxy smtpProxy = (ISmtpProxy)mockSmtpProxy.MockInstance; smtpserver.OverrideSmtpProxy(smtpProxy); /* * mockSmtpProxy.ExpectAndReturn("SendString", new SmtpResponse(554, "Invalid UserName"), base64Username); * mockSmtpProxy.ExpectAndReturn("SendString", new SmtpResponse(554, "Invalid Password"), base64Password); * mockSmtpProxy.ExpectAndReturn("MailFrom", new SmtpResponse(250, "mail from"), emailMessage.FromAddress); * foreach (EmailAddress rcpttoaddr in emailMessage.ToAddresses) * { * mockSmtpProxy.ExpectAndReturn("RcptTo", new SmtpResponse(250, "receipt to"), rcpttoaddr); * } * mockSmtpProxy.ExpectAndReturn("Data", new SmtpResponse(354, "data open"), null); * mockSmtpProxy.ExpectAndReturn("WriteData", new SmtpResponse(250, "data"), emailMessage.ToDataString()); * * mockSmtpProxy.ExpectAndReturn("Quit", new SmtpResponse(221, "quit"), null); * mockSmtpProxy.Expect("Close", null); */ try { emailMessage.Send(smtpserver); Assert.Fail("The auth type is wrong"); } catch (SmtpException ex) { log.Debug("ERROR CODE IS " + 554); Assert.AreEqual(554, ex.ErrorCode); } }
public void TestForcedLoginSmtpNegotiationWithAuth() { SmtpServer smtpserver = new SmtpServer("localhost"); smtpserver.SmtpAuthToken = new LoginAuthToken("test", "testtest"); Base64Encoder encoder = Base64Encoder.GetInstance(); String base64Username = encoder.EncodeString(smtpserver.SmtpAuthToken.UserName, System.Text.Encoding.ASCII); String base64Password = encoder.EncodeString(smtpserver.SmtpAuthToken.Password, System.Text.Encoding.ASCII); EmailMessage emailMessage = GetTestHtmlAndTextMessage(); IMock mockSmtpProxy = new DynamicMock(typeof(ISmtpProxy)); mockSmtpProxy.ExpectAndReturn("Open", new SmtpResponse(220, "welcome to the mock object server"), null); EhloSmtpResponse ehloResponse = new EhloSmtpResponse(); ehloResponse.AddAvailableAuthType("login"); ehloResponse.Message = "OK"; ehloResponse.ResponseCode = 250; mockSmtpProxy.ExpectAndReturn("Ehlo", ehloResponse, Dns.GetHostName()); mockSmtpProxy.ExpectAndReturn("Auth", new SmtpResponse(334, encoder.EncodeString("Username:"******"login"); mockSmtpProxy.ExpectAndReturn("SendString", new SmtpResponse(334, encoder.EncodeString("Password:"******"SendString", new SmtpResponse(235, "Hooray, Authenticated"), base64Password); mockSmtpProxy.ExpectAndReturn("MailFrom", new SmtpResponse(250, "mail from"), emailMessage.FromAddress); foreach (EmailAddress rcpttoaddr in emailMessage.ToAddresses) { mockSmtpProxy.ExpectAndReturn("RcptTo", new SmtpResponse(250, "receipt to"), rcpttoaddr); } mockSmtpProxy.ExpectAndReturn("Data", new SmtpResponse(354, "data open"), null); mockSmtpProxy.ExpectAndReturn("WriteData", new SmtpResponse(250, "data"), emailMessage.ToDataString()); mockSmtpProxy.ExpectAndReturn("Quit", new SmtpResponse(221, "quit"), null); mockSmtpProxy.Expect("Close", null); ISmtpProxy smtpProxy = (ISmtpProxy)mockSmtpProxy.MockInstance; smtpserver.OverrideSmtpProxy(smtpProxy); try { emailMessage.Send(smtpserver); } catch (SmtpException ex) { log.Debug("Exception was " + ex.Message); log.Debug(ex.StackTrace); throw ex; } }
/// <summary> /// Return the 235 response code if valid, otherwise /// return the error. /// </summary> /// <param name="smtpProxy">The SmtpProxy being used</param> /// <param name="supportedAuthTypes">String array of EHLO-specified auth types</param> /// <returns></returns> public SmtpResponse Negotiate(ISmtpProxy smtpProxy, String[] supportedAuthTypes) { ISmtpAuthToken token = null; foreach (String authType in supportedAuthTypes) { if (authType.Equals("login")) { token = new LoginAuthToken(_userName, _password); break; } } if (token == null) { return(new SmtpResponse(504, DotNetOpenMail.Resources.ARM.GetInstance().GetString("unrecognized_auth_type"))); } return(token.Negotiate(smtpProxy, supportedAuthTypes)); }
/// <summary> /// Return the 235 response code if valid, otherwise /// return the error. /// </summary> /// <param name="smtpProxy">The SmtpProxy being used</param> /// <param name="supportedAuthTypes">String array of EHLO-specified auth types</param> /// <returns></returns> public SmtpResponse Negotiate(ISmtpProxy smtpProxy, String[] supportedAuthTypes) { ISmtpAuthToken token=null; foreach (String authType in supportedAuthTypes) { if (authType.Equals("login")) { token=new LoginAuthToken(_userName, _password); break; } } if (token==null) { return new SmtpResponse(504, Alps.Net.Mail.Resources.ARM.GetInstance().GetString("unrecognized_auth_type")); } return token.Negotiate(smtpProxy, supportedAuthTypes); }
/* /// <summary> /// Return true if this is among the supported AUTH /// types. /// </summary> /// <param name="authtypes"></param> /// <returns></returns> public bool IsSupported(SmtpAuthType[] authtypes) { log.Debug("CHECKING SUPPORTED TYPES"); for (int i=0; i<authtypes.Length; i++) { log.Debug("CHECKING IF "+authtypes[i]+"="+SmtpAuthType.Login); if (authtypes[i]==SmtpAuthType.Login) { return true; } } return false; } */ /// <summary> /// Return the 235 response code if valid, otherwise /// return the error. /// </summary> /// <param name="smtpProxy"></param> /// <param name="supportedAuthTypes">the supported auth types</param> /// <returns></returns> public SmtpResponse Negotiate(ISmtpProxy smtpProxy, String[] supportedAuthTypes) { SmtpResponse response=smtpProxy.Auth("login"); //log.Debug("RESPONSE WAS "+response.ResponseCode+" "+response.Message); if (response.ResponseCode!=334) { return response; } Base64Encoder encoder=Base64Encoder.GetInstance(); response=smtpProxy.SendString(encoder.EncodeString(this.UserName, this._charEncoding )); if (response.ResponseCode!=334) { return response; } response=smtpProxy.SendString(encoder.EncodeString(this.Password, this._charEncoding )); if (response.ResponseCode!=334) { // here it's an error return response; } else { // here it's ok. return response; } }
/// <summary> /// Override the SmtpProxy. This is only /// for testing smtp negotiation without an /// smtp server. /// </summary> /// <param name="smtpProxy"></param> public void OverrideSmtpProxy(ISmtpProxy smtpProxy) { _smtpProxy=smtpProxy; }
/// <summary> /// Override the SmtpProxy. This is only /// for testing smtp negotiation without an /// smtp server. /// </summary> /// <param name="smtpProxy"></param> public void OverrideSmtpProxy(ISmtpProxy smtpProxy) { _smtpProxy = smtpProxy; }
/// <summary> /// Send the email. /// </summary> /// <param name="emailMessage">The completed message</param> /// <param name="rcpttocollection">A list of email addresses which /// are to be used in the RCPT TO SMTP communication</param> /// <param name="mailfrom">An email address for the MAIL FROM /// part of the SMTP protocol.</param> /// <exception cref="SmtpException">throws an SmtpException if there /// is an unexpected SMTP error number sent from the server.</exception> /// <exception cref="MailException">throws a MailException if there /// is a network problem, connection problem, or other issue.</exception> /// <returns></returns> internal bool Send(ISendableMessage emailMessage, EmailAddressCollection rcpttocollection, EmailAddress mailfrom) { //ISmtpNegotiator negotiator=_smtpserver.GetSmtpNegotiator(); ISmtpProxy smtpproxy = GetSmtpProxy(); //smtpproxy.CaptureSmtpConversation=CaptureSmtpConversation; SmtpResponse smtpResponse = smtpproxy.Open(); try { #region Connect if (smtpResponse.ResponseCode != 220) { throw smtpResponse.GetException(); } #endregion #region HELO / EHLO if (UseEhlo()) { EhloSmtpResponse esmtpResponse = smtpproxy.Ehlo(GetHeloHost()); if (esmtpResponse.ResponseCode != 250) { // TODO: FIX THIS throw new SmtpException(esmtpResponse.ResponseCode, esmtpResponse.Message); } // do SMTP AUTH if (this._authToken != null) { smtpResponse = _authToken.Negotiate(smtpproxy, esmtpResponse.GetAvailableAuthTypes()); if (smtpResponse.ResponseCode != 235) { throw smtpResponse.GetException(); } } } else { smtpResponse = smtpproxy.Helo(GetHeloHost()); if (smtpResponse.ResponseCode != 250) { throw smtpResponse.GetException(); } } #endregion #region MAIL FROM smtpResponse = smtpproxy.MailFrom(mailfrom); if (smtpResponse.ResponseCode != 250) { throw smtpResponse.GetException(); } #endregion #region RCPT TO foreach (EmailAddress rcpttoaddress in rcpttocollection) { smtpResponse = smtpproxy.RcptTo(rcpttoaddress); if (smtpResponse.ResponseCode != 250) { throw smtpResponse.GetException(); } } #endregion #region DATA smtpResponse = smtpproxy.Data(); if (smtpResponse.ResponseCode != 354) { throw smtpResponse.GetException(); } //smtpResponse=negotiator.WriteData(); String message = emailMessage.ToDataString(); if (message == null) { throw new MailException("The message content is null"); } /* * // START Test With Domain Keys * // (this would appear as an event callback if it works) * MailSigner mailsigner=new MailSigner(); * bool signed = mailsigner.signMail(message); * if (!signed) * { * throw new MailException("Error creating DomainKeys signature."); * } * message=mailsigner.signedHeader + message; * // END Test With Domain Keys */ // Send the data smtpResponse = smtpproxy.WriteData(message); if (smtpResponse.ResponseCode != 250) { throw smtpResponse.GetException(); } #endregion #region QUIT // QUIT smtpResponse = smtpproxy.Quit(); if (smtpResponse.ResponseCode != 221) { throw smtpResponse.GetException(); } #endregion } finally { smtpproxy.Close(); OnLogSmtpCompleted(this, "Connection Closed"); } return(true); }