VB.NET中的Sendmail附件问题

时间:2014-08-13 11:46:28

标签: vb.net sendmail

我在vb.net上有一个sendmail程序。我有附件发送就好了。问题是当没有附件时,程序错误。如何处理没有附件绕过并发送没有附件?以下工作正常。

是的,我知道,我的命名惯例太可怕了。一旦我完成了100%的逻辑,我就会改变。

Imports System.Net.Mail
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then
            MsgBox("Please describe the issue you're having Bender, I'm not a mindreader!")
            Exit Sub
        Else
            lblpleasewait.Visible = True
            Delay(2000)
            Dim Recipients As New List(Of String)
            Recipients.Add("emtnap6@yahoo.com")
            Dim FromEmailAddress As String = Recipients(0)
            Dim Subject As String = "IT Help!"
            Dim Body As String = TextBox1.Text
            Dim UserName As String = "fakeout"
            Dim Password As String = "fakeout"
            Dim Port As Integer = 587
            Dim Server As String = "smtp.gmail.com"
            Dim Attachments As New List(Of String)
            MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments))
            TextBox2.Text = "Attach Screenshot:"
            TextBox2.TextAlign = HorizontalAlignment.Right
            lblpleasewait.Visible = False
            TextBox1.Text = ""
            TextBox1.Focus()
        End If
    End Sub
    Sub delay(ByVal delay_ms As Integer)
        Dim tspan As New TimeSpan
        Dim tstart = Now
        While tspan.TotalMilliseconds < delay_ms
            tspan = Now - tstart
            Application.DoEvents()
        End While
    End Sub


    Function SendEmail(ByVal Recipients As List(Of String), _
                      ByVal FromAddress As String, _
                      ByVal Subject As String, _
                      ByVal Body As String, _
                      ByVal UserName As String, _
                      ByVal Password As String, _
                      Optional ByVal Server As String = "smtp.gmail.com", _
                      Optional ByVal Port As Integer = 587, _
                      Optional ByVal Attachments As List(Of String) = Nothing) As String
        Dim Email As New MailMessage()
        Try
            Dim SMTPServer As New SmtpClient
            ''For Each Attachment As String In Attachments
            ''    Email.Attachments.Add(New Attachment(Attachment))
            'Next
            Dim mailattach As String = TextBox2.Text
            Dim attachment As System.Net.Mail.Attachment
            attachment = New System.Net.Mail.Attachment(mailattach)
            Email.Attachments.Add(attachment)

            Email.From = New MailAddress(FromAddress)
            For Each Recipient As String In Recipients
                Email.Bcc.Add(Recipient)
            Next
            Email.Subject = Subject
            Email.Body = Body
            SMTPServer.Host = Server
            SMTPServer.Port = Port
            SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password)
            SMTPServer.EnableSsl = True
            SMTPServer.Send(Email)
            Email.Dispose()
            Return "Email to Derek was successfully sent."
        Catch ex As SmtpException
            Email.Dispose()
            Return "Sending Email Failed. Smtp Error."
        Catch ex As ArgumentOutOfRangeException
            Email.Dispose()
            Return "Sending Email Failed. Check Port Number."
        Catch Ex As InvalidOperationException
            Email.Dispose()
            Return "Sending Email Failed. Check Port Number."
        End Try
    End Function

    Private Sub FolderBrowserDialog1_HelpRequest(sender As Object, e As EventArgs)
        If (FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            TextBox2.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            TextBox2.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Focus()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If Not Environment.Is64BitProcess Then
            Process.Start("C:\Windows\sysnative\SnippingTool.exe")
        Else
            Process.Start("C:\Windows\system32\SnippingTool.exe")
        End If

    End Sub

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        OpenFileDialog1.InitialDirectory = Environment.SpecialFolder.MyComputer
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题。

If TextBox2.text <> "" then
    Dim mailattach As String = TextBox2.Text
    Dim attachment As System.Net.Mail.Attachment
    attachment = New System.Net.Mail.Attachment(mailattach)
    Email.Attachments.Add(attachment)
End if