-
Notifications
You must be signed in to change notification settings - Fork 2
Mail Utilities
Mail code is something you will have to rewrite on almost every application you work on. I've found it to be much more productive to just stick this in my utilities library and reuse it.
public class Email : IMailDispatcher
{
private readonly Botmail _botmail;
public Email()
{
_botmail = new Botmail { Addresser = "[email protected]", Ssl = true };
}
public string Body { get; set; }
public ICollection<string> Recipients { get; set; } = new List<string>();
public string Subject { get; set; }
public async Task Send()
{
await Task.Factory.StartNew(
() =>
{
_botmail.Addressees = Recipients;
_botmail.Body = Body;
_botmail.Subject = Subject;
Mailbot.Instance.QueueMail(_botmail);
});
}
#endregion
public void AddRecipients(params string[] recipients)
{
recipients.ForEach(_recipients.Add);
}
public async Task Send(string subject, string body)
{
Subject = subject;
Body = body;
await Send();
}M
}
Mailbot
wants you to use an encrypted string in your web.config or app.config file for your SMTP passwords. Because storing cleartext
passwords in your config files is a bad idea. For documentation on encrypting your password see the [Encryption] page.
To configure your SMTP settings, use the following configuration:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Devlord.Utilities.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Devlord.Utilities.Properties.Settings>
<setting name="SmtpServer" serializeAs="String">
<value>email-smtp.us-east-1.amazonaws.com</value>
</setting>
<setting name="SmtpLogin" serializeAs="String">
<value>AKANOTREAL7KQR2OS</value>
</setting>
<setting name="SmtpPassword" serializeAs="String">
<value>k05VhhOHHBqFY6Yr8RzexvbQMMuxscp3pNotRealEitherLOL9UrQCie0++Vgdii8YYShyG71UPrVJLtiJJupJI0UHGqMGI=</value>
</setting>
<setting name="SmtpPort" serializeAs="String">
<value>587</value>
</setting>
</Devlord.Utilities.Properties.Settings>
One of the most fun features is the ability to throttle your mail sender in case you are worried about hitting limits and getting blacklisted. The default settings below are based on old numbers from Gmail/Google Apps, so you might need to do research to see which settings you would want to use.
MinuteThrottle = new MailThrottle { Interval = ThrottleInterval.Minute, Limit = 180 };
HourlyThrottle = new MailThrottle { Interval = ThrottleInterval.Hour, Limit = 3600 };
DailyThrottle = new MailThrottle { Interval = ThrottleInterval.Day, Limit = 10000 };
These settings are used by default in Mailbot.QueueMail()
.