Attaching a screenshot to new email outlook image with vb net

随声附和 提交于 2021-02-10 14:35:25

问题


been looking for a bit of code to take a screenshot and attach the screenshot to new email. I'll post what I have. It work 99%, just can't for the life of me figure out why it isn't attaching to the email.

It does everything but attach the new screenshot to the email. Is this possible?

Private Sub testStripMenuItem_Click(sender As Object, e As EventArgs) Handles testStripMenuItem.Click

    Dim maxHeight As Integer = 0
    Dim maxWidth As Integer = 0
    For Each scr As Screen In Screen.AllScreens
        maxWidth += scr.Bounds.Width
        If scr.Bounds.Height > maxHeight Then maxHeight = scr.Bounds.Height
    Next
    Dim AllScreensCapture As New Bitmap(maxWidth, maxHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
    Dim screenGrab As Bitmap
    Dim screenSize As Size
    Dim g As Graphics
    Dim g2 As Graphics = Graphics.FromImage(AllScreensCapture)
    Dim a As New Point(0, 0)
    For Each scr As Screen In Screen.AllScreens
        screenSize = New Size(scr.Bounds.Width, scr.Bounds.Height)
        screenGrab = New Bitmap(scr.Bounds.Width, scr.Bounds.Height)
        g = Graphics.FromImage(screenGrab)
        g.CopyFromScreen(a, New Point(0, 0), screenSize)
        g2.DrawImage(screenGrab, a)
        a.X += scr.Bounds.Width
    Next
    Dim Screenshot = "C:\img.png"
    If System.IO.File.Exists(Screenshot) Then
        System.IO.File.Delete(Screenshot)
    End If
    AllScreensCapture.Save(Screenshot, System.Drawing.Imaging.ImageFormat.Jpeg)

    'Email Code
    Dim strUserDomain As String
    Dim strCompName As String
    strUserDomain = Environ$("UserDomain")
    strCompName = Environ$("ComputerName")
    Dim theStringBuilder As New StringBuilder()
    theStringBuilder.Append("mailto:email@gmail.com.au")
    theStringBuilder.Append("&subject=From Domain: " & strUserDomain & ". Computer Name: " & strCompName)
    theStringBuilder.Append("&attach=" & Screenshot)

    Process.Start(theStringBuilder.ToString())
End Sub

回答1:


Alternative to sending through an installed mail client...

' Some of these may come from the form or user settings or wherever
Private Const MailSenderEmail As String = ""
Private Const MailSenderName As String = ""
Private Const MailRecipient As String = ""
Private Const MailSubject As String = ""
Private Const MailBody As String = ""
Private Const MailHost As String = ""
Private Const MailPort As String = ""
Private Const MailUser As String = ""
Private Const MailPass As String = ""
Private Const MailEnableSsl As Boolean = False

Private Sub testStripMenuItem_Click(sender As Object, e As EventArgs) Handles testStripMenuItem.Click
    '
    ' Your code to capture the screen
    '

    Dim Screenshot = "C:\img.png"
    If System.IO.File.Exists(Screenshot) Then
        System.IO.File.Delete(Screenshot)
    End If
    AllScreensCapture.Save(Screenshot, System.Drawing.Imaging.ImageFormat.Jpeg)

    ' Send the email with Screenshot attached
    Using MailMessage As New Net.Mail.MailMessage
        With MailMessage
            .From = New Net.Mail.MailAddress(MailSenderEmail, MailSenderName)
            .To.Add(MailRecipient)
            .Subject = MailSubject
            .Body = MailBody
            .Attachments.Add(New Net.Mail.Attachment(Screenshot))
        End With

        With New Net.Mail.SmtpClient
            .Host = MailHost
            .Port = MailPort
            .EnableSsl = MailEnableSsl

            Select Case True
                Case String.IsNullOrWhiteSpace(MailUser)
                Case String.IsNullOrWhiteSpace(MailPass)
                Case Else
                    .Credentials = New Net.NetworkCredential(MailUser, MailPass)
            End Select

            .Send(MailMessage)
        End With
    End Using
End Sub


来源:https://stackoverflow.com/questions/42641269/attaching-a-screenshot-to-new-email-outlook-image-with-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!