<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>asp net core send email &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/asp-net-core-send-email/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>Help you to find best ASP.NET Core Hosting</description>
	<lastBuildDate>Tue, 15 Dec 2020 04:30:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>asp net core send email &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Using Mailkit to Send Email ASP.NET Core 3.1</title>
		<link>https://topreviewhostingasp.net/using-mailkit-to-send-email-asp-net-core-3-1/</link>
					<comments>https://topreviewhostingasp.net/using-mailkit-to-send-email-asp-net-core-3-1/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Tue, 15 Dec 2020 04:23:53 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp net core send email]]></category>
		<category><![CDATA[asp.net core tips]]></category>
		<category><![CDATA[asp.net core tutorial]]></category>
		<category><![CDATA[asp.net tips]]></category>
		<category><![CDATA[mailkit asp net core]]></category>
		<category><![CDATA[using mailkit]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=2829</guid>

					<description><![CDATA[This post is an example how to send email in Asp.net Core 3.1 using Mailkit. I assume that you have downloaded it and install it on your server. How to Send HTML Email in ASP.NET Core This code sends a simple HTML email using the Ethereal free SMTP testing service, you can create a free test account [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>This post is an example how to send email in Asp.net Core 3.1 using <a href="https://www.nuget.org/packages/MailKit/">Mailkit</a>. I assume that you have downloaded it and install it on your server.</p>



<h2 class="wp-block-heading">How to Send HTML Email in ASP.NET Core</h2>



<p>This code sends a simple HTML email using the <em>Ethereal</em> free SMTP testing service, you can create a free test account in one click at <a href="https://ethereal.email/" target="_blank" rel="noreferrer noopener">https://ethereal.email/</a> and copy the username and password from below the title <em>SMTP configuration</em>. See instructions below for using different SMTP providers such as Gmail and Hotmail.</p>



<pre class="wp-block-code"><code>// create email message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse("from_address@example.com"));
email.To.Add(MailboxAddress.Parse("to_address@example.com"));
email.Subject = "Test Email Subject";
email.Body = new TextPart(TextFormat.Html) { Text = "&lt;h1&gt;Example HTML Message Body&lt;/h1&gt;" };

// send email
using var smtp = new SmtpClient();
smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
smtp.Authenticate("[USERNAME]", "[PASSWORD]");
smtp.Send(email);
smtp.Disconnect(true);</code></pre>



<h2 class="wp-block-heading">How to Send Plain Text Email in ASP.NET Core</h2>



<p>This code sends the same email as above with a plain text body.</p>



<pre class="wp-block-code"><code>// create email message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse("from_address@example.com"));
email.To.Add(MailboxAddress.Parse("to_address@example.com"));
email.Subject = "Test Email Subject";
email.Body = new TextPart(TextFormat.Plain) { Text = "Example Plain Text Message Body" };

// send email
using var smtp = new SmtpClient();
smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
smtp.Authenticate("[USERNAME]", "[PASSWORD]");
smtp.Send(email);
smtp.Disconnect(true);</code></pre>



<h2 class="wp-block-heading">Change Your SMTP Provider</h2>



<p>To change the above code to use a different email provider simply update the host parameter (the first parameter) passed to the <code>smtp.Connect()</code> method, for example:</p>



<pre class="wp-block-code"><code>// gmail
smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);

// hotmail
smtp.Connect("smtp.live.com", 587, SecureSocketOptions.StartTls);

// office 365
smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);</code></pre>



<h2 class="wp-block-heading">Wrapping it up in an Email Service</h2>



<p>To encapsulate the email sending functionality and make it easy to send email from anywhere in your ASP.NET Core application you can create an <code>EmailService</code> class and <code>IEmailService</code> interface like below.</p>



<pre class="wp-block-code"><code>using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Options;
using MimeKit;
using MimeKit.Text;
using WebApi.Helpers;

namespace WebApi.Services
{
    public interface IEmailService
    {
        void Send(string from, string to, string subject, string html);
    }

    public class EmailService : IEmailService
    {
        private readonly AppSettings _appSettings;

        public EmailService(IOptions&lt;AppSettings&gt; appSettings)
        {
            _appSettings = appSettings.Value;
        }

        public void Send(string from, string to, string subject, string html)
        {
            // create message
            var email = new MimeMessage();
            email.From.Add(MailboxAddress.Parse(from));
            email.To.Add(MailboxAddress.Parse(to));
            email.Subject = subject;
            email.Body = new TextPart(TextFormat.Html) { Text = html };

            // send email
            using var smtp = new SmtpClient();
            smtp.Connect(_appSettings.SmtpHost, _appSettings.SmtpPort, SecureSocketOptions.StartTls);
            smtp.Authenticate(_appSettings.SmtpUser, _appSettings.SmtpPass);
            smtp.Send(email);
            smtp.Disconnect(true);
        }
    }
}</code></pre>



<h2 class="wp-block-heading">Conclusion</h2>



<p>I hope above tutorial can help you if you encounter issue when send email via your Asp.net Core application. See you on next tips!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/using-mailkit-to-send-email-asp-net-core-3-1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
