FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

Email To Disk Error

Open bearsystems opened this issue 5 years ago • 13 comments

I am using a very basic version with the call: var email = Email .From("[email protected]") .To("[email protected]") .Subject("Errors founds in " + _context.LocationName) .Body(strMyText, false).Send();

This is my setup in my Startup.cs file: services.AddFluentEmail("[email protected]").AddSmtpSender("localhost", 25);

Whenever I send the file, I am getting the following error: The specified path is invalid : '\\2018-11-29_09-17-50_163' at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle) at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at FluentEmail.Core.Defaults.SaveToDiskSender.SaveEmailToDisk(IFluentEmail email) at FluentEmail.Core.Defaults.SaveToDiskSender.SendAsync(IFluentEmail email, Nullable1 token) at FluentEmail.Core.Defaults.SaveToDiskSender.Send(IFluentEmail email, Nullable1 token) at FluentEmail.Core.Email.Send(Nullable1 token)`

bearsystems avatar Nov 29 '18 16:11 bearsystems

I figured it out - I added the follwoing:

Email.DefaultSender = new SmtpSender(new System.Net.Mail.SmtpClient() { Host = "localhost", Port = 25 });

bearsystems avatar Nov 29 '18 16:11 bearsystems

I had similar troubles with this issue on localhost. Is there a reason that the Startup code doesn't work on localhost? Is there something missing from the documentation?

billthemaxster avatar Dec 24 '18 02:12 billthemaxster

AddSmtpSender doesn't set the DefaultSender of Email

https://github.com/lukencode/FluentEmail/blob/master/src/Senders/FluentEmail.Smtp/FluentEmailSmtpBuilderExtensions.cs

Should this be opened as a separate issue?

McPhale avatar Mar 04 '19 19:03 McPhale

I am getting the same issue using the SendGridSender on localhost.

IOException: The specified path is invalid : '\\2019-03-27_10-36-28_153' System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle) System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options) System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) FluentEmail.Core.Defaults.SaveToDiskSender.SaveEmailToDisk(IFluentEmail email) FluentEmail.Core.Defaults.SaveToDiskSender.SendAsync(IFluentEmail email, Nullable<CancellationToken> token) FluentEmail.Core.Email.SendAsync(Nullable<CancellationToken> token)

bryanllewis avatar Mar 27 '19 17:03 bryanllewis

Whats the status on this - does any have a fix?

julian-code avatar Sep 16 '19 08:09 julian-code

The same issue applies for AddMailKitSender, which means my startup looks like this:

  var smtpClientOptions = new SmtpClientOptions
            {
                UseSsl = EmailSettings.UseSsl,
                Password = EmailSettings.Password,
                Port = EmailSettings.Port,
                Server = EmailSettings.Server,
                User = EmailSettings.User,
                UsePickupDirectory = false,
                RequiresAuthentication = !string.IsNullOrEmpty(EmailSettings.User)
            };
            services.AddFluentEmail(EmailSettings.FromAddress).AddRazorRenderer().AddMailKitSender(smtpClientOptions);
            Email.DefaultRenderer = new RazorRenderer();
            Email.DefaultSender = new MailKitSender(smtpClientOptions);

It seems a bit redundant to have to do both .AddRazorRender and .AddMailKitSender followed by then setting them as the default.

Perhaps the simple API change here is an overload on the extension which allows you to add the Sender/Renderer and set it as the default in one call.

agrath avatar Oct 21 '19 10:10 agrath

👍 Same issue, surely these calls can be made more concise

JoshZA avatar Sep 07 '20 23:09 JoshZA

Just don't use static class "Email" on your controler, but use an instance of "IFluentEmail" via DI.

Then use

await emailService .To(recipient, "bob") .Subject("hows it going bob") .Body("yo bob, long time no see!") .SendAsync();

And it will work :)

remi0411 avatar Oct 09 '20 09:10 remi0411

Hi Everyone, this appears to have just bitten me too.... been roaming around for a few days trying to work out what the issue is, and finally I stumble across this.

Brand new ASP.Net core application (Using Rider) , added this in my startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddRazorPages();
      services.AddServerSideBlazor();

      services
        .AddFluentEmail("[email protected]")
        .AddRazorRenderer()
        .AddSmtpSender("mailbox.digital-solutions.local", 25);
    }

Only to find that neither the razor renderer or SMTP sender are actually registered as default.

Actual send code in my action is:

@page "/"
@using FluentEmail.Core
@using FluentEmail.Razor

<h1>Serverside Blazor</h1>

<button @onclick="SendEmail" class="btn btn-primary">Send an Email</button>

@code
{
  private void SendEmail()
  {
    var template = "Dear @Model.Name, You are totally @Model.Compliment.";

    var email = Email
      .From("[email protected]")
      .To("[email protected]")
      .Subject("Fluent Email test")
      .UsingTemplate(template, new { Name = "Shawty", Compliment = "Awesome" })
      .Send();
    
  }
  
}

Once I realised that the sender & renderer where not set up as expected, I added in my own call to set the renderer

Email.DefaultRenderer = new RazorRenderer();

Just before the template line in the above code, and now I'm faced with a new problem that throws an exception every time it tries to render the razor template now:

image

The assembly in question is most definately not missing, I've checked that and also forced a copy to install using NuGet

image

I suspect however that this most recent exception, is a separate issue to what's being reported here, so I will in due course once I finish trying some other resolutions, raise that as a separate issue.

The documentation however, does need to be more clear, as it leads to the expectation that all that is needed are the lines in startup and off we go, this is the reason I choose to investigate fluent email over something like mailkit.

shawty avatar Aug 15 '21 13:08 shawty

I'm having the same issue with FluentEmail.Core on mobile. Getting the invalid path error message. My code is just

await Email
.To(recipient, "bob")
.Subject("hows it going bob")
.Body("yo bob, long time no see!")
.SendAsync();

I don't know if I'm missing something or I just can't use core?

JoshA295 avatar Apr 29 '22 09:04 JoshA295

Try running visual studio as administrator and testing the project. This solves the write to disk issue.

Turns out the issue is with the default sender. I followed the steps in the documentation but this did not set the default sender for FluentEmail. This worked:

        SmtpClient smtp = new SmtpClient
        {
            //smtp Server address
            Host = "mailserveraddress",
            UseDefaultCredentials = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            // Enter here that you are sending smtp User name and password for the server 
            Credentials = new NetworkCredential("username", "password"),
            EnableSsl = true
        };

        Email.DefaultSender = new SmtpSender(smtp);

        
var email = await Email
    .From(from)
    .To(toEmail, toEmail)
    .Subject(subject)
    .Body(message)
    .SendAsync();

reference: https://ofstack.com/asp.net/44195/net-core-sample-code-for-sending-mail-using-fluentemail.html

wskanaan avatar Aug 13 '22 18:08 wskanaan

Just rejected this library based on this issue. If you can't even get through sending one email without working around bugs in such basic behavior, I'm not about to trust this library with anything important.

stefan-helios avatar Apr 05 '23 14:04 stefan-helios

Can you try my fork?

https://github.com/jcamp-code/FluentEmail

If it still happens with this version, submit an issue there and I'll take a look. I love this library and want to keep it going.

JohnCampionJr avatar Apr 05 '23 15:04 JohnCampionJr