【发布时间】: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
如果我将 OutlookMessage.Send() 替换为 OutlookMessage.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") 正在打印。你能帮忙弄清楚发生了什么吗?