Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

email

vb.net

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?

like image 341
Matt Hanson Avatar asked Sep 11 '08 23:09

Matt Hanson


People also ask

Can .net send email?

NET and . NET Core come with built-in support for sending emails through the System. Net.Mail namespace. While it might seem like the easy choice, you will need an SMTP server for this to work.

What is the code to send an email from an ASP NET application?

Net.Mail; SmtpClient smtp = new SmtpClient("smtp.mailtrap.io", 2525); smtp. EnableSsl = true; smtp. Credentials = new NetworkCredential("SMTP_USERNAME", "SMTP_PASSWORD"); smtp.

Can C# send an email?

The standard approach to send an email using C# is SMTP (Simple Mail Transfer Protocol). It is a network protocol used to send emails over the internet. Additionally, it allows you to relayemails across multiple networks.


3 Answers

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="[email protected]">
         <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.

like image 92
Dr8k Avatar answered Oct 06 '22 00:10

Dr8k


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

like image 32
Esteban Araya Avatar answered Oct 06 '22 01:10

Esteban Araya


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.

like image 39
Brannon Avatar answered Oct 06 '22 01:10

Brannon