需要从VB.net中的表单发送outlook电子邮件

需要从VB.net中的表单发送outlook电子邮件,vb.net,forms,visual-studio,outlook,Vb.net,Forms,Visual Studio,Outlook,作为一名新手,我只能在Microsoft visual studio中创建表单。但我的要求是发送一封OutLook邮件,而所有选项都应填写在VB.net中创建的表单中。例如,收件人地址将是VB.net格式的下拉列表。 请帮忙。这是一个小班,我写这个小班就是为了做同样的事情。我在网上查看了一些例子,发现了一些非常好的和不好的例子,并上了这个小班。有些方法是专门为我们的需要而设置的,但是您应该能够根据您的需要来塑造它 Public Class Email : Implements IDisposa

作为一名新手,我只能在Microsoft visual studio中创建表单。但我的要求是发送一封OutLook邮件,而所有选项都应填写在VB.net中创建的表单中。例如,收件人地址将是VB.net格式的下拉列表。
请帮忙。

这是一个小班,我写这个小班就是为了做同样的事情。我在网上查看了一些例子,发现了一些非常好的和不好的例子,并上了这个小班。有些方法是专门为我们的需要而设置的,但是您应该能够根据您的需要来塑造它

 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
公共类电子邮件:实现IDisposable
变暗为字符串
将项目作为邮件项目
ReadOnly\u oApp=新的Microsoft.Office.Interop.Outlook.Application
次新
尝试
'将oApp设置为Microsoft.Office.Interop.Outlook.\u应用程序
'如果是我(Microsoft.Office.Interop.Outlook.Application)
_item=\u oApp.CreateItem(OlItemType.olMailItem)
作为例外捕获
MessageBox.Show(“此计算机上的outlook有问题。”,“无法访问电子邮件”,MessageBoxButtons.OK,MessageBoxIcon.Warning)
[错误]=True
结束尝试
端接头
私有属性[Error]为布尔值=False
私有属性HasError为Boolean=False
Public Sub AddAttachement(路径为字符串)
'调试.打印(路径)
_item.Attachments.Add(路径)
端接头
公共共享函数GetAccountForMailAddress(ByVal应用程序作为Microsoft.Office.Interop.Outlook.application,ByVal地址作为字符串)作为帐户
'循环当前Outlook会话的帐户集合。
作为帐户的帐户
对于application.Session.Accounts中的每个帐户
'当电子邮件地址匹配时,返回帐户。
Debug.Print(account.SmtpAddress.ToString)
如果account.SmtpAddress=address.ToString,则
返回帐户
如果结束
下一个
Dim message As String=$“不存在地址为:{Address.ToString}的帐户!”&Environment.NewLine&Environment.NewLine&“仅:”&Environment.NewLine&String.Join(Environment.NewLine,GetAllEmailAccounts(application.ToArray)&Environment.NewLine&“在此计算机上存在。”
抛出新System.Exception(message.ToString)
端函数
公共共享函数GetAllEmailAccounts(ByVal应用程序作为Microsoft.Office.Interop.Outlook.application)作为ArrayList
'循环当前Outlook会话的帐户集合。
尝试
Dim acc作为新的ArrayList()
作为帐户的帐户
对于application.Session.Accounts中的每个帐户
acc.Add(account.SmtpAddress.ToString)
下一个
返回acc
捕获ex作为系统异常
迈罗(前)
一无所获
结束尝试
端函数
公共子发送()
尝试
如果HasError=False,则
_项目.发送()
如果ShowNotification=True,则
MessageBox.Show(“电子邮件已成功发送至:”&Environment.NewLine&_e.ToString,“Success”,MessageBoxButtons.OK,MessageBoxIcon.Information)
如果结束
如果结束
捕获ex作为系统异常
迈罗(前)
最后
结束尝试
端接头
公共子收件人(电子邮件地址为字符串)
对于每个外接程序emailAddress.Split(“;”)
'Debug.Print(删除空格(添加))
_item.Recipients.Add(删除空格(Add))
下一个
如果不是_item.Recipients.ResolveAll,则
hasrerror=True
抛出新System.Exception(“可以向以下地址发送电子邮件:”&Environment.NewLine&emailAddress.ToString)
其他的
_e=电子邮件地址
如果结束
端接头
公共函数SetupEmail(主题为字符串,htmlBody为字符串,sendUsing为字符串)为布尔值
'Dim defaultFolder As MAPIFolder=\u oApp.Session.GetDefaultFolder(OlDefaultFolders.olFolderDrafts)
Dim html=“”
html=html&htmlBody
尝试
'item=DirectCast(Outlook.Application.CreateItem(OlItemType.olMailItem),Outlook.MailItem)
Dim account As account=GetAccountForMailAddress(_oApp,sendUsing)
'item=DirectCast(oApp.CreateItem(Outlook.OlItemType.olMailItem),Outlook.MailItem)
'项目.收件人.添加(电子邮件地址)
_item.Subject=“---随附数字证书---”
_item.SendUsingAccount=帐户
_item.Subject=Subject.ToString
_item.SendUsingAccount=帐户
_item.BodyFormat=OlBodyFormat.olFormatHTML
_item.HTMLBody=String.Empty
_item.HTMLBody=html
_item.BodyFormat=OlBodyFormat.olFormatHTML
返回真值
将异常1捕获为System.Exception
hasrerror=True
我的错误(例外1)
返回错误
结束尝试
端函数
公共属性FontFamily As String=“Tahoma”
公共属性FontSize为整数=12
公共只读属性HasErrrors为布尔值
得到
返回HasError
结束
端属性
公共属性ShowNotification为布尔值
得到
返回显示通知
结束
设置(值为布尔值)
_ShowNotification=值
端集
端属性
私有属性_ShowNotification为布尔值=True
 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