[Test] // .ctor (String) public void Constructor3 () { string msg; SmtpException se; msg = "MESSAGE"; se = new SmtpException (msg); Assert.IsNotNull (se.Data, "#A1"); Assert.AreEqual (0, se.Data.Keys.Count, "#A2"); Assert.IsNull (se.InnerException, "#A3"); Assert.AreSame (msg, se.Message, "#A4"); Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#A5"); msg = string.Empty; se = new SmtpException (msg); Assert.IsNotNull (se.Data, "#B1"); Assert.AreEqual (0, se.Data.Keys.Count, "#B2"); Assert.IsNull (se.InnerException, "#B3"); Assert.AreSame (msg, se.Message, "#B4"); Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#B5"); msg = null; se = new SmtpException (msg); Assert.IsNotNull (se.Data, "#C1"); Assert.AreEqual (0, se.Data.Keys.Count, "#C2"); Assert.IsNull (se.InnerException, "#C3"); Assert.IsNotNull (se.Message, "#C4"); Assert.IsTrue (se.Message.IndexOf ("'" + typeof (SmtpException).FullName + "'") != -1, "#C5:" + se.Message); Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#C6"); }
static XPathSmtpError GetError(SmtpException exception) { return new XPathSmtpError { Status = exception.StatusCode, Message = exception.Message }; }
private void Complete(Exception exception, IAsyncResult result) { ContextAwareResult asyncState = (ContextAwareResult)result.AsyncState; try { if (this.cancelled) { exception = null; this.Abort(); } else if ((exception != null) && (!(exception is SmtpFailedRecipientException) || ((SmtpFailedRecipientException)exception).fatal)) { this.Abort(); if (!(exception is SmtpException)) { exception = new SmtpException(SR.GetString("SmtpSendMailFailure"), exception); } } else { if (this.writer != null) { this.writer.Close(); } this.transport.ReleaseConnection(); } } finally { asyncState.InvokeCallback(exception); } }
/// <summary> /// Sends an email from the SMTP server specified in the application configuration file. /// </summary> /// <param name="toSend">The email to send.</param> /// <param name="message">A message generated by this method.</param> /// <param name="exception">The SMTP exception should one occur.</param> /// <returns><c>true</c> if the email sent successfully; otherwise, <c>false</c>.</returns> public virtual bool SendEmail(Email toSend, out string message, out SmtpException exception) { bool returnVal = false; exception = null; message = null; SmtpClient sendMessage = new SmtpClient(); MailMessage email = new MailMessage(toSend.EmailFrom, toSend.EmailTo, toSend.Subject, toSend.EmailMessage) { IsBodyHtml = toSend.IsHtml }; if (toSend.CC != null && toSend.CC.Length > 0) foreach (string cc in toSend.CC) email.CC.Add(new MailAddress(cc)); if (toSend.EmailAttachments != null && toSend.EmailAttachments.Length > 0) foreach (Attachment attachment in toSend.EmailAttachments) email.Attachments.Add(attachment); try { sendMessage.Send(email); returnVal = true; message = Resources.EmailMessageSent.FormatWith(toSend.Subject, toSend.EmailFrom, toSend.EmailTo); } catch (SmtpException e) { exception = e; message = Resources.EmailMessageNotSent.FormatWith(toSend.Subject, toSend.EmailFrom, toSend.EmailTo); } return returnVal; }
private void LogEmailFailure(string[] address, string subject, string body, SmtpException e) { string message = "Email Failure: {0}\r\nAddresses: {1}\r\nSubject {2}\r\n".With( e.Message, address.Join(";"), subject); logger.Error(message, e); }
private void Complete(Exception exception, IAsyncResult result) { ContextAwareResult operationCompletedResult = (ContextAwareResult)result.AsyncState; if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } try { if (_cancelled) { //any exceptions were probably caused by cancellation, clear it. exception = null; Abort(); } // An individual failed recipient exception is benign, only abort here if ALL the recipients failed. else if (exception != null && (!(exception is SmtpFailedRecipientException) || ((SmtpFailedRecipientException)exception).fatal)) { if (NetEventSource.IsEnabled) { NetEventSource.Error(this, exception); } Abort(); if (!(exception is SmtpException)) { exception = new SmtpException(SR.SmtpSendMailFailure, exception); } } else { if (_writer != null) { try { _writer.Close(); } // Close may result in a DataStopCommand and the server may return error codes at this time. catch (SmtpException se) { exception = se; } } _transport.ReleaseConnection(); } } finally { operationCompletedResult.InvokeCallback(exception); } if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Complete"); } }
[Test] // .ctor () public void Constructor1 () { SmtpException se = new SmtpException (); Assert.IsNotNull (se.Data, "#1"); Assert.AreEqual (0, se.Data.Keys.Count, "#2"); Assert.IsNull (se.InnerException, "#3"); Assert.IsNotNull (se.Message, "#4"); Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#5:" + se.Message); Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#6"); }
void Complete(Exception exception, IAsyncResult result) { ContextAwareResult operationCompletedResult = (ContextAwareResult)result.AsyncState; GlobalLog.Enter("SmtpClient#" + ValidationHelper.HashString(this) + "::Complete"); try { if (cancelled) { //any exceptions were probably caused by cancellation, clear it. exception = null; Abort(); } // An individual failed recipient exception is benign, only abort here if ALL the recipients failed. else if (exception != null && (!(exception is SmtpFailedRecipientException) || ((SmtpFailedRecipientException)exception).fatal)) { GlobalLog.Print("SmtpClient#" + ValidationHelper.HashString(this) + "::Complete Exception: " + exception.ToString()); Abort(); if (!(exception is SmtpException)) { exception = new SmtpException(SR.GetString(SR.SmtpSendMailFailure), exception); } } else { if (writer != null) { try { writer.Close(); } // Close may result in a DataStopCommand and the server may return error codes at this time. catch (SmtpException se) { exception = se; } } transport.ReleaseConnection(); } } finally { operationCompletedResult.InvokeCallback(exception); } GlobalLog.Leave("SmtpClient#" + ValidationHelper.HashString(this) + "::Complete"); }
[Test] // .ctor (SmtpStatusCode) public void Constructor2 () { SmtpException se; se = new SmtpException (SmtpStatusCode.HelpMessage); Assert.IsNotNull (se.Data, "#A1"); Assert.AreEqual (0, se.Data.Keys.Count, "#A2"); Assert.IsNull (se.InnerException, "#A3"); Assert.IsNotNull (se.Message, "#A4"); Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#A5:" + se.Message); Assert.AreEqual (SmtpStatusCode.HelpMessage, se.StatusCode, "#A6"); se = new SmtpException ((SmtpStatusCode) 666); Assert.IsNotNull (se.Data, "#B1"); Assert.AreEqual (0, se.Data.Keys.Count, "#B2"); Assert.IsNull (se.InnerException, "#B3"); Assert.IsNotNull (se.Message, "#B4"); Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#B5:" + se.Message); Assert.AreEqual ((SmtpStatusCode) 666, se.StatusCode, "#B6"); }
private void Complete(Exception exception, IAsyncResult result) { ContextAwareResult asyncState = (ContextAwareResult) result.AsyncState; try { if (this.cancelled) { exception = null; this.Abort(); } else if ((exception != null) && (!(exception is SmtpFailedRecipientException) || ((SmtpFailedRecipientException) exception).fatal)) { this.Abort(); if (!(exception is SmtpException)) { exception = new SmtpException(SR.GetString("SmtpSendMailFailure"), exception); } } else { if (this.writer != null) { this.writer.Close(); } this.transport.ReleaseConnection(); } } finally { asyncState.InvokeCallback(exception); } }
public static bool SendEmail(Emails email, string type) { MailMessage mailMsg = new MailMessage(); SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["smtpServer"]); smtpClient.Port = 587; //smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpUser"].ToString(), ConfigurationManager.AppSettings["smtpPw"].ToString()); //SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); //smtpClient.Credentials = new System.Net.NetworkCredential("*****@*****.**", "malik420"); //smtpClient.EnableSsl = false; try { string WelcomeNote = string.Empty; string EmailText = string.Empty; mailMsg.From = new MailAddress(email.From, email.From); mailMsg.To.Add(new MailAddress(email.To, email.To)); mailMsg.Subject = email.Subject; //mailMsg.CC.Add(new MailAddress(email.CC, email.CC)); //mailMsg.Bcc.Add(new MailAddress(email.BCC, email.BCC)); switch (type) { case "RegistrationSubmissionEmail": { mailMsg.Subject = "EPRTS Registration Submission Email."; EmailText = RegistrationSubmissionEmail.ToString(); } break; case "ApplicationApprovedEmail": { EmailText = ApplicationApprovedEmail.ToString(); EmailText = EmailText.Replace("[URL]", email.URL.ToString()); } break; case "ForgetPassordEmail": { EmailText = ForgetPassordEmail.ToString(); EmailText = EmailText.Replace("[URL]", email.URL.ToString()); } break; } mailMsg.Body = EmailText; mailMsg.IsBodyHtml = true; smtpClient.Send(mailMsg); } catch (Exception ex) { SmtpException smtpEx = new SmtpException(ex.ToString()); new SqlLog().InsertSqlLog(0, "Emails.cs SendEmail", ex); return false; } finally { if (smtpClient != null) smtpClient = null; if (mailMsg != null) mailMsg = null; } return true; }
private void Complete(Exception exception, IAsyncResult result) { ContextAwareResult operationCompletedResult = (ContextAwareResult)result.AsyncState; if (GlobalLog.IsEnabled) { GlobalLog.Enter("SmtpClient#" + LoggingHash.HashString(this) + "::Complete"); } try { if (_cancelled) { //any exceptions were probably caused by cancellation, clear it. exception = null; Abort(); } // An individual failed recipient exception is benign, only abort here if ALL the recipients failed. else if (exception != null && (!(exception is SmtpFailedRecipientException) || ((SmtpFailedRecipientException)exception).fatal)) { if (GlobalLog.IsEnabled) { GlobalLog.Print("SmtpClient#" + LoggingHash.HashString(this) + "::Complete Exception: " + exception.ToString()); } Abort(); if (!(exception is SmtpException)) { exception = new SmtpException(SR.SmtpSendMailFailure, exception); } } else { if (_writer != null) { try { _writer.Close(); } // Close may result in a DataStopCommand and the server may return error codes at this time. catch (SmtpException se) { exception = se; } } _transport.ReleaseConnection(); } } finally { operationCompletedResult.InvokeCallback(exception); } if (GlobalLog.IsEnabled) { GlobalLog.Leave("SmtpClient#" + LoggingHash.HashString(this) + "::Complete"); } }
private static bool FormatSmtpException(StringBuilder text, SmtpException smtpEx) { if (smtpEx == null) return false; text.AppendFormat("SMTP Error : {0} - {1}", smtpEx.StatusCode, smtpEx.Message); return true; }
private void Complete(Exception exception, IAsyncResult result) { ContextAwareResult operationCompletedResult = (ContextAwareResult)result.AsyncState; if (NetEventSource.IsEnabled) NetEventSource.Enter(this); try { if (_cancelled) { //any exceptions were probably caused by cancellation, clear it. exception = null; Abort(); } // An individual failed recipient exception is benign, only abort here if ALL the recipients failed. else if (exception != null && (!(exception is SmtpFailedRecipientException) || ((SmtpFailedRecipientException)exception).fatal)) { if (NetEventSource.IsEnabled) NetEventSource.Error(this, exception); Abort(); if (!(exception is SmtpException)) { exception = new SmtpException(SR.SmtpSendMailFailure, exception); } } else { if (_writer != null) { try { _writer.Close(); } // Close may result in a DataStopCommand and the server may return error codes at this time. catch (SmtpException se) { exception = se; } } _transport.ReleaseConnection(); } } finally { operationCompletedResult.InvokeCallback(exception); } if (NetEventSource.IsEnabled) NetEventSource.Info(this, "Complete"); }
void IPscxErrorHandler.WriteSmtpSendMessageError(object target, SmtpException exc) { WriteError(new ErrorRecord(exc, "SmtpError", ErrorCategory.NotSpecified, target)); }
public void GetObjectData_SerializationInfo_Null () { SmtpException se = new SmtpException (); try { se.GetObjectData ((SerializationInfo) null, new StreamingContext ()); Assert.Fail ("#1"); } catch (ArgumentNullException ex) { Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); Assert.IsNull (ex.InnerException, "#3"); Assert.IsNotNull (ex.Message, "#4"); Assert.AreEqual ("info", ex.ParamName, "#5"); } }
private bool SendOneNotify(NotifyContent notifyContent, string logFileName) { lock (_sendObj) { //发送邮件 SmtpException smtpEx = new SmtpException(); MailUserToken userToken = new MailUserToken(notifyContent.MsgID, logFileName, notifyContent); List<string> receiver = new List<string>(); for (int i = 0; i < _emailConfig.ReceiveInfoList.Count; i++) { receiver.Add(_emailConfig.ReceiveInfoList[i].EmailAddr); } if (receiver.Count <= 0) { return false; } for (int i = 0; i < notifyContent.AttachmentFileNameList.Count; i++) { _mailSender.Attachments(notifyContent.AttachmentFileNameList[i]); } bool isSendSucceed = true; if (!_mailSender.SetMailMessageContext(notifyContent.MsgContent, notifyContent.MsgTitle, false)) { isSendSucceed = false; } else { if (!_mailSender.SendAsync(receiver, System.Net.Mail.MailPriority.Normal, ref smtpEx, userToken)) { isSendSucceed = false; } else { _bSending = true; } } if (_emailConfig.EnableJournal) { #region 写入日志 #endregion } return isSendSucceed; } }
public void GetObjectData () { string msg = "MESSAGE"; Exception inner = new ArgumentException ("whatever"); SerializationInfo si; SmtpException se; se = new SmtpException (msg, inner); si = new SerializationInfo (typeof (SmtpException), new FormatterConverter ()); se.GetObjectData (si, new StreamingContext ()); Assert.AreEqual (12, si.MemberCount, "#A1"); Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#A2"); Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#A3"); Assert.AreSame (msg, si.GetString ("Message"), "#A4"); Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#A5"); Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#A6"); Assert.IsNull (si.GetString ("StackTraceString"), "#A7"); Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#A8"); Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#A9"); Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#A10"); Assert.IsNull (si.GetString ("Source"), "#A11"); Assert.IsNull (si.GetString ("ExceptionMethod"), "#A12"); Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure, si.GetInt32 ("Status"), "#A13"); // attempt initialization of lazy init members Assert.IsNotNull (se.Data); Assert.IsNull (se.Source); Assert.IsNull (se.StackTrace); si = new SerializationInfo (typeof (SmtpException), new FormatterConverter ()); se.GetObjectData (si, new StreamingContext ()); Assert.AreEqual (12, si.MemberCount, "#B1"); Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#B2"); Assert.AreSame (se.Data, si.GetValue ("Data", typeof (IDictionary)), "#B3"); Assert.AreSame (msg, si.GetString ("Message"), "#B4"); Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#B5"); Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#B6"); Assert.IsNull (si.GetString ("StackTraceString"), "#B7"); Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#B8"); Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#B9"); Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#B10"); Assert.IsNull (si.GetString ("Source"), "#B11"); Assert.IsNull (si.GetString ("ExceptionMethod"), "#B12"); Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure, si.GetInt32 ("Status"), "#B13"); try { throw new SmtpException (msg, inner); } catch (SmtpException ex) { si = new SerializationInfo (typeof (SmtpException), new FormatterConverter ()); ex.GetObjectData (si, new StreamingContext ()); Assert.AreEqual (12, si.MemberCount, "#C1"); Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#C2"); Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#C3"); Assert.AreSame (msg, si.GetString ("Message"), "#C4"); Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#C5"); Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#C6"); Assert.IsNotNull (si.GetString ("StackTraceString"), "#C7"); Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#C8"); Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#C9"); Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#C10"); Assert.IsNotNull (si.GetString ("Source"), "#C11"); //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#C12"); Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure, si.GetInt32 ("Status"), "#C13"); } try { throw new SmtpException (msg, inner); } catch (SmtpException ex) { // force initialization of lazy init members Assert.IsNotNull (ex.Data); Assert.IsNotNull (ex.StackTrace); si = new SerializationInfo (typeof (SmtpException), new FormatterConverter ()); ex.GetObjectData (si, new StreamingContext ()); Assert.AreEqual (12, si.MemberCount, "#D1"); Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#D2"); Assert.AreSame (ex.Data, si.GetValue ("Data", typeof (IDictionary)), "#D3"); Assert.AreSame (msg, si.GetString ("Message"), "#D4"); Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#D5"); Assert.AreSame (ex.HelpLink, si.GetString ("HelpURL"), "#D6"); Assert.IsNotNull (si.GetString ("StackTraceString"), "#D7"); Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#D8"); Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#D9"); Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#D10"); Assert.AreEqual (typeof (SmtpExceptionTest).Assembly.GetName ().Name, si.GetString ("Source"), "#D11"); //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#D12"); Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure, si.GetInt32 ("Status"), "#D13"); } }
[Test] // .ctor (String, Exception) public void Constructor6 () { string msg = "MESSAGE"; Exception inner = new Exception (); SmtpException se; se = new SmtpException (msg, inner); Assert.IsNotNull (se.Data, "#A1"); Assert.AreEqual (0, se.Data.Keys.Count, "#A2"); Assert.AreSame (inner, se.InnerException, "#A3"); Assert.AreSame (msg, se.Message, "#A4"); Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#A5"); se = new SmtpException (msg, (Exception) null); Assert.IsNotNull (se.Data, "#B1"); Assert.AreEqual (0, se.Data.Keys.Count, "#B2"); Assert.IsNull (se.InnerException, "#B3"); Assert.AreSame (msg, se.Message, "#B4"); Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#B5"); se = new SmtpException ((string) null, inner); Assert.IsNotNull (se.Data, "#C1"); Assert.AreEqual (0, se.Data.Keys.Count, "#C2"); Assert.AreSame (inner, se.InnerException, "#C3"); Assert.IsNotNull (se.Message, "#C4"); Assert.AreEqual (new SmtpException ((string) null).Message, se.Message, "#C5"); Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#C6"); se = new SmtpException ((string) null, (Exception) null); Assert.IsNotNull (se.Data, "#D1"); Assert.AreEqual (0, se.Data.Keys.Count, "#D2"); Assert.IsNull (se.InnerException, "#D3"); Assert.IsNotNull (se.Message, "#D4"); Assert.AreEqual (new SmtpException ((string) null).Message, se.Message, "#D5"); Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#D6"); }