发送电子邮件时附加图像

时间:2014-07-25 15:06:37

标签: vb.net

我有这个应用程序,它在文件夹中查找任何jpg图像,如果有任何jpeg发现它向客户端发送电子邮件我可以从jpeg发送数据,但如何将图像附加到邮件。 我试过这个但是收到了这个错误system.io .__ error.winioerror(int32 errorcode string maybefullpath)。

Dim DirSearch As IO.FileInfo() = FileDirInfo.GetFiles("*.jpg")
            Dim FileInfo As IO.FileInfo

            Dim FileDir, FileName As String
            FileDir = FileDirInfo.ToString


 For Each FileInfo In DirSearch
            'for any files found, Split the filename into strings
            FileName = FileInfo.ToString

            Dim attachment As System.Net.Mail.Attachment
            attachment = New System.Net.Mail.Attachment(FileName)

                Dim attachment As System.Net.Mail.Attachment
                attachment = New System.Net.Mail.Attachment(FileName)

                Dim SmtpServer As New SmtpClient
                Dim mail As New MailMessage
                SmtpServer.Credentials = New Net.NetworkCredential("pramm", "roar")(//fake)
                'SmtpServer1.EnableSsl = True
                SmtpServer.Port = 587
                SmtpServer.Host = "email.loror.ac.uk"
                mail = New MailMessage
                mail.From = New MailAddress("alerts@loro.ac.uk")
                mail.To.Add("alerts@loro.ac.uk")
                mail.Subject = "Alert"
                mail.Body = "Vehicle " & VRM & " captured on " & Camera & " at " & TimeStr & " on the date " & DateStr
         attachment = New System.Net.Mail.Attachment(FileName)
            'mail.Attachments.Add(attachment)
                SmtpServer.Send(mail)

2 个答案:

答案 0 :(得分:0)

这是我用于HTML类型电子邮件和文件附件的代码。

您还可以将HTML代码包含在电子邮件的HTML代码中。

''' <summary>
''' Sends the HTML-type email and possible attached files to the given email addresses.
''' </summary>
Public Sub SendEmail(ByVal fromAddress As String, _
             ByVal fromName As String, _
             ByVal toAddresses As String(), _
             ByVal subject As String, _
             ByVal body As String, _
             Optional ByVal attachedFilePaths As List(Of String) = Nothing)


    Dim oMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

    Dim addrFrom As New System.Net.Mail.MailAddress(fromAddress, fromName)
    oMsg.From = addrFrom

    For i As Integer = 0 To toAddresses.Length - 1
        Dim address As New System.Net.Mail.MailAddress(toAddresses(i))
        oMsg.To.Add(address)
    Next

    oMsg.Subject = subject

    oMsg.Body = body

    oMsg.IsBodyHtml = True

    If attachedFilePaths IsNot Nothing Then
        For Each path As String In attachedFilePaths
            Dim oAttch As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(path)
            oMsg.Attachments.Add(oAttch)
        Next
    End If

    '---
    'GENERIC:
    Dim client As New System.Net.Mail.SmtpClient(emailSenderHost, emailSenderPort)
    Dim myLogin As New Net.NetworkCredential(emailSenderUsername, emailSenderPassword)
    client.EnableSsl = False

    client.EnableSsl = False
    client.UseDefaultCredentials = True
    client.Credentials = myLogin
    client.EnableSsl = emailSenderEnableSsl
    client.Send(oMsg)

End Sub

答案 1 :(得分:0)

请检查以下内容是否有帮助。

  • 如果图像是由某个进程创建的,请确保该进程正确处理它们。或者尝试手动放置图像。
  • 确保应用程序池具有该文件夹的权限。

作为基本测试,您可以在应用程序的根目录下创建一个示例目录,手动放置一个jpg。然后在进行必要的更改以使用此目录后运行该应用程序。