简体   繁体   中英

Sending outlook mail using VB.NET in Visual Studio 2015

I am trying to make a birthday application to read data from CSV, match today's date with DOB in the CSV file and send email to the corresponding Outlook email Id in the CSV file. We will be sending mail to Outlook Email Id's only.

I have written the code to get email addresses to which I need to send the email and I have stored them in a list. The list is iterated through and Outlook despatches an email to each. When the code reaches OutlookMessage.Send() the application doesn't do anything. Any code above OutlookMessage.Send() including Console.WriteLine() statement works properly.

arrName stores the names of the people mentioned in the CSV, arrValue is an array to store the date of birth of person mentioned, and arrEmail stores the email address.

matchName is a list, storing names of matching people according to their DOB mentioned in the CSV. matchDOB is a list to store their DOBs, and matchEmail stores email addresses.

The CSV file contains 3 columns: Name, DOB, Email ID.

The last Console.WriteLine() statement after the OutlookMessage.Send() is not working, but the one before it is fine.

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

If I replace OutlookMessage.Send() with OutlookMessage.Display() then the Console.WriteLine("Email Not Sent") gets printed.

Just the sending mail part is left.

The last Console.WriteLine does not work because you catch an exception and do not display it

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

So first, take a look at the exception raised.

My guess is: your Visual Studio is running as administrator and not your Outlook. Both must run as the same. So restart your Outlook as administrator, or run your VS without administrator privilege

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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