This project has moved and is read-only. For the latest updates, please go here.

Email

Nov 23, 2008 at 7:13 PM
The email configuration doesn't allow me to specify a port.
For example if I want to send a email throw Gmail I can´t do that with the following configuration. Gmail uses smtp port 587 and that can´t be specified in the configuration I think it should be used something like this:

  <system.net>
    <mailSettings>
      <smtp from="xxx@gmail.com" deliveryMethod="Network">
        <network host="smtp.gmail.com" port="587" userName="xxx@gmail.com" password="123"></network>
      </smtp>
    </mailSettings>
  </system.net>

Using this way i can specify smtp port and it's the standard email configuration in .Net.
Nov 23, 2008 at 7:47 PM
ok I thought that you aren't supporting this configuration

  <system.net>
    <mailSettings>
      <smtp from="xxx@gmail.com" deliveryMethod="Network">
        <network host="smtp.gmail.com" port="587" userName="xxx@gmail.com" password="123"></network>
      </smtp>
    </mailSettings>
  </system.net>

but after examining the code I saw that you support SMTP and I can use the previous configuration, so I was wrong.
What you are not supporting is the flag EnableSSL that exists in type SmtpClient. This flag can't be set in the configuration and it has to be specified explicitly in the code.

can you change your email configuration to support EnableSSL and in the code you will have to write something like this:

        <Email>
            .....
            <add key="EnableSSL" value="true"/>
        </Email>

public void SendSmtp(string exceptionString, CompletedMethodDelegate setEmailCompletedState)
        {
            var smtpClient = new SmtpClient(_reportInfo.SmtpServer) { DeliveryMethod = SmtpDeliveryMethod.Network };

            if(_reportInfo.EnableSSL)
            {
                    smtpClient.EnableSsl = true;
            }
            MailMessage mailMessage = CreateMailMessage(exceptionString);

            smtpClient.SendCompleted += delegate { setEmailCompletedState.Invoke(true); };
            smtpClient.SendAsync(mailMessage, null);
        }


Nov 23, 2008 at 8:12 PM
continuing with the email.
In method MailMessage CreateMailMessage(string exceptionString) that is used by SMTP
you use mailMessage.To.Add(new MailAddress(_reportInfo.ContactEmail));

and in method SendMapi(string exceptionString, IntPtr windowHandle) that is used by MAPI
you use string emailAddress = (string.IsNullOrEmpty(_reportInfo.EmailReportAddress))
                                      ? _reportInfo.ContactEmail
                                      : _reportInfo.EmailReportAddress;

            mapi.AddRecipient(emailAddress, null, false);

why don't you used the same thing for SMTP??
why don't you do:

string emailAddress = (string.IsNullOrEmpty(_reportInfo.EmailReportAddress))
                                      ? _reportInfo.ContactEmail
                                      : _reportInfo.EmailReportAddress;

mailMessage.To.Add(new MailAddress(emailAddress));

this way it's the same for both methods SMTP and MAPI

Thanks

Nov 24, 2008 at 1:32 PM
Edited Nov 24, 2008 at 1:39 PM
Hi oeN,

Good question, I had to think a while about the email address - which means the code should be clearer.

It's because the default is SimpleMAPI, and config is not compulsory, so an email address may easily be null - so this condition protects the email from not being set.

However, if you explicitly change the default email to "SMTP" and then forget to set an email address... (which is what this condition is catering for) - that's quite a seriously dumb mistake -hence we don't cater for it in the same way.

That's the original logic behind the decision, however, I'm open to changing it in any way that will make things clearer.