简体   繁体   中英

Best way to send an email from a .NET application?

I'm working on a Windows Forms (.NET 3.5) application that has a built-in exception handler to catch any (heaven forbid) exceptions that may arise. I'd like the exception handler to be able to prompt the user to click a Send Error Report button, which would then cause the app to send an email to my FogBugz email address.

What's the best way to do this, and are there any "gotchas" to watch out for?

You shouldn't need to worry about client credentials and just use the SmtpClient as suggested by Esteban. You will need the user to provide a valid Smtp server url at configuration, but most ISPs allow anonymous smtp providing you are on their network (one of their clients) - as long as the user puts in the url for their ISPs smptp server then most people wouldn't have any problems.

Note: There is a predefined section of the .config file for storing the configuration options for the SmtpClient object. If you put the settings in there you don't have to explicitly set anything in you code when sending an email. An example of the section is below:

<system.net>
   <mailSettings>
      <smtp deliveryMethod="Network" from="stuff@somewhere.com.au">
         <network host="smtp.somewhere.com.au" />
      </smtp>
   </mailSettings>
</system.net>

The user name and password are optional. Intellisense works for these parts of the config file.

Edit: slight correction to my code example.

You'll want to use the SmtpClient class as outlined here .
There are no gotchas - sending email is about as easy as it gets.

In a controlled environment, using SmtpClient would be the answer. But on a user's machine you would need an SMTP server to send through.

You could prompt the user for their SMTP credentials, but I think that would be impractical for your case. As a user, I would not want to provide my SMTP credentials to a random app (think SPAM). You also don't want to hard-code your own SMTP credentials into the app, it would be trivial for a malicious user to sniff those credentials and use your server to send SPAM.

Ideally you would be able to use the user's mail agent to send the email. I was thinking you might be able to formulate and execute a mailto: URL, but I'm not sure if you'd be able to specify the body or any attachments for the message.

You mentioned you're using Fogbugz.

Try http://www.fogcreek.com/FogBugz/docs/60/topics/customers/BugzScout.html?isl=59722 or http://www.fogcreek.com/FogBugz/blog/post/C-wrapper-for-the-FogBugz-API.aspx?isl=59722

There is some sample code around, I think in your FB install directory. I checked with Michael Pryor re: licensing and he said it was fine to use their code, but YMMV, so I'd check.

It provides a good starting point.

您可能还想查看第三方aspNetEmail库,它提供了许多有用的功能,可以在System.Net.Mail上面提供。

You'll want to use the SmtpClient class as outlined here . There are no gotchas - sending email is about as easy as it gets.

An extensive System.Net.Mail FAQ is located here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM