.Net – Sending Email with In Memory Attachment


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/www/danfolkes.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

This VB.Net code takes the FileUpload control’s PostedFile and without saving it to the filesystem attaches it to an MailMessage and sends an email.

Advantages:

  • No threading, read/write permissions needed.
  • One line of code.

Front End:

<asp:FileUpload ID="fileUpload" runat="server" />

Code:

Dim m As New MailMessage()
m.Attachments.Add(New Attachment(fileUpload.PostedFile.InputStream, fileUpload.FileName))