我在 Visual Studio 中尝试使用 Microsoft Outlook 库通过 VB.net 通过 Outlook 发送和接收电子邮件时遇到问题。我尝试按照微软网站页面进行操作,但它是用 VBA 编写的,根本没有帮助,因为我一开始尝试调用该库时遇到了问题。我已经进入依赖项并添加了以下 COM 的
Microsoft Outlook 16.0 Object Library
和 Microsoft Outlook View Control
。但我不确定我是否需要在代码中的某个地方引用它们,而且我不确定命令是什么,到目前为止我所拥有的只是:。我如何打电话给图书馆并发送电子邮件。
编辑:这只适用于 .NET Framework 2.0 或更高版本,.NET Core 似乎不起作用
Imports System
Imports System.IO
Module Program
Sub main()
Dim objOL = New Outlook.Application
Dim objNS = objOL.GetNamespace("MAPI")
Dim objFolder = objNS.GetDefaultFolder(10)
Dim Newtask As Outlook.TaskItem
' Set the Application object
Dim objOLApps = New Outlook.Application
' You can only use CreateItem for default items
Dim NewTasks = objOL.CreateItem(6)
' Display the new task form so the user can fill it out
Newtask.Display()
End Sub
End Module
安装“Microsoft.Office.Interop.Outlook”nuget包后,您可以尝试以下代码发送电子邮件(代码来自https://stackoverflow.com/a/42743804/12666543):
Imports System.IO
Imports Outlook = Microsoft.Office.Interop.Outlook
Module Module1
Sub Main()
Dim arrAttachFiles As List(Of String) = New List(Of String)() From {
"the file path you want to attach (e.g. D:\Test.xlsx)"
}
sendEmailViaOutlook("your email address", "email addresses you want to send", "email addresses you want to cc", "this is subject", "this is body", arrAttachFiles)
End Sub
Public Sub sendEmailViaOutlook(ByVal sFromAddress As String, ByVal sToAddress As String, ByVal sCc As String, ByVal sSubject As String, ByVal sBody As String, ByVal Optional arrAttachments As List(Of String) = Nothing)
Try
Dim app As Outlook.Application = New Outlook.Application()
Dim newMail As Outlook.MailItem = CType(app.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
If Not String.IsNullOrWhiteSpace(sToAddress) Then
Dim arrAddTos As String() = sToAddress.Split(New Char() {";"c, ","c})
For Each strAddr As String In arrAddTos
If Not String.IsNullOrWhiteSpace(strAddr) AndAlso strAddr.IndexOf("@"c) <> -1 Then
newMail.Recipients.Add(strAddr.Trim())
Else
Throw New Exception("Bad to-address: " & sToAddress)
End If
Next
Else
Throw New Exception("Must specify to-address")
End If
If Not String.IsNullOrWhiteSpace(sCc) Then
Dim arrAddTos As String() = sCc.Split(New Char() {";"c, ","c})
For Each strAddr As String In arrAddTos
If Not String.IsNullOrWhiteSpace(strAddr) AndAlso strAddr.IndexOf("@"c) <> -1 Then
newMail.Recipients.Add(strAddr.Trim())
Else
Throw New Exception("Bad CC-address: " & sCc)
End If
Next
End If
If Not newMail.Recipients.ResolveAll() Then
Throw New Exception("Failed to resolve all recipients: " & sToAddress & ";" & sCc)
End If
If arrAttachments IsNot Nothing Then
For Each strPath As String In arrAttachments
If File.Exists(strPath) Then
newMail.Attachments.Add(strPath)
Else
Throw New Exception("Attachment file is not found: """ & strPath & """")
End If
Next
End If
If Not String.IsNullOrWhiteSpace(sSubject) Then newMail.Subject = sSubject
If Not String.IsNullOrWhiteSpace(sBody) Then newMail.Body = sBody
Dim accounts As Outlook.Accounts = app.Session.Accounts
Dim acc As Outlook.Account = Nothing
For Each account As Outlook.Account In accounts
If account.SmtpAddress.Equals(sFromAddress, StringComparison.CurrentCultureIgnoreCase) Then
acc = account
Exit For
End If
Next
If acc IsNot Nothing Then
newMail.SendUsingAccount = acc
newMail.Send()
Else
Throw New Exception("Account does not exist in Outlook: " & sFromAddress)
End If
Catch ex As Exception
Console.WriteLine("ERROR: Failed to send mail: " & ex.Message)
End Try
End Sub
End Module
结果:
我知道这是一篇旧帖子,但最近我尝试从 hotmail Outlook 发送电子邮件但没有成功,并注意到 MS Outlook 库“很难”使用,所以决定采用我自己的快速方法。在网上搜索终于找到了一个关键数据,正确的smtp服务器,这是一个片段我希望对其他人有帮助:
Friend Sub SendEmail(strSMTP As String)
Dim mail As New MailMessage()
mail.From = New MailAddress(txtEmail.Text.Trim)
mail.To.Add(txtDestinatario.Text.Trim)
mail.Subject = "Prueba"
mail.Body = "Cuerpo del correo de prueba"
Dim smtp As New SmtpClient(strSMTP)
smtp.Port = 587
smtp.Credentials = New Net.NetworkCredential(txtEmail.Text.Trim, txtPassword.Text.Trim)
smtp.EnableSsl = True
Try
smtp.Send(mail)
MsgBox("Correo enviado correctamente.")
Catch ex As Exception
Debug.WriteLine("Error al enviar correo: " & ex.Message)
End Try
End Sub
要发送电子邮件,请按以下方式调用该流程: SendEmail("smtp.office365.com") 您将需要导入 导入 System.Net.Mail 请注意,生成的电子邮件最终会进入收件人电子邮件中的垃圾邮件文件夹。