I am implementing a razor contact form on Umbraco CMS, which includes inline C# script to create and send an email using .Net’s MailMessage. The end user now needs to be able to upload a file, which gets added to the email as an attachment.
From my knowledge, the only way to do this is by uploading to the disk and then loading the attachment from the disk.
Is it possible to upload and attach without writing to disk? Keep in mind there is no code-behind or controller – this is all in-line razor logic.
You need to pass the
InputStreamfrom the uploaded file to the MailMessage’sAttachmentconstructor:Make sure that (unless you are using the FileUpload Helper) you set the form’s method to
POST, add anenctypeattribute with a value ofmultipart/form-data, and that you provide yourinput type=filewith anameattribute.