簡體   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