2017-08-30 78 views
0

作爲新手,我只能在Microsoft Visual Studio中創建表單。但我的要求是發送一個外觀郵件,而所有的選項應該填寫在VB.net中創建的表單。例如,地址將是VB.net表單中的下拉列表。 請幫忙。需要在VB.net中從表單發送Outlook電子郵件

+3

的可能的複製[vb.net發送電子郵件(https://stackoverflow.com/questions/4862649/vb-net-send-email ) – muffi

回答

2

這是我寫的一個小班,完成同樣的事情。我在網上看了一些例子,發現一些非常好的和不好的,並且做了這個小課。有些方法特別適合我們的需求,但您應該能夠根據您的需求進行模塑。

Public Class Email : Implements IDisposable 
     Dim _e As String 
     Dim _item As _MailItem 
     ReadOnly _oApp = New Microsoft.Office.Interop.Outlook.Application 

     Sub New() 
      Try 

       'Dim oApp As Microsoft.Office.Interop.Outlook._Application 
       'If Me(Microsoft.Office.Interop.Outlook.Application) 
       _item = _oApp.CreateItem(OlItemType.olMailItem) 
      Catch ex As COMException 
       MessageBox.Show("There was a problem with outlook on this machine.", "No Access to Email", MessageBoxButtons.OK, MessageBoxIcon.Warning) 
       [Error] = True 
      End Try 
     End Sub 

     Private Property [Error] As Boolean = False 
     Private Property HasError As Boolean = False 

     Public Sub AddAttachement(path As String) 
      'Debug.Print(Path) 
      _item.Attachments.Add(path) 
     End Sub 

     Public Shared Function GetAccountForEmailAddress(ByVal application As Microsoft.Office.Interop.Outlook.Application, ByVal address As String) As Account 
      ' Loop over the Accounts collection of the current Outlook session. 
      Dim account As Account 
      For Each account In application.Session.Accounts 
       ' When the e-mail address matches, return the account. 
       Debug.Print(account.SmtpAddress.ToString) 
       If account.SmtpAddress = address.ToString Then 
        Return account 
       End If 
      Next 
      Dim message As String = $"No Account with Address: {address.ToString} exists!" & Environment.NewLine & Environment.NewLine & "Only:" & Environment.NewLine & String.Join(Environment.NewLine, GetAllEmailAccounts(application).ToArray) & Environment.NewLine & "exist on this computer." 
      Throw New System.Exception(message.ToString) 
     End Function 

     Public Shared Function GetAllEmailAccounts(ByVal application As Microsoft.Office.Interop.Outlook.Application) As ArrayList 
      ' Loop over the Accounts collection of the current Outlook session. 
      Try 
       Dim acc As New ArrayList() 
       Dim account As Account 
       For Each account In application.Session.Accounts 
        acc.Add(account.SmtpAddress.ToString) 
       Next 
       Return acc 
      Catch ex As System.Exception 
       MyError(ex) 
       Return Nothing 
      End Try 
     End Function 

     Public Sub Send() 

      Try 


       If HasError = False Then 
        _item.Send() 
        If ShowNotification = True Then 
         MessageBox.Show("Email successfully sent to: " & Environment.NewLine & _e.ToString, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) 
        End If 

       End If 
      Catch ex As System.Exception 
       MyError(ex) 
      Finally 

      End Try 
     End Sub 

     Public Sub SentTo(emailAddress As String) 
      For Each add In emailAddress.Split(";") 
       'Debug.Print(RemoveWhitespace(add)) 
       _item.Recipients.Add(RemoveWhitespace(add)) 
      Next 

      If Not _item.Recipients.ResolveAll Then 
       HasError = True 
       Throw New System.Exception("Could send email to the following addresses: " & Environment.NewLine & emailAddress.ToString) 
      Else 
       _e = emailAddress 
      End If 
     End Sub 

     Public Function SetupEmail(subject As String, htmlBody As String, sendUsing As String) As Boolean 

      'Dim defaultFolder As MAPIFolder = _oApp.Session.GetDefaultFolder(OlDefaultFolders.olFolderDrafts) 
      Dim html = "<html><div style="" font-size:" & FontSize & "px;font-family:" & FontFamily & ";"">" 
      html = html & htmlBody 
      Try 

       'item = DirectCast(Outlook.Application.CreateItem(OlItemType.olMailItem), Outlook.MailItem) 

       Dim account As Account = GetAccountForEmailAddress(_oApp, sendUsing) 
       'item = DirectCast(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem) 
       'item.Recipients.Add(EmailAddress) 
       _item.Subject = "--- Digital Certificate Attached ---" 
       _item.SendUsingAccount = account 

       _item.Subject = subject.ToString 
       _item.SendUsingAccount = account 

       _item.BodyFormat = OlBodyFormat.olFormatHTML 
       _item.HTMLBody = String.Empty 
       _item.HTMLBody = html 
       _item.BodyFormat = OlBodyFormat.olFormatHTML 

       Return True 
      Catch exception1 As System.Exception 

       HasError = True 
       MyError(exception1) 
       Return False 
      End Try 
     End Function 

     Public Property FontFamily As String = "Tahoma" 
     Public Property FontSize As Integer = 12 

     Public ReadOnly Property HasErrrors As Boolean 
      Get 
       Return HasError 
      End Get 
     End Property 

     Public Property ShowNotification As Boolean 
      Get 
       Return _ShowNotification 
      End Get 
      Set(value As Boolean) 
       _ShowNotification = value 
      End Set 
     End Property 
     Private Property _ShowNotification As Boolean = True 

     Private _disposedValue As Boolean ' To detect redundant calls 

     ' IDisposable 
     Protected Overridable Sub Dispose(disposing As Boolean) 
      If Not _disposedValue Then 
       If disposing Then 
        ' TODO: dispose managed state (managed objects). 
        If _oApp IsNot Nothing Then 
         'Debug.Print("oWord has value") 
         Marshal.ReleaseComObject(_oApp) 
        End If 

        If _item IsNot Nothing Then 
         'Debug.Print("oWord has value") 
         Marshal.ReleaseComObject(_item) 
        End If 

       End If 


      End If 
      _disposedValue = True 
     End Sub 

     Public Sub Dispose() Implements IDisposable.Dispose 
      ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. 
      Dispose(True) 

     End Sub 
    End Class 

我用它的方式如下:

Using myemail As New <ClassName>.Email 
     myemail.SentTo(emailaddress) 
     myemail.AddAttachement(attachment) 
     If myemail.SetupEmail(EmailBody, Subject, SendingEmail) = True Then 
     myemail.Send() 
     End If 
End Using