Open Closed

How to send email, using Abp.Net.Mail.Smtp.SmtpEmailSender? #209


0
wangwei22 created

Hi! I am using UserManager to send email, but it doesn't work. Do I need to config something, is EmailService of UserManager? In Abp, there's a class Abp.Net.Mail.Smtp.SmtpEmailSender, how do use it with UserManaer? Please give me some tip or sample. Thanks.

_userManager.SendEmail(
                        user.Id,
                        "subject",
                        "main content"
                        );

5 Answer(s)
  • 0
    hikalkan created

    Yes, you should configure it using ISettingManager. You can check https://github.com/aspnetboilerplate/as ... rovider.cs to see all configuration.

  • 0
    gvb created

    I will give how i did it! It's not the best way of doing it but it use some built-in part of ABP

    First i added some Setting in the database with Seed (preferably do it manually in production with a script) my site is still in progress so i keep it there to rebuild it anywhere pretty fast.

    // Seed
                    _context.Settings.Add(new Abp.Configuration.Setting
                    {
                        Name = EmailSettingNames.Smtp.Host,
                        Value = "smtp.gmail.com"
                    });
    
                    _context.Settings.Add(new Abp.Configuration.Setting
                    {
                        Name = EmailSettingNames.Smtp.Port,
                        Value = "587"
                    });
    
                    _context.Settings.Add(new Abp.Configuration.Setting
                    {
                        Name = EmailSettingNames.Smtp.UserName,
                        Value = "YOUR EMAIL"
                    });
    
                    _context.Settings.Add(new Abp.Configuration.Setting
                    {
                        Name = EmailSettingNames.Smtp.Password,
                        Value = "YOUR PASSWORD"
                    });
    
                    _context.Settings.Add(new Abp.Configuration.Setting
                    {
                        Name = EmailSettingNames.Smtp.EnableSsl,
                        Value = "true"
                    });
    
                    _context.Settings.Add(new Abp.Configuration.Setting
                    {
                        Name = EmailSettingNames.Smtp.UseDefaultCredentials,
                        Value = "false"
                    });
    

    then i use that code in ApplicationService :

    private readonly ISmtpEmailSenderConfiguration _smtpConfig;
    
            public SendGeneratedCode(ISmtpEmailSenderConfiguration smtpConfig)
            {
                _smtpConfig = smtpConfig;
            }
    
            public void Execute(GenerateCodeMessage msg)
            {
                var smtpClient = new SmtpEmailSender(_smtpConfig).BuildClient();
    
                var from = new MailAddress(_smtpConfig.UserName);
    
                smtpClient.Send(new MailMessage(from, new MailAddress("EMAIL TO"))
                {
                    Subject = "SUBJECT",
                    Body = "CONTENT",
                    IsBodyHtml = true
                });
            }
    

    i hope it will help you a little bit :)

    I got some probleme with my Google account that it say someone unsafe tried to use my account, but i activated an option to tell that it can be used by unsafe application, but i doesnt remember exactly what was the name of the paramater. And the email sent is usually in the Garbage of my outlook until i tell outlook that it is a safe email source....

  • 0
    hikalkan created

    Thanks.

    You can directly inject ISmtpEmailSender and use it to send emails (it will get configuration automatically). Also, be careful on seed data since it's executed in every "Update-Database" command. So, you should check it if a setting exists and don't add again if so.

  • 0
    gvb created

    i didn't know about the ISmtpEmailSender injection! It will save some lines of code in a lot of place :) thx

    for the Seed i just posted where i add those to the DB because i always do the verification if it exist or not!

    Like this

    if (!_context.Settings.Any(x => x.Name == EmailSettingNames.Smtp.Host) 
    {
        _context.Settings.Add(new Abp.Configuration.Setting
        {
            Name = EmailSettingNames.Smtp.Host,
            Value = "smtp.gmail.com"
        });
    }
    

    i don't know if it can be better !

  • 0
    wangwei22 created

    Thanks for above all.