We would like to give special thanks to Wade, one of our valued customers, for providing a great example of how to send emails using MailKit.Net.Smtp and MimeKit. Mailkit is a cross-platform .NET library for IMAP, POP3, and SMTP. This example demonstrates a simple implementation for sending emails using SmarterMail SMTP servers and highlights important tips for ensuring successful email delivery.
Overview
This code snippet uses the MailKit and MimeKit libraries to send emails with plain text and HTML bodies. The example includes handling for SMTP authentication and SSL connections, making it ideal for use with SmarterMail Email Servers hosted on Adaptive Web Hosting.
Key Points:
- Ensure you fill the
Sender
field in theMimeMessage
object. If left empty, the email might appear to send but will never actually be delivered. - Proper DKIM setup in your DNS records is crucial for successful email delivery to major providers like Google Mail (Gmail) and others.
- Use port 465 with SSL enabled when connecting to the SmarterMail SMTP server.
Usage Notes
-
Connecting to the SMTP Server: Replace
"XXXXXX-XXXXXXX.adaptivewebhosting.com"
with the specific SMTP address provided for your SmarterMail account. -
Authentication: Replace
"MailboxMailAddress"
and"MailBoxPassword"
with your actual SmarterMail email address and password. -
Handling Exceptions: The
catch
block logs any exceptions encountered during the process. You can modify this to integrate with your application’s error logging system. -
DKIM Configuration: Ensure that your domain’s DNS records include the necessary DKIM records to improve deliverability and avoid being marked as spam by major email providers.
Code Example
using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Utils;
public bool SendMailwMailKit(string fromName, string fromEmail, string toEmail, string subject, string textbody, string htmlbody)
{
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress(fromName, fromEmail));
message.To.Add(MailboxAddress.Parse(toEmail));
message.Sender = new MailboxAddress(fromName, fromEmail);
message.Subject = subject;
var builder = new BodyBuilder
{
TextBody = textbody,
HtmlBody = htmlbody
};
message.Body = builder.ToMessageBody();
using (SmtpClient client = new SmtpClient())
{
try
{
// Replace XXXXXX-XXXXXXX.adaptivewebhosting.com with your assigned SmarterMail SMTP server address
client.Connect("XXXXXX-XXXXXXX.adaptivewebhosting.com", 465, true);
// Replace "MailboxMailAddress" and "MailBoxPassword" with your actual SmarterMail credentials
client.Authenticate("MailboxMailAddress", "MailBoxPassword");
string svrespons = client.Send(message); // Send email
return svrespons == "OK";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
finally
{
client.Disconnect(true);
client.Dispose();
}
}
}
- Multiple Recipients: You can add multiple recipients by using
message.To.Add(new MailboxAddress(...))
multiple times. - Attachments: Use
builder.Attachments.Add()
to add files to the email. - CC and BCC Support: Add CC and BCC recipients using
message.Cc.Add()
andmessage.Bcc.Add()
respectively.
Conclusion
This example provides a solid foundation for sending emails using MailKit and SmarterMail. By following Wade’s implementation and the provided tips, you can easily integrate reliable email functionality into your ASP.NET or .NET Core applications.