asp.net MVC邮件发送有时失败

时间:2016-09-21 12:32:37

标签: asp.net asp.net-mvc email

在我的应用程序中,它有时无法发送邮件,并且它不是特定于域的(即在gmail中有时会发送邮件,有时根据客户端投诉不会发送邮件)。可能是什么原因,任何想法?我附上了我的identityConfig.cs的代码

using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using RoasterManagementSystem.Data.DbContext;
using RoasterManagementSystem.Model;
using System.Net;
using System.Net.Mail;

namespace RoasterManagementSystem.App
 {
   public class EmailService : IIdentityMessageService
    { 
     public async Task SendAsync(IdentityMessage message)
      {
        var smtpConfig = new System.Net.Mail.SmtpClient();
        var credential =     (System.Net.NetworkCredential)smtpConfig.Credentials;
        var fromAddress = new MailAddress(credential.UserName, "Roster     Management Application");
        var toAddress = new MailAddress(message.Destination);
        string fromPassword = "123456Tt";
        string subject = message.Subject;
        string body = message.Body;

        var smtp = new SmtpClient
        {
            Host = smtpConfig.Host,
            Port = smtpConfig.Port,
            EnableSsl = smtpConfig.EnableSsl,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = smtpConfig.UseDefaultCredentials,
            Credentials = new NetworkCredential(credential.UserName, credential.Password)
        };

        var mail = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body,
            IsBodyHtml = true
        };

        using(smtp)
        using (mail)
        {
            try
            {
                await smtp.SendMailAsync(mail);
            }
            catch (Exception ex)
            {
                //If email sending failed then do nothing for now
            }
        }
    }
}

public class SmsService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Plug in your SMS service here to send a text message.
        return Task.FromResult(0);
    }
}

// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {

        this.UserValidator = new UserValidator<ApplicationUser>(this)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        // Configure validation logic for passwords
        this.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 8,
            RequireNonLetterOrDigit = false,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        // Configure user lockout defaults
        this.UserLockoutEnabledByDefault = true;
        this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        this.MaxFailedAccessAttemptsBeforeLockout = 5;

        // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
        // You can write your own provider and plug it in here.
        this.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
        {
            MessageFormat = "Your security code is {0}"
        });
        this.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
        {
            Subject = "Security Code",
            BodyFormat = "Your security code is {0}"
        });
        this.EmailService = new EmailService();
        this.SmsService = new SmsService();

        var dataProtectionProvider = Startup.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            this.UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
    }
}

// Configure the application sign-in manager which is used in this application.
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
    public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
        : base(userManager, authenticationManager)
    {
    }

    public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
    {
        return user.GenerateUserIdentityAsync((ApplicationUserManager) UserManager);
    }
}

}

0 个答案:

没有答案