【问题标题】:Sending outlook mail using VB.NET in Visual Studio 2015在 Visual Studio 2015 中使用 VB.NET 发送 Outlook 邮件
【发布时间】:2017-01-24 09:18:38
【问题描述】:

我正在尝试制作一个生日应用程序以从 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.Send() 替换为 OutlookMes​​sage.Display() 那么 Console.WriteLine("Email Not Sent")被打印出来。

只剩下发送邮件部分。

【问题讨论】:

  • 如果这是在 VBA 而不是 VB.NET 中运行,那么我建议将 .SEND.DISPLAY 交换,以消除它是阻止发送邮件的安全设置的可能性。也许这可能是一个类似的问题?
  • @CLR 我在 catch 中添加了一个 Console.WriteLine("Email Not Sent") 并将 .SEND 替换为 .DISPLAY。现在 Console.WriteLine("Email Not Sent") 正在打印。你能帮忙弄清楚发生了什么吗?

标签: excel vb.net outlook msdn


【解决方案1】:

最后一个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

【讨论】:

  • catch 不起作用,因为我在那里也放了一个 Console.WriteLine() 语句。我也会尝试管理员的事情,并在评论中告诉你。
  • 我在 catch 中添加了一个 Console.WriteLine("Email Not Sent") 并将 .SEND 替换为 .DISPLAY。现在 Console.WriteLine("Email Not Sent") 正在打印。你能帮忙弄清楚发生了什么吗?
  • 异常错误号和异常信息是什么? (它在 ex 变量中)
  • 你是对的。以管理员邮件的身份运行两者。所以将来如果我需要发送电子邮件,我需要以管理员身份运行它们。
  • 也有可能我们可以自动化这个应用程序。除了运行这个生日应用程序来发送邮件,我们可以做一些事情,它每天运行一次,然后如果在当前日期找到 DOB,则发送邮件。
猜你喜欢
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
  • 2016-06-22
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多