我正在发送像这样的asp.net core 2.0的电子邮件
public string EmailServiceSend(string body, string subject = null, string to = null,
string multipleRecipients = null, string attachmentUrl = null, string smtpClient = null,
string userCredential = null, string userPassword = null, string from = null, bool template = false,
string templateRoute = null)
{
try
{
SmtpClient client = new SmtpClient("mysmtpserver");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("whoever@me.com");
mailMessage.To.Add("receiver@me.com");
mailMessage.Body = body;
mailMessage.Subject = subject;
client.Send(mailMessage);
}
catch(Exception ex)
{
ex.Message.ToString();
}
return "Email sended"
}
我想知道,如何创建要阻止的电子邮件域列表并发送错误消息。例如,我想阻止这个域:
0-mail.com
0815.ru
0clickemail.com
0wnd.net
然后将响应发送到我的return
语句中,例如:
if(listofblockmails == true){
return "You can't add this email domain, change it please"
}
我怎样才能实现这一目标?此致
答案 0 :(得分:0)
尝试使用Linq:
让我们说你有这个清单:
List<string> listofblockmails = new List<string>();
listofblockmails.Add("0-mail.com");
listofblockmails.Add("0815.ru");
listofblockmails.Add("0clickemail.com");
listofblockmails.Add("0wnd.net");
您可以执行以下操作:
if (listofblockmails.Contains("emailToBeChecked.com"))
{
//return "You can't add this email domain, change it please";
}
else
{
//send
//return "Email sended";
}
让垃圾收集器处理。
答案 1 :(得分:0)
您只需解析电子邮件地址并获取域名部分,然后再次比较坏域名列表。如果您的域名列表中存在域名,请返回信息。
您可以创建这样的方法。
private bool IsGoodDomain(string email)
{
if (string.IsNullOrEmpty(email))
return false;
var badDomains = new List<string>
{
"0815.ru",
"0wnd.net"
};
var r = email.Split('@');
if (!r.Any()) return false;
var domainName = r.Last();
return !badDomains.Contains(domainName, StringComparer.InvariantCultureIgnoreCase);
}
并在您的其他方法中调用此方法
if(IsGoodDomain(someEmailAddressVariable))
{
//contnue sending
}
else
{
// return the error message
}