user2166297
user2166297

Reputation:

MVC WebMail Helper - Is It Possible to Send An Email With Attachments Without Saving Them First?

Is this possible?

I'm using the MVC WebMail helper to send an email from a form where the user can add an attachment using a file input

    WebMail.Send(
            to: email.To,
            cc: email.CC,
            bcc: email.BCC,
            replyTo: email.ReplyTo,
            subject: email.Subject,
            body: email.MessageBody,
            filesToAttach: email.Attachments,
            isBodyHtml: email.IsBodyHtml
    );

I get a FileNotFound exception so obviously I could save this attachment first, send the email, then go back and delete it, but I was wondering if there was a way to avoid having to do that. I remember in WebForms that this was possible using the FileUpload control and System.Net.Mail's attachments system. Any suggestions? Thanks!

Upvotes: 1

Views: 2524

Answers (1)

Jeremy Bell
Jeremy Bell

Reputation: 718

The is the source for the System.Web.Helpers.WebMail.cs

http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Helpers/WebMail.cs

It looks like the helper method only supports file paths.

You'll have to just use the MailMessage and SmtpClient directly if you want to be able to send attachments from a file in memory.

message.Attachments.Add(new Attachment(postedFile.Stream, postedFile.Name, postedFile.ContentType));

Upvotes: 2

Related Questions