Join us
@sofiatarhonska ă» Sep 22,2022 ă» 5 min read ă» 2163 views ă» Originally posted on mailtrap.io
Youâve got a long ASP.NET project ahead. Youâre trying to wrap your head around all tasks at hand â infrastructure, business logic, admin panel, integrations. On top of that, thereâs a long list of âcould haveâ type of features that the team would like to implement if time and resources allow.
One of them is adding the ability to send an email in ASP.NET C# that youâve been postponing for a while. After all, itâs such an obvious feature that might as well be left for the very end, when almost everything is up and running. Before your project goes live, you will need to validate your email workflows anyway and probably you donât want to stay extra hours just before the launch to do so.
While sending emails with C# is not rocket science, we strongly recommend thinking about it sooner rather than later. To make it easier to start, weâve covered the first steps with the various code samples. Letâs start!
Throughout the course of this article, weâll often be using MailMessage
class. Itâs a part of System.Net.Mail
namespace and is used to create email messages that are then sent to an SMTP server. The delivery is then taken care of by the SmtpClient
class.
For the complete set of parameters of this class, please refer to Microsoftâs documentation.
This one is fairly easy and quick to set up as SMTP (Simple Mail Transfer Protocol) is the most common communication standard used in email transmission. In the example below, weâll show how to send a very simple email with the following details:
To: elizabeth@westminster.co.uk
From: piotr@mailtrap.io
Title: Good morning, Elizabeth
Body: Elizabeth, Long time no talk. Would you be up for lunch in Soho on Monday? Iâm paying.
In order to send such an email, weâll use the aforementioned MailMessage
class from .NET API.
Once the message was configured, we connected to the SMTP server and sent it this way!
Mailtrap Email Delivery also allows you to send emails via SMTP. Just verify your domain, copy the provided SMTP settings to your app or service, and start sending to your recipients.
Now that we know how to send basic emails, letâs consider a very common scenario and add an attachment to our message to the Queen of England. This could be an invoice for the last royal wedding or a set of pictures we took on that fabulous weekend.
On top of MailMessage
class, weâll use Attachment class from .NET API. Make sure you add the attachment to the current working directory first.
If you need to send multiple invoices (lucky you!) or other files, simply use a loop and add the other files to the email.
Weâve just covered sending emails with attachments. But what if youâre sending images but want them displayed inline rather than being attached to an email. Certainly, it will be easier to attract the Queenâs attention this way. With a bit of modification of our previous code, this can be easily achieved.
When trying to send an email with images in C#, youâre not going to use a typical <img src="" alt="" />
construction. It would add an image attachment to the email and thatâs the last thing we need right now. Instead, weâll use LinkedResource
object to directly embed an image in the HTML version of our message to the Queen. We will then follow our regular approach with MailMessage
class. As always, remember to upload the file first to your current working directory. If itâs missing, an email will be sent without any attachment and, really, arenât we receiving these âoops, forgot about the attachment!â emails too often already?
Letâs say we want to use the opportunity and remind the Queen and her grandson about unpaid invoices.
No rocket science here either. New recipients are simply added to the code, separated by a comma. As in the example below:
Things get a tiny bit more tricky when you want to add people in bcc and cc, weâll need a few additional lines:
Voila!
Letâs consider a situation when you donât have (or donât want to have) an SMTP server configured but still want to send an email in C#.
The first option is to do a DNS MX lookup of the email address that youâre trying to send an email to. This allows you to figure out what the SMTP server is and connect to it. So, youâre still going to use an SMTP server, just not yours!
We donât, however, recommend such an approach for several reasons:
In order to implement it, youâll need to install a 3rd party plugin. DnsPlugin.NET is our choice here. See the sample code:
A much better approach to sending emails without SMTP is with API. Itâs definitely the fastest one. It also provides an additional layer of security by utilizing API keys and allows you to use many 3rd party providers that give you tons of additional features such as analytics, email authentication, and others.
Mailtrap Email Delivery is one such service, and you get exemplary APIs for nine programming languages. More importantly, we provide comprehensive stats and deliverability alerts to help maintain your domain authority.
Finally, you might be wondering if thereâs a way to receive the emails directly with the C# code. After all, the Queen might get back to us any minute now. Of course, you could easily track new emails with simple email clients, such as Gmail or Thunderbird, without a single line of code and with minimal effort. But letâs assume you need to see at least the topics of incoming emails with your code, for example, to build some integrations around that.
Receiving emails is not a part of core C# stack so you will need to utilize some 3rd party libraries for that purpose. OpenPop.NET open-source library seems to be getting the job done in this case. You can use the following code to fetch the topics of incoming mail:
For the details on using OpenPop library for this or any other purposes, please refer to their documentation.
Try it out when you launch your first project but consider some more robust solutions if you plan to scale. If you need to do some email testing, the last thing you want is users receiving the test emails they were not supposed to get.
Testing out of the way, feel free to check out Mailtrapâs Email Delivery for free and ensure your transactional emails reach the right recipients
I hope you learned useful things by reading our guide on how to send HTML email using ASP.NET C#, which was originally published in the Mailtrap Blog by Piotr Malek.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.