vbscript [.net] [vb]从内存流发送包含附件的电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript [.net] [vb]从内存流发送包含附件的电子邮件相关的知识,希望对你有一定的参考价值。

Dim photoBase64 As String
Dim photoImgType As String 

' Prep photo attachment
Dim isImgJpeg As Integer = InStr(photoBase64, photoImgType)
Dim imgBytes As Byte() = Nothing
If isImgJpeg > 0 Then ' Attach this image
    ' Crop out section: "data:image/jpeg;base64,"
    ' Convert base64 to byte() to be placed in memory stream and attached to email
    imgBytes = Convert.FromBase64String(Mid(photoBase64, InStr(photoBase64, ",") + 1))
End If

' Send email
emailSent = MyEmailProvider.SendSupportEmail(toAddresses, "Product Support Inquiry", bodyText, imgBytes, "photo.jpg", photoImgType)



==============



Public Function SendSupportEmail( _
        ByVal toAddresses() As String, _
        ByVal subjectLine As String, _
        ByVal bodyText As StringBuilder, _
        ByVal attachedFileData As Byte(), _
        ByVal attachedFileName As String, _
        ByVal attachedMediaType As String _
    ) As Boolean

    SendSupportEmail = True
    Dim errorStr As String = "None"

    Try
        Dim mail As MailMessage = New MailMessage()

        Dim toAddressesStr As String = String.Empty
        For i As Integer = 0 To toAddresses.GetUpperBound(0)
            If toAddresses(i) IsNot String.Empty Then
                toAddressesStr &= toAddresses(i) & ","
            End If
        Next
        toAddressesStr = Mid(toAddressesStr, 1, toAddressesStr.Length - 1)
        mail.To.Add(toAddressesStr)

        mail.From = New MailAddress(ConfigurationManager.AppSettings("FROMNAME") & " <" & ConfigurationManager.AppSettings("FROMEMAIL") & ">")
        'Mail.From = New MailAddress(ConfigurationManager.AppSettings("FROMEMAIL"), ConfigurationManager.AppSettings("FROMNAME"))
        mail.Subject = subjectLine

        mail.Body = bodyText.ToString()
        mail.IsBodyHtml = True

        If attachedFileData IsNot Nothing And attachedFileName IsNot Nothing And attachedMediaType IsNot Nothing Then
            Dim ms As MemoryStream = New MemoryStream(attachedFileData)
            mail.Attachments.Add(New Attachment(ms, attachedFileName, attachedMediaType))
        End If

        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = ConfigurationManager.AppSettings("SMTP").ToString()
        smtp.Port = CInt(ConfigurationManager.AppSettings("SMTPPORT"))
        smtp.Credentials = New NetworkCredential(ConfigurationManager.AppSettings("FROMEMAIL"), ConfigurationManager.AppSettings("FROMPWD"))
        'smtp.EnableSsl = True

        smtp.Send(mail)

    Catch ex As Exception
        SendSupportEmail = False
        errorStr = ex.ToString()

    End Try

    Return SendSupportEmail

End Function

以上是关于vbscript [.net] [vb]从内存流发送包含附件的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

vbscript [.net] [vb]从目录发送带有文件附件的电子邮件

vbscript 和 vb.net 有啥区别?

vbscript [.net] VB.NET笔记

vbscript [VB.NET]新事物#test

vbscript Varios Razor VB.NET

vbscript VB.net正常运行时间报告