Exemplo n.º 1
0
		public async Task SendEmailAsync(
			SmtpOptions smtpOptions,
			string from,
			string to,
			string subject,
			string body,
			bool htmlBody = true,
			string fromAlias = null,
			string toAlias = null,
			string replyTo = null)
		{
			ValidateEmail(from, to, subject);

			MimeMessage emailMessage = BuildMimeMessage(from, fromAlias, to, toAlias, subject, body, htmlBody, replyTo);

			await SendEmailAsync(emailMessage, smtpOptions);
		}
Exemplo n.º 2
0
		private async Task SendEmailAsync(MimeMessage emailMessage, SmtpOptions smtpOption)
		{
			using (var client = new SmtpClient())
			{
				await client.ConnectAsync(smtpOption.Server, smtpOption.Port, smtpOption.UseSsl)
					.ConfigureAwait(false);

				// Note: since we don't have an OAuth2 token, disable the XOAUTH2 authentication mechanism.
				client.AuthenticationMechanisms.Remove("XOAUTH2");

				// Note: only needed if the SMTP server requires authentication.
				if (smtpOption.RequiresAuthentication)
				{
					await client.AuthenticateAsync(smtpOption.User, smtpOption.Password)
						.ConfigureAwait(false);
				}

				await client.SendAsync(emailMessage).ConfigureAwait(false);
				await client.DisconnectAsync(true).ConfigureAwait(false);
			}
		}