0

下面是我用来从 jquery ajax 调用接收图像的 vb.net 代码。

Public Function MailKpiChart(ByVal img As String) As GenericResponse(Of Boolean) Implements IKPI.MailKpiChart

        Dim SmtpServer As New Net.Mail.SmtpClient()
        SmtpServer.Credentials = New Net.NetworkCredential("xyz@gmail.com", "password")
        SmtpServer.Port = 587
        SmtpServer.Host = "smtp.gmail.com"
        SmtpServer.EnableSsl = True
        Dim blStatus As Boolean
        Try
            Dim mailMessage As New MailMessage()
            mailMessage.From = New MailAddress("from@gmail.com", ".Net Devoloper")
            mailMessage.To.Add(New MailAddress("to@gmail.com"))
            mailMessage.Subject = "test mail"
            mailMessage.Body = "hello world"
            Dim imgStream As New MemoryStream()
            Dim Image As System.Drawing.Bitmap = getImagefromBase64(img)
            Dim filename As String = "sample image"

            Image.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png)
            mailMessage.Attachments.Add(New Attachment(imgStream, filename, System.Net.Mime.MediaTypeNames.Image.Jpeg))
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
            SmtpServer.Send(mailMessage)
            blStatus = True
            Return ServiceUtility.BuildResponse(Of Boolean)(blStatus, String.Empty, String.Empty, 0)
        Catch ex As Exception
            Return ServiceUtility.BuildResponse(Of Boolean)(False, "",
        ex.Message, AppConstants.ErrorSeverityCodes.HIGH)
        End Try

    End Function

这是我将base64字符串转换为图像的功能

 Public Function getImagefromBase64(ByVal uploadedImage As String) As Image
        'get a temp image from bytes, instead of loading from disk
        'data:image/gif;base64,
        'this image is a single pixel (black)
        Dim bytes As Byte() = Convert.FromBase64String(uploadedImage)

        Dim image__1 As Image
        Using ms As New MemoryStream(bytes)
            ms.Write(bytes, 0, bytes.Length)
            image__1 = Image.FromStream(ms)
        End Using

        Return image__1
    End Function

当我看到我的 gmail 收件箱 ica 邮件带有附件时,但我没有看到我发送的实际图像..我只是 ca 空白图像

需要帮助

问候

4

2 回答 2

1

我遇到了同样的问题,并通过在将流附加到消息之前添加以下行来解决它。

imgStream.Position = 0

问候,乔纳森。

于 2013-06-18T12:19:02.210 回答
0

添加您的网址 - 指向另一个网站

 Dim bodys As String = "<html><head><title> example </title></head><body><table width='100%'>"

    bodys += "<p><img width=135 height=77 src='http://www.example.com/image.gif'></p>"

    bodys += "</table></body></html>"

Dim mails As New System.Net.Mail.MailMessage()
        SmtpServer.Host = "127.0.0.1"
        SmtpServer.Port = 25

        mails = New System.Net.Mail.MailMessage()
        mails.From = New System.Net.Mail.MailAddress("example@example.com")
        mails.To.Add(txtEmail.Text)
        mails.Subject = ""
        mails.BodyEncoding = Encoding.UTF8
        mails.IsBodyHtml = True

        mails.Body = bodys.ToString()


        SmtpServer.Send(mails)

相对 URL - 指向网站内的文件

src="image.gif"

http://www.w3schools.com/tags/att_img_src.asp

于 2014-12-01T16:23:19.817 回答