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

Excel 在Visual Studio 2015中使用VB.NET发送outlook邮件,excel,vb.net,outlook,msdn,Excel,Vb.net,Outlook,Msdn,我正在尝试制作一个生日应用程序,从CSV读取数据,将今天的日期与CSV文件中的DOB匹配,并将电子邮件发送到CSV文件中相应的Outlook电子邮件Id。我们将只向Outlook电子邮件Id发送邮件 我已经编写了代码来获取我需要发送电子邮件的电子邮件地址,并将它们存储在列表中。该列表会被反复浏览,Outlook会向每个人发送一封电子邮件。当代码到达OutlookMessage.Send时,应用程序不会执行任何操作。OutlookMessage.Send上面的任何代码(包括Console.Writ

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

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

arrName存储CSV中提到的人的姓名,arrValue是存储提到的人的出生日期的数组,ArrMail存储电子邮件地址

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

CSV文件包含3列:名称、DOB、电子邮件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
如果我将OutlookMessage.Send替换为OutlookMessage.Display,则会打印Console.WriteLineMail Not Sent

只剩下发送邮件部分。

最后一个Console.WriteLine无法工作,因为您捕获了一个异常而没有显示它

Catch ex As Exception
    'MessageBox.Show("Mail could not be sent") 'if you dont want this message, simply delete this line 
因此,首先,看一看引发的异常


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

捕获功能无法正常工作,因为我在其中放置了一个Console.WriteLine语句。我也会尝试管理员的方法,并在评论中告诉您。我在catch中添加了一个Console.writelinemail Not Sent,并将.SEND替换为.DISPLAY。现在正在打印Console.WriteLineMail Not Sent。你能帮我弄清楚发生了什么吗?异常错误号和异常消息是什么?你是对的。以管理员的身份运行这两种邮件。因此,将来如果我需要发送电子邮件,我需要以管理员的身份运行这两个应用程序。我们是否可以自动化此应用程序。我们可以不运行这个生日应用程序来发送邮件,而是每天运行一次,如果在当前日期发现DOB,则发送邮件。如果这是在VBA中运行的,而不是在VB.NET中运行的,那么我建议使用.swap with.DISPLAY来消除安全设置阻止邮件发送的可能性。也许这可能是一个类似的问题?@CLR i在catch中添加了一个Console.writelinemail未发送,并将.SEND替换为.DISPLAY。现在正在打印Console.WriteLineMail Not Sent。你能帮我弄清楚发生了什么事吗。