.net core 企业qq邮件发送配置
本文是在ABP 2.1下使用:
1:接受和发送者配置:
using Abp;
using Abp.Configuration;
using Abp.Localization;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace BangFaoffsite.Otd
{
public static class AppSettings
{
/// <summary>
/// SMTP related email settings.
/// </summary>
public static class Smtp
{
#region 发送者
/// <summary>
/// Abp.Net.Mail.DefaultFromAddress 发件人的电子邮件地址 1632694570@qq.com tianxujun@tyemckjwwgc.onexmail.com
/// </summary>
public const string DefaultAddress = "1632694570@qq.com";
/// <summary>
/// 授权码【个人】
/// </summary>
public const string smurdizujnjgcacj = "smurdizujnjgcacj";
/// <summary>
/// Abp.Net.Mail.DefaultFromDisplayName 用作发件人的显示名称( 太原亚蟒蛇科技有限公司
/// </summary>
public const string DefaultDisplayName = "磅管家科技";
/// <summary>
/// Abp.Net.Mail.Smtp.Host SMTP服务器的IP /域(默认值:127.0.0.1)。 smtp.exmail.qq.com
/// </summary>
public const string Host = "smtp.qq.com";
/// <summary>
/// Abp.Net.Mail.Smtp.Port SMTP服务器的端口(默认值:25)。
/// </summary>
public const string Port = "25";
/// <summary>
/// Abp.Net.Mail.Smtp.UserName 用户名,如果SMTP服务器需要身份验证 tianxujun@tyemckjwwgc.onexmail.com 123Txj 1632694570@qq.com 5211314tianxjun
/// </summary>
public const string UserName = "1632694570@qq.com ";
/// <summary>
/// Abp.Net.Mail.Smtp.Password 密码,如果SMTP服务器需要身份验证
/// </summary>
public const string Password = "123";
/// <summary>
/// Abp.Net.Mail.Smtp.Domain 用户名的域(如果SMTP服务器需要身份验证) 【企业】 tyemckjwwgc.onexmail.com
/// </summary>
public const string Domain = "mail.qq.com";
/// <summary>
/// Abp.Net.Mail.Smtp.EnableSsl 一个值,该值指示SMTP服务器是否使用SSL(“ true”或“ false”。默认值:“ false”)。
/// </summary>
public const string EnableSsl = "false";
/// <summary>
/// Abp.Net.Mail.Smtp.UseDefaultCredentials 如果为true,则使用默认凭据而不是提供的用户名和密码(“ true”或“ false”。默认值:“ true”)。
/// </summary>
public const string UseDefaultCredentials = "true";
#endregion
#region 接受者
/// <summary>
/// 接受者地址
/// </summary>
public const string RecipientAddress = "953706194@qq.com";
/// <summary>
/// 显示名称
/// </summary>
public const string RecipientderDefaultDisplayName = "雷荣";
#endregion
}
}
public class AppSettingProvider : SettingProvider
{
public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
{
var data = new[]
{
new SettingDefinition(AppSettings.Smtp.Host, AppSettings.Smtp.Host, L("SmtpHost"),
scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.Port, AppSettings.Smtp.Port, L("SmtpPort"),
scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.UserName, AppSettings.Smtp.UserName, L("Username"),
scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.Password, AppSettings.Smtp.Password, L("Password"),
scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.Domain, AppSettings.Smtp.Domain, L("DomainName"),
scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.EnableSsl, AppSettings.Smtp.EnableSsl, L("UseSSL"),
scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.UseDefaultCredentials, AppSettings.Smtp.UseDefaultCredentials, L("UseDefaultCredentials"),scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.RecipientAddress, AppSettings.Smtp.RecipientAddress, L("RecipientAddress"),scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.RecipientderDefaultDisplayName, AppSettings.Smtp.RecipientderDefaultDisplayName, L("RecipientderDefaultDisplayName"),scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.smurdizujnjgcacj, AppSettings.Smtp.smurdizujnjgcacj, L("smurdizujnjgcacj"),scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.DefaultAddress, AppSettings.Smtp.DefaultAddress,
L("DefaultAddress"), scopes: SettingScopes.Application | SettingScopes.Tenant),
new SettingDefinition(AppSettings.Smtp.DefaultDisplayName, AppSettings.Smtp.DefaultDisplayName,L("DefaultDisplayName"), scopes: SettingScopes.Application | SettingScopes.Tenant)
};
return data;
}
private static LocalizableString L(string name)
{
return new LocalizableString(name, AbpConsts.LocalizationSourceName);
}
}
}
2:新建配置接口与实现
using System;
using System.Collections.Generic;
using System.Text;
namespace BangFaoffsite.IManager
{
public interface IUserEmailSenderConfiguration
{
string Host { get; }
int Port { get; }
string UserName { get; }
string Password { get; }
string Domain { get; }
bool EnableSsl { get; }
bool UseDefaultCredentials { get; }
string DefaultAddress { get; }
string DefaultDisplayName { get; }
string Smurdizujnjgcacj { get; }
string RecipientAddress { get; }
string RecipientderDefaultDisplayName { get; }
}
}
2》:实现
using Abp;
using Abp.Configuration;
using Abp.Dependency;
using Abp.Domain.Services;
using Abp.Extensions;
using BangFaoffsite.IManager;
using BangFaoffsite.Otd;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace BangFaoffsite.Manager
{
public class UserEmailSenderConfiguration : DomainService, IUserEmailSenderConfiguration, ITransientDependency
{
/// <summary>
/// Gets a setting value by checking. Throws <see cref="AbpException"/> if it's null or empty.
/// </summary>
/// <param name="name">Name of the setting</param>
/// <returns>Value of the setting</returns>
protected string GetNotEmptySettingValue(string name)
{
var value = SettingManager.GetSettingValue(name);
if (value.IsNullOrEmpty())
{
throw new AbpException(String.Format("Setting value for '{0}' is null or empty!", name));
}
return value;
}
/// <summary>
/// SMTP Host name/IP.
/// </summary>
public string Host
{
get { return GetNotEmptySettingValue(AppSettings.Smtp.Host); }
}
/// <summary>
/// SMTP Port.
/// </summary>
public int Port
{
get { return SettingManager.GetSettingValue<int>(AppSettings.Smtp.Port); }
}
/// <summary>
/// User name to login to SMTP server.
/// </summary>
public string UserName
{
get { return GetNotEmptySettingValue(AppSettings.Smtp.UserName); }
}
/// <summary>
/// Password to login to SMTP server.
/// </summary>
public string Password
{
get { return GetNotEmptySettingValue(AppSettings.Smtp.Password); }
}
/// <summary>
/// Domain name to login to SMTP server.
/// </summary>
public string Domain
{
get { return SettingManager.GetSettingValue(AppSettings.Smtp.Domain); }
}
/// <summary>
/// Is SSL enabled?
/// </summary>
public bool EnableSsl
{
get { return SettingManager.GetSettingValue<bool>(AppSettings.Smtp.EnableSsl); }
}
/// <summary>
/// Use default credentials?
/// </summary>
public bool UseDefaultCredentials
{
get { return SettingManager.GetSettingValue<bool>(AppSettings.Smtp.UseDefaultCredentials); }
}
public string DefaultAddress
{
get { return GetNotEmptySettingValue(AppSettings.Smtp.DefaultAddress); }
}
public string Smurdizujnjgcacj
{
get { return GetNotEmptySettingValue(AppSettings.Smtp.smurdizujnjgcacj); }
}
public string DefaultDisplayName
{
get { return SettingManager.GetSettingValue(AppSettings.Smtp.DefaultDisplayName); }
}
public string RecipientAddress
{
get { return SettingManager.GetSettingValue(AppSettings.Smtp.RecipientAddress); }
}
public string RecipientderDefaultDisplayName
{
get { return SettingManager.GetSettingValue(AppSettings.Smtp.RecipientderDefaultDisplayName); }
}
}
}
3:新建发送服务接口与实现:
using Abp.Domain.Services;
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace BangFaoffsite.IManager
{
public interface IUserSmtpEmailSender : IDomainService
{
Task SendAsync(string to, string subject, string body, bool isBodyHtml = true);
void Send(string to, string subject, string body, bool isBodyHtml = true);
Task SendAsync(string from, string to, string subject, string body, bool isBodyHtml = true);
void Send(string from, string to, string subject, string body, bool isBodyHtml = true);
Task SendAsync(MailMessage mail, bool normalize = true);
void Send(MailMessage mail, bool normalize = true);
Task InitMessageAsync(string body, string subject, bool isBodyHtml = true, bool normalize = true);
}
}
2》:实现
using Abp.Dependency;
using Abp.Domain.Services;
using Abp.Extensions;
using BangFaoffsite.IManager;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace BangFaoffsite.Manager
{
public class UserSmtpEmailSender : DomainService, IUserSmtpEmailSender, ITransientDependency
{
private readonly IUserEmailSenderConfiguration _configuration;
public UserSmtpEmailSender(IUserEmailSenderConfiguration configuration)
{
_configuration = configuration;
}
public async Task SendAsync(string to, string subject, string body, bool isBodyHtml = true)
{
await SendAsync(_configuration.DefaultAddress, to, subject, body, isBodyHtml);
}
public void Send(string to, string subject, string body, bool isBodyHtml = true)
{
Send(_configuration.DefaultAddress, to, subject, body, isBodyHtml);
}
public async Task SendAsync(string from, string to, string subject, string body, bool isBodyHtml = true)
{
await SendAsync(new MailMessage(from, to, subject, body) { IsBodyHtml = isBodyHtml });
}
public void Send(string from, string to, string subject, string body, bool isBodyHtml = true)
{
Send(new MailMessage(from, to, subject, body) { IsBodyHtml = isBodyHtml });
}
public async Task SendAsync(MailMessage mail, bool normalize = true)
{
if (normalize)
NormalizeMail(mail);
await SendEmailAsync(mail);
}
public void Send(MailMessage mail, bool normalize = true)
{
if (normalize)
NormalizeMail(mail);
SendEmail(mail);
}
public async Task InitMessageAsync(string body, string subject, bool isBodyHtml, bool normalize = true)
{
var message = new MailMessage(new MailAddress(_configuration.DefaultAddress, _configuration.DefaultDisplayName, Encoding.UTF8), new MailAddress(_configuration.RecipientAddress, _configuration.RecipientderDefaultDisplayName, Encoding.UTF8))
{
Body = body,
BodyEncoding = Encoding.UTF8,
HeadersEncoding = Encoding.UTF8,
Subject = subject,
SubjectEncoding = Encoding.UTF8,
IsBodyHtml = isBodyHtml
};
//if (normalize)
// NormalizeMail(message);
await SendEmailAsync(message);
}
/// <summary>
/// 简单发送
/// </summary>
/// <returns></returns>
public async Task Task()
{
////var email = new Email();
//var client = new SmtpClient("smtp.qq.com")
//{
// EnableSsl = true,
// UseDefaultCredentials = false,
// Credentials = new System.Net.NetworkCredential("1632694570@qq.com", "smurdizujnjgcacj")
//};
//MailAddress from = new MailAddress("1632694570@qq.com", "磅管家科技", Encoding.UTF8);//初始化发件人
//MailAddress to = new MailAddress("953706194@qq.com", "雷榕", Encoding.UTF8);//初始化收件人
// ////设置邮件内容
//MailMessage message = new MailMessage(from, to)
//{
// Body = "测试",
// BodyEncoding = Encoding.UTF8,
// Subject = "测试",
// SubjectEncoding = Encoding.UTF8,
// IsBodyHtml = true
//};
//client.Send(message);
}
public async Task SendMessageAsync(MailMessage mail, bool normalize = true)
{
if (normalize)
NormalizeMail(mail);
await SendEmailAsync(mail);
}
public SmtpClient BuildClient()
{
var host = _configuration.Host;
var port = _configuration.Port;
var smtpClient = new SmtpClient(host, port);
try
{
if (_configuration.EnableSsl)
smtpClient.EnableSsl = true;
if (_configuration.UseDefaultCredentials)
{
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential(_configuration.DefaultAddress, _configuration.Smurdizujnjgcacj);
}
else
{
smtpClient.UseDefaultCredentials = false;
var userName = _configuration.UserName;
if (!userName.IsNullOrEmpty())
{
var password = _configuration.Password;
var domain = _configuration.Domain;
smtpClient.Credentials = !domain.IsNullOrEmpty()
? new NetworkCredential(userName, password, domain)
: new NetworkCredential(userName, password);
}
}
return smtpClient;
}
catch
{
smtpClient.Dispose();
throw;
}
}
/// <summary>
/// Normalizes given email.
/// Fills <see cref="MailMessage.From" /> if it's not filled before.
/// Sets encodings to UTF8 if they are not set before.
/// </summary>
/// <param name="mail">Mail to be normalized</param>
protected virtual void NormalizeMail(MailMessage mail)
{
if ((mail.From == null) || mail.From.Address.IsNullOrEmpty())
mail.From = new MailAddress(
_configuration.DefaultAddress,
_configuration.DefaultDisplayName,
Encoding.UTF8
);
if (mail.HeadersEncoding == null)
mail.HeadersEncoding = Encoding.UTF8;
if (mail.SubjectEncoding == null)
mail.SubjectEncoding = Encoding.UTF8;
if (mail.BodyEncoding == null)
mail.BodyEncoding = Encoding.UTF8;
}
protected async Task SendEmailAsync(MailMessage mail)
{
try
{
using (var smtpClient = BuildClient())
{
await smtpClient.SendMailAsync(mail);
}
}
catch (Exception e)
{
throw;
}
}
protected void SendEmail(MailMessage mail)
{
using (var smtpClient = BuildClient())
{
smtpClient.Send(mail);
}
}
}
}
至此完成!【本公司系统项目异常检测使用】