Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Help configuring email #1347


User avatar
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("[email protected]", "[email protected]", "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)
  • User Avatar
    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 protected]"), new MailAddress("[email protected]"))
     {
        Subject = "test",
         Body = [some view here],
        IsBodyHtml = true
     }
    
  • User Avatar
    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.