Visual Basic Overload resolution failed because no accessible 'New' can be called, error

email, vb.net

Solution

or use this

Using MemoryStream As MemoryStream = new MemoryStream()

Problem

I have this code from this question: Generate a PDF file as system.net.mail.attachment using Memory Stream to help me create an email attachment in memory. ``` Imports System.IO Imports System.Net.Mail Imports System.Text.ASCIIEncoding Imports System.net.Mime Public Sub SendMail(ByVal att As String, Optional ByVal filename As String _ = "Attachment.csv") Dim sendMail As New SmtpClient Dim mail As New MailMessage Using MemoryStream = New MemoryStream If att.Length <> 0 Then Dim data As Byte() = ASCII.GetBytes(att) MemoryStream.Write(data, 0, data.Length) MemoryStream.Seek(0, SeekOrigin.Begin) MemoryStream.Position = 0 Dim content As New Net.Mime.ContentType() content.MediaType = MediaTypeNames.Application.Octet content.Name = filename Dim Attach As Attachment Attach = New Attachment(MemoryStream, content) mail.Attachments.Add(Attach) End If sendMail.DeliveryMethod = SmtpDeliveryMethod.Network sendMail.Host = "SERVER" sendMail.UseDefaultCredentials = False sendMail.Credentials = New System.Net.NetworkCredential("UN", "PW") sendMail.Send(mail) End Using End Sub ``` I receive this error : Overload resolution failed because no accessible 'New' can be called without a narrowing conversion: 'Public Sub New(contentStream As System.IO.Stream, contentType As System.Net.Mime.ContentType)': Argument matching parameter 'contentStream' narrows from 'Object' to 'System.IO.Stream'. 'Public Sub New(fileName As String, contentType As System.Net.Mime.ContentType)': Argument matching parameter 'fileName' narrows from 'Object' to 'String'. ``` Dim Attach As Attachment Attach = New Attachment(MemoryStream, content) ``` at this line. How can I fix this?

Original source