繁体   English   中英

在 Visual Studio 2015 中使用 VB.NET 发送 Outlook 邮件

[英]Sending outlook mail using VB.NET in Visual Studio 2015

我正在尝试制作一个生日应用程序以从 CSV 读取数据,将今天的日期与 CSV 文件中的 DOB 匹配,并将电子邮件发送到 CSV 文件中相应的 Outlook 电子邮件 ID。 我们将仅向 Outlook 电子邮件 ID 发送邮件。

我已经编写了代码来获取我需要将电子邮件发送到的电子邮件地址,并将它们存储在列表中。 该列表被迭代,Outlook 会向每个人发送一封电子邮件。 当代码到达OutlookMessage.Send() ,应用程序不会执行任何操作。 OutlookMessage.Send()之上的任何代码包括Console.WriteLine()语句都可以正常工作。

arrName存储在CSV中提到的人的名字, arrValue是存储的人提到诞生之日起的数组, arrEmail存储的电子邮件地址。

matchName是一个列表,根据 CSV 中提到的 DOB 存储匹配人员的姓名。 matchDOB是一个存储他们的 DOB 的列表,而matchEmail存储电子邮件地址。

CSV 文件包含 3 列:姓名、出生日期、电子邮件 ID。

OutlookMessage.Send()之后的最后一个Console.WriteLine()语句不起作用,但它之前的语句没问题。

Imports System.IO
Imports Microsoft.VisualBasic.FileIO.TextFieldParser
Imports outlook = Microsoft.Office.Interop.Outlook

Module Module1

    Sub Main()
        Dim File1 As String = File.ReadAllText("C:\Users\Music\Excel1.csv")

        Dim arrName() As String
        Dim arrValue() As String
        Dim arrEmail() As String

        Dim matchName As New List(Of String)
        Dim matchDOB As New List(Of String)
        Dim matchEmail As New List(Of String)

        Using ioReader As New FileIO.TextFieldParser("C:\Users\Music\Excel1.csv")

            ioReader.TextFieldType = FileIO.FieldType.Delimited
            ioReader.SetDelimiters(",")

            While Not ioReader.EndOfData

                Dim arrCurrentRow As String() = ioReader.ReadFields()

                If arrName Is Nothing Then

                    ReDim Preserve arrName(0)
                    ReDim Preserve arrValue(0)
                    ReDim Preserve arrEmail(0)

                    arrName(0) = arrCurrentRow(0)
                    arrValue(0) = arrCurrentRow(1)
                    arrEmail(0) = arrCurrentRow(2)

                Else

                    ReDim Preserve arrName(arrName.Length)
                    ReDim Preserve arrValue(arrValue.Length)
                    ReDim Preserve arrEmail(arrEmail.Length)

                    arrName((arrName.Length - 1)) = arrCurrentRow(0)
                    arrValue((arrValue.Length - 1)) = arrCurrentRow(1)
                    arrEmail((arrEmail.Length - 1)) = arrCurrentRow(2)

                End If

            End While

            Dim regDate As Date = Date.Now()
            Dim strDate As String = regDate.ToString("dd/MMM/yyyy")
            Dim index As Integer = 0

            For Each element As String In arrValue


                Dim compCond As Integer = String.Compare(strDate, element)

                If compCond = 0 Then

                    matchDOB.Add(element)
                    matchName.Add(arrName(index))
                    matchEmail.Add(arrEmail(index))

                End If

                index = index + 1
            Next

        End Using

        Dim OutlookMessage As outlook.MailItem
        Dim AppOutlook As New outlook.Application

        Dim ind As Integer = 0
        For Each matchDOB1 As String In matchDOB

            Try
                Console.WriteLine("Starting 1")
                OutlookMessage = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
                Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
                Recipents.Add(matchEmail(ind))
                OutlookMessage.Subject = matchName(ind)
                OutlookMessage.Body = matchDOB1
                OutlookMessage.BodyFormat = outlook.OlBodyFormat.olFormatHTML
                Console.WriteLine("Before Send")
                OutlookMessage.Send()
                Console.WriteLine("Email Sent For " + matchName(ind))

            Catch ex As Exception
                Console.WriteLine("Email Not Sent")
                'MessageBox.Show("Mail could not be sent") 'if you dont want this message, simply delete this line 

            Finally
                OutlookMessage = Nothing
                AppOutlook = Nothing
                ind = ind + 1
            End Try
        Next
        Console.Read()
    End Sub

End Module

如果我有OutlookMes​​sage.Display替换OutlookMes​​sage.Send()(),然后Console.WriteLine命令(“电子邮件未送出”)被打印出来。

只剩下发送邮件部分。

最后一个Console.WriteLine不起作用,因为您捕获了异常并且不显示它

Catch ex As Exception
    'MessageBox.Show("Mail could not be sent") 'if you dont want this message, simply delete this line 

所以首先,看看引发的异常。

我的猜测是:您的 Visual Studio 以管理员身份运行,而不是您的 Outlook。 两者必须以相同的方式运行。 所以以管理员身份重启你的 Outlook,或者在没有管理员权限的情况下运行你的 VS

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM