Base solution for your next web application
Open Closed

Email - SMTP UserName is null or empty #3353


0
jdavis01 created

I am running an internal site on AspNet Core mVC JQuery on framework 4.6.1.

Here are the application smtp settings (fake values of course) :

from address: <a href="mailto:[email protected]">[email protected]</a> from Name: First Last

Host: ##.##.##.## Use SSL = false Use default address: Yes

Send To address: <a href="mailto:[email protected]">[email protected]</a>

Here is the error that is generated

ERROR 2017-06-08 07:00:02,489 [27   ] Mvc.ExceptionHandling.AbpExceptionFilter - Setting value for 'Abp.Net.Mail.Smtp.UserName' is null or empty!
Abp.AbpException: Setting value for 'Abp.Net.Mail.Smtp.UserName' is null or empty!
   at Abp.Net.Mail.EmailSenderConfiguration.GetNotEmptySettingValue(String name)
   at Abp.Net.Mail.Smtp.SmtpEmailSenderConfiguration.get_UserName()
   at Abp.MailKit.DefaultMailKitSmtpBuilder.ConfigureClient(SmtpClient client)
   at Abp.MailKit.DefaultMailKitSmtpBuilder.Build()
   at Abp.MailKit.MailKitEmailSender.BuildSmtpClient()
   at Abp.MailKit.MailKitEmailSender.<SendAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---

So here is the kicker .. I have another app that uses System.Net.Mail and no issues with the same settings and no errors..

Thoughts?


9 Answer(s)
  • 0
    ismcagdas created
    Support Team

    Hi @jdavis01,

    How did you configured email settings ? Related setting is retrieved just like this

    var value = SettingManager.GetSettingValue(name);
    

    You can run this code before sending email to see if setting value is retrieved correctly or not.

    Thanks.

  • 0
    jdavis01 created

    Sorry for the late response.. The values are getting populated correctly because the following code retrieves all the settings...

    var output = await _tenantSettingsAppService.GetAllSettings();
    

    Here is where I get stuck. Not sure where the value is getting dropped using the original Send Test Email Button..

    $('#SendTestEmailButton').click(function () {
               _tenantSettingsService.sendTestEmail({
                    emailAddress: $('#TestEmailAddressInput').val()
                }).done(function () {
                    abp.notify.info(app.localize('TestEmailSentSuccessfully'));
                });
            });
    
  • 0
    ismcagdas created
    Support Team

    Hi,

    Actually, those are showing setting values but while sending an email, same setting values are retrieved again using

    var value = SettingManager.GetSettingValue(name);
    

    for each setting like from address, host, use ssl etc...

    So, that is why I asked you to check the value of

    var value = SettingManager.GetSettingValue(name);
    

    You can also copy the code of <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp/Net/Mail/Smtp/SmtpEmailSender.cs">https://github.com/aspnetboilerplate/as ... lSender.cs</a> into your project and use it to send email and see if it works with your settings or not. In that way, you can identify problem faster I think.

    Thanks.

  • 0
    jdavis01 created

    Sorry it took so long again for me to get back to this.

    I tried the sample code you recommended, and it returned null

    var value = SettingsManager.GetSettingValue(EmailSettingNames.Smtp.UserName)
    

    I finally got the email system to work however and here is how I did it..

    I had to implement ISmtpEmailSender instead of IEmailSender in the SettingsAppServiceBase.

    using System.Threading.Tasks;
    using Abp.Net.Mail;
    using wcb.ScratchPad.Configuration.Host.Dto;
    using Abp.Net.Mail.Smtp;
    
    namespace wcb.ScratchPad.Configuration
    {
        public abstract class SettingsAppServiceBase : ScratchPadAppServiceBase
        {
           // private readonly IEmailSender _emailSender;
            private readonly ISmtpEmailSender _emailSender;
    
            protected SettingsAppServiceBase(           
                ISmtpEmailSender emailSender)
            {
                _emailSender = emailSender;
            }
    
            #region Send Test Email   
            public async Task SendTestEmail(SendTestEmailInput input)
            {
                await _emailSender.SendAsync(               
                    input.EmailAddress,
                    L("TestEmail_Subject"),
                    L("TestEmail_Body")
                );
            }
    
            #endregion
        }
    }
    

    It seems that IEmailSender throws this exception no matter how many ways you populate the value.

    AbpException: Setting value for 'Abp.Net.Mail.Smtp.UserName' is null or empty!
    Abp.Net.Mail.EmailSenderConfiguration.GetNotEmptySettingValue(string name)
    

    Not sure if this is considered and acceptable solution,but it has me moving forward for now. If it is not i will raise an issue on the git repo to start tracking it.

    thanks for the support and general direction to get started debugging..

  • 0
    jdavis01 created

    To clean things up, and to help anyone else having this issue, I had to implement the ISmtpEmailSender in the UserMailer.cs class inside the Core project. Now the email system is working very nicely..

    Here is the change....

    /// <summary>
        /// Used to send email to users.
        /// </summary>
        public class UserEmailer : ScratchPadServiceBase, IUserEmailer, ITransientDependency
        {
            private readonly IEmailTemplateProvider _emailTemplateProvider;
            private readonly ISmtpEmailSender _emailSender;
            private readonly IRepository<Tenant> _tenantRepository;
            private readonly ICurrentUnitOfWorkProvider _unitOfWorkProvider;
    
            public UserEmailer(IEmailTemplateProvider emailTemplateProvider,
                ISmtpEmailSender emailSender,
                IRepository<Tenant> tenantRepository,
                ICurrentUnitOfWorkProvider unitOfWorkProvider)
            {
                _emailTemplateProvider = emailTemplateProvider;
                _emailSender = emailSender;
                _tenantRepository = tenantRepository;
                _unitOfWorkProvider = unitOfWorkProvider;
            }
    
  • 0
    ismcagdas created
    Support Team

    Thanks for the backup @jdavis01.

  • 0
    jdavis01 created

    Welcome.. Love the project more and more each day..

  • 0
    mdonogma created

    SMTP mail sending also failing for me.

    Is this going to be fixed in the baseline code as I'd expect this to work without having to fix.

  • 0
    ismcagdas created
    Support Team

    Hi @Mdonogma,

    Can you share the error message you are getting ? It might be related to your email configuration.

    Thanks.