<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Blazor Experiments</title>
    <link href="https://blazorcode.uk/codecheck/feed.xml" rel="self" />
    <link href="https://blazorcode.uk/codecheck" />
    <updated>2022-03-22T11:14:22+00:00</updated>
    <author>
        <name>CJB</name>
    </author>
    <id>https://blazorcode.uk/codecheck</id>

    <entry>
        <title>Code Check</title>
        <author>
            <name>CJB</name>
        </author>
        <link href="https://blazorcode.uk/codecheck/code-check.html"/>
        <id>https://blazorcode.uk/codecheck/code-check.html</id>

        <updated>2022-03-22T11:14:22+00:00</updated>
            <summary>
                <![CDATA[
                    Create a Test Page Before attempting to send emails automatically, we will test the basic email setup using a separate page, with hard-coded credentials. using MailKit.Net.Smtp; using MailKit.Security; using Microsoft.Extensions.Configuration; using MimeKit; using MimeKit.Text; using System; namespace BlazorBirthdayReminders.Data; public interface IEmailService { bool Send(string to,&hellip;
                ]]>
            </summary>
        <content type="html">
            <![CDATA[
                
    <h2 id="create-a-test-page">
      Create a Test Page
    </h2>

  <p>
    Before attempting to send emails automatically, we will test the basic email setup using a separate page, with hard-coded credentials.
  </p>
<pre class="line-numbers  language-csharp"><code>using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Configuration;
using MimeKit;
using MimeKit.Text;
using System;


namespace BlazorBirthdayReminders.Data;

    public interface IEmailService
    {
        bool Send(string to, string subject, string body);
    }

    public class EmailService : IEmailService
    {
        private readonly IConfiguration configuration;

        public EmailService(IConfiguration Configuration)
        {
            configuration = Configuration;
        }

        public bool Send(string to, string subject, string body)
        {
            //Get settings from appsettings
            var SmtpHost = configuration["SmtpHost"];
            var SmtpPort = configuration["SmtpPort"];
            var SmtpUserFriendlyName = configuration["SmtpUserFriendlyName"];
            var SmtpUserEmailAddress = configuration["SmtpUserEmailAddress"];
            var SmtpPass = configuration["SmtpPass"];
            // create message
            var email = new MimeMessage();
            email.From.Add(new MailboxAddress(SmtpUserFriendlyName, SmtpUserEmailAddress));
            email.To.Add(new MailboxAddress(to, to));
            email.Subject = subject;
            email.Body = new TextPart(TextFormat.Html) { Text = body };

            try
            {
                // send email
                using var smtp = new SmtpClient();

                smtp.Connect(SmtpHost, Int32.Parse(SmtpPort), SecureSocketOptions.StartTls);
                smtp.Authenticate(SmtpUserEmailAddress, SmtpPass);
                smtp.Send(email);
                smtp.Disconnect(true);

                return true;
            }
            catch
            {
                return false;
            }

        }
    }



</code></pre>
            ]]>
        </content>
    </entry>
</feed>
