Visual Basic重载分辨率失败,因为无法调用可访问的“新建”,错误

时间:2012-10-22 12:13:10

标签: vb.net email

我有这个问题的代码:

Generate a PDF file as system.net.mail.attachment using Memory Stream

帮我在内存中创建电子邮件附件。

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

我收到此错误:

  

重载解析失败,因为没有缩小转换就无法调用可访问的“新建”:

     

'Public Sub New(contentStream As System.IO.Stream,contentType As System.Net.Mime.ContentType)':参数匹配参数'contentStream'从'Object'缩小为'System.IO.Stream'。

     

'Public Sub New(fileName As String,contentType As System.Net.Mime.ContentType)':参数匹配参数'fileName'从'Object'缩小为'String'。

Dim Attach As Attachment
Attach = New Attachment(MemoryStream, content)

在这一行。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

在Using语句中,您将名为MemoryStream的变量初始化为Object而不是MemoryStream。

尝试更改

中的使用语句
Using MemoryStream = New MemoryStream

Using MemoryStream As New MemoryStream

答案 1 :(得分:3)

或使用此

Using MemoryStream As MemoryStream = new MemoryStream()