Skip to content

Commit 3621dea

Browse files
committed
SMTP show supplied error message on failure
1 parent c776404 commit 3621dea

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/ExceptionReporter/ExceptionReportPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void SendSmtpMail()
134134
try
135135
{
136136
var mailSender = new MailSender(ReportInfo);
137-
mailSender.SendSmtp(emailText, _view.SetEmailCompletedState);
137+
mailSender.SendSmtp(emailText, _view);
138138
}
139139
catch (Exception exception)
140140
{

src/ExceptionReporter/Mail/MailSender.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class MailSender
1515
public delegate void CompletedMethodDelegate(bool success);
1616
private readonly ExceptionReportInfo _reportInfo;
1717
private AttachAdapter _attacher;
18+
private IExceptionReportView _view;
1819

1920
internal MailSender(ExceptionReportInfo reportInfo)
2021
{
@@ -24,8 +25,9 @@ internal MailSender(ExceptionReportInfo reportInfo)
2425
/// <summary>
2526
/// Send SMTP email
2627
/// </summary>
27-
public void SendSmtp(string exceptionReport, CompletedMethodDelegate setEmailCompletedState)
28+
public void SendSmtp(string exceptionReport, IExceptionReportView view)
2829
{
30+
_view = view;
2931
var smtpClient = new SmtpClient(_reportInfo.SmtpServer)
3032
{
3133
DeliveryMethod = SmtpDeliveryMethod.Network,
@@ -37,10 +39,23 @@ public void SendSmtp(string exceptionReport, CompletedMethodDelegate setEmailCom
3739

3840
var mailMessage = CreateMailMessage(exceptionReport);
3941

40-
smtpClient.SendCompleted += delegate { setEmailCompletedState.Invoke(true); };
42+
smtpClient.SendCompleted += SmtpClient_SendCompleted;
4143
smtpClient.SendAsync(mailMessage, "Exception Report");
4244
}
4345

46+
private void SmtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
47+
{
48+
if (e.Error != null)
49+
{
50+
_view.SetEmailCompletedState(true);
51+
_view.ShowErrorDialog(e.Error.Message, e.Error);
52+
}
53+
else
54+
{
55+
_view.SetEmailCompletedState(false);
56+
}
57+
}
58+
4459
private MailMessage CreateMailMessage(string exceptionReport)
4560
{
4661
var mailMessage = new MailMessage(_reportInfo.SmtpFromAddress, _reportInfo.EmailReportAddress)

src/WinFormsDemoApp/DemoAppView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ private static void Throw_CustomMessage_Click(object sender, EventArgs e)
5454

5555
//--- smtp
5656
// exceptionReporter.Config.MailMethod = ExceptionReportInfo.EmailMethod.SMTP;
57-
// exceptionReporter.Config.SmtpPort = 465;
57+
// exceptionReporter.Config.SmtpPort = 587;
5858
// exceptionReporter.Config.SmtpServer= "smtp.gmail.com";
5959
// exceptionReporter.Config.SmtpUsername = "<user>@gmail.com";
6060
// exceptionReporter.Config.SmtpUseSsl = true;
6161
// exceptionReporter.Config.SmtpPassword = "<password>";
62-
// exceptionReporter.Config.SmtpFromAddress = "user@exceptionreport.com";
62+
// exceptionReporter.Config.SmtpFromAddress = "<user>@gmail.com";
6363
//
6464
// exceptionReporter.Config.EmailReportAddress = "<user>@gmail.com";
6565
//----

0 commit comments

Comments
 (0)