serilog/serilog-sinks-email

Emails not sending

Closed this issue · 10 comments

Hello,

The emails are not sending .
Tried multiple code but still not sending .
Here part of my code :

public class Email_Logger : IEmail_Interface
{
    public Serilog.Core.Logger _email_Logger ;
    
    public Email_Logger()
    {
        _email_Logger = new LoggerConfiguration()
    .WriteTo.Email(new EmailConnectionInfo()
    {
        EmailSubject = "Subject Of Email",
        EnableSsl = false,
        FromEmail = "noreply@mondomain",
        MailServer = "FQDN of Internal SMTP SERVER",
        Port = 25,
        ToEmail = @"recipient@domain",
        NetworkCredentials=null,
        IsBodyHtml = true
    }
)
.CreateLogger();
}

Is there something wrong ?

What version of MailKit are you using? If you have Mailkit >2.6.0 it won't work.

Try donwgrading it to 2.6.0

Dont know why it has not been changed to make it work with newer versions of mailkit

Hello

I'm not using MailKit...
Do u have a sample/tuto working ?
Regards

EmailConnectionInfo notificationEmailInfo = new EmailConnectionInfo
{
    FromEmail = "youremail@somecompany.com",
    MailServer = "smtp.office365.com",
    EmailSubject = "Subject of the email",
    EnableSsl = false,
    Port = 587,
    ToEmail = emails,
    NetworkCredentials = new NetworkCredential()
    {
        UserName = "youremail@somecompany.com",
        Password = "yourpassword"
    }
};


logConfig.WriteTo.Email(
        notificationEmailInfo,
        restrictedToMinimumLevel: LogEventLevel.Information,
        period: TimeSpan.FromMinutes(60)
    );

This is what I am using, but I'm sending emails with office365 smtp, so a different use case than your's, as you seem to be trying to send emails from a relay server without authentication.

Be sure to call Log.CloseAndFlush() before program or thread ends, otherwise email sending will not execute.

Also, you might want to try enabling Serilog self debug to check if any error is raised internally:

Serilog.Debugging.SelfLog.Enable(Console.Out);

I'm trying to use serilog with dependency injection, so i have an interface and a service implementing the interface.
Here is rhe code for these 2 :

public interface IEmail_Interface { void Error(Exception? exception, string message, int propertyValue); }

`public class Email_Logger : IEmail_Interface
{
public Serilog.Core.Logger _email_Logger;

public Email_Logger()
{
    _email_Logger = new LoggerConfiguration()
                    .WriteTo.Email(new EmailConnectionInfo()
                    {
                        EmailSubject = "Mail Subject",
                        EnableSsl = false,
                        FromEmail = "mail@domain",
                        MailServer = "smtp.office365.com",
                        Port = 587,
                        ToEmail = @"mail@domain",
                        NetworkCredentials = new NetworkCredential() {
                        UserName="user@domain",
                        Password= "password"

                        },
                        IsBodyHtml = true
                    }
                        , restrictedToMinimumLevel: LogEventLevel.Information
                        , period: TimeSpan.FromMinutes(60))

.CreateLogger();
}
void IEmail_Interface.Error(Exception? exception, string message, int propertyValue)
{
_email_Logger.Error(exception, message, propertyValue);
}

}`

Then in the Service program i call :

_email_interface.Error(ex, "le message",50);

but nothing is send and there is no error in the console

Office365 does't allow to send emails with the account password by default.

You have to have two factor enabled and then go to :

https://aka.ms/CreateAppPassword

And create an app password to send emails with that password.

If it's not working yet, try to call _email_interface.email_logger.CloseAndFlush(); after calling the logger.Error method to check if that triggers the email sending.

ok for O365 but with my relay server it should work without credentials on port 25...
I Can't find the _email_interface.email_logger.CloseAndFlush().

hello it definitively doesn't work with .net7 and asp application...
too bad.

Hi shraned, try changing the new logconfiguration to new Serilog.LoggerConfiguration()

Hi! I've updated the version of MailKit we're using, and generally freshened up the package in the 3.0.0-dev-* packages now on NuGet.

I have done very minimal testing, so the new dev packages may be broken in obvious ways, however there's a pretty good chance anything that was version-related will now be resolved.

Any help testing the new packages out would be appreciated; I'll close this as stale but please feel free to open a new issue with example configuration/source code if you still need help. Cheers!