/// <summary> /// Sends STARTTLS command to SMTP server. /// </summary> /// <param name="certCallback">SSL server certificate validation callback. Value null means any certificate is accepted.</param> /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this method is accessed.</exception> /// <exception cref="InvalidOperationException">Is raised when SMTP client is not connected or is already secure connection.</exception> /// <exception cref="SMTP_ClientException">Is raised when SMTP server returns error.</exception> /// <remarks>After successful STARTTLS all SMTP session data(EHLO,MAIL FROM, ....) will be reset. /// If unknwon(not SMTP error) error happens during STARTTLS negotiation, SMTP client should disconnect.</remarks> public void StartTLS(RemoteCertificateValidationCallback certCallback) { if(this.IsDisposed){ throw new ObjectDisposedException(this.GetType().Name); } if(!this.IsConnected){ throw new InvalidOperationException("You must connect first."); } if(this.IsSecureConnection){ throw new InvalidOperationException("Connection is already secure."); } ManualResetEvent wait = new ManualResetEvent(false); using(StartTlsAsyncOP op = new StartTlsAsyncOP(certCallback)){ op.CompletedAsync += delegate(object s1,EventArgs<StartTlsAsyncOP> e1){ wait.Set(); }; if(!this.StartTlsAsync(op)){ wait.Set(); } wait.WaitOne(); wait.Close(); if(op.Error != null){ throw op.Error; } } }
/// <summary> /// Starts sending STARTTLS command to SMTP server. /// </summary> /// <param name="op">Asynchronous operation.</param> /// <returns>Returns true if aynchronous operation is pending (The <see cref="StartTlsAsyncOP.CompletedAsync"/> event is raised upon completion of the operation). /// Returns false if operation completed synchronously.</returns> /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and and this method is accessed.</exception> /// <exception cref="InvalidOperationException">Is raised when SMTP client is not connected or connection is already secure.</exception> /// <exception cref="ArgumentNullException">Is raised when <b>op</b> is null reference.</exception> /// <remarks>After successful STARTTLS all SMTP session data(EHLO,MAIL FROM, ....) will be reset. /// If unknwon(not SMTP error) error happens during STARTTLS negotiation, SMTP client should disconnect.</remarks> public bool StartTlsAsync(StartTlsAsyncOP op) { if(this.IsDisposed){ throw new ObjectDisposedException(this.GetType().Name); } if(!this.IsConnected){ throw new InvalidOperationException("You must connect first."); } if(this.IsSecureConnection){ throw new InvalidOperationException("Connection is already secure."); } if(op == null){ throw new ArgumentNullException("op"); } if(op.State != AsyncOP_State.WaitingForStart){ throw new ArgumentException("Invalid argument 'op' state, 'op' must be in 'AsyncOP_State.WaitingForStart' state.","op"); } return op.Start(this); }