Open Closed

Help configuring email #1347


1
eu11111 created

Good afternoon! I need some help to send emails through ABP

Here is what I did so far:

Created this class

class EmailSettingProvider : SettingProvider
{
    public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
    {
        return new[]
                {
                    new SettingDefinition(EmailSettingNames.Smtp.Host, "smtp.gmail.com", L("SmtpHost"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                    new SettingDefinition(EmailSettingNames.Smtp.Port, "465", L("SmtpPort"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                    new SettingDefinition(EmailSettingNames.Smtp.UserName, "****@gmail.com", L("Username"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                    new SettingDefinition(EmailSettingNames.Smtp.Password, "*****", L("Password"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                    new SettingDefinition(EmailSettingNames.Smtp.Domain, "gmail.com", L("DomainName"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                    new SettingDefinition(EmailSettingNames.Smtp.EnableSsl, "true", L("UseSSL"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                    new SettingDefinition(EmailSettingNames.Smtp.UseDefaultCredentials, "true", L("UseDefaultCredentials"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                    new SettingDefinition(EmailSettingNames.DefaultFromAddress, "****@gmail.com", L("DefaultFromSenderEmailAddress"), scopes: SettingScopes.Application | SettingScopes.Tenant),
                    new SettingDefinition(EmailSettingNames.DefaultFromDisplayName, "My Name", L("DefaultFromSenderDisplayName"), scopes: SettingScopes.Application | SettingScopes.Tenant)
                };
    }

    private static LocalizableString L(string name)
    {
        return new LocalizableString(name, AbpConsts.LocalizationSourceName);
    }
}

On my Application Module

public override void PreInitialize()
{
     Configuration.Settings.Providers.Add<EmailSettingProvider>();
}

Created an App Service

public class EmailAppService : NHiveAppServiceBase, IEmailAppService
{
    private readonly ISmtpEmailSender _smtpEmailSender;

    public EmailAppService(
        ISmtpEmailSender smtpEmailSender)
    {
        _smtpEmailSender = smtpEmailSender;
    }

    public async Task SendTestEmail()
    {
        _smtpEmailSender.Send("test@gmail.com", "test@gmail.com", "test",  false);
    }
}

Did I do it correctly? As I understood from my google searches, SmtpEmailSender gets the settings automatically from the PreInitialize method. Is that correct?

It failed to send the e-mail, though.

Thanks in advance,


2 Answer(s)
  • 0
    eu11111 created

    I got it working, if anyone is wondering how, I changed the settings:

    Port > 587 Domain > empty string UseDefaultCredentials > false DefaultFromAddress > empty string DefaultFromDisplayName > empty string

    I am wondering now how to get a View as a string, so I can build the mail from a View

    new MailMessage(new MailAddress("email@gmail.com"), new MailAddress("email@gmail.com"))
     {
        Subject = "test",
         Body = [some view here],
        IsBodyHtml = true
     }
    
  • 0
    hikalkan created
    Support Team

    you can find better help on the web for "how to get a View as a string" since this is not related to ABP.