How to generate emails from Microsoft Outlook when writing code in C# on a Windowns platform using VS2005?

Posted on 2012-09-14
Last Modified: 2012-09-24
I am developing a C# Windows platform application using VS2005.

Do you know how the following VBA Access code could be rewritten into C# code?

I am trying to generate emails from Microsoft Outlook.

Sub sndEmailTest1Fall()
    On Error GoTo SndEmailTest1_ErrorHandler                  
    Dim iMsg As Object
    Dim iConf As Object
    Dim rsList As ADODB.Recordset
    Dim rsBranch As ADODB.Recordset
    Dim cn As ADODB.Connection
    Dim cn2 As ADODB.Connection
    Dim Flds As Object
    Dim str_sql1 As String
    Dim str_sql2 As String
    Dim str_BCC As String
    Dim str_From As String
    Dim str_To As String
    Dim intRec As Integer
    Dim rs As ADODB.Recordset
    Dim strPreviousBranch
    Dim strConcat_EmailAdd
    Dim strConcat_EmailAdd_New
    Dim sMail As String, rsCur, rsPrev
    Dim LResult As String
    Dim strBranch As String
    Dim sMailConst As String
    Set rsList = New ADODB.Recordset
    Set rsBranch = New ADODB.Recordset
    Set cn = CurrentProject.Connection
    Set cn2 = CurrentProject.Connection
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
    With Flds
        .Item("") = 5555
        .Item("") = ""
        .Item("") = 7777
    End With
    str_sql1 = "If  Exists(SELECT * FROM dbo.SYSOBJECTS WHERE NAME = 'tblProblemBranch' AND TYPE = 'U') DELETE FROM tblProblemBranch"
    DoCmd.RunSQL (str_sql1)
     With rsList
    .Open "SELECT DISTINCT E.Branch, E.EMAILADD From tblPSEmail", cn, 2, 2
     Do Until .EOF
        strBranch = .Fields("Branch")
        Do Until strBranch <> .Fields("Branch")
            sMail = sMail & .Fields("EMailAdd") & ";"
            If .EOF Then
                Exit Do
            End If
       LResult = Mid(sMail, 1, 1)
       If LResult <> "#" Then
          Set iMsg = CreateObject("CDO.Message")
          With iMsg
             Set .Configuration = iConf
             .To = str_To
             .Subject = "UDL Branch " & strBranch
              ExportedFile = "\\ny" & strBranch & ".XLS"
              If isFileExist(ExportedFile) Then Sort_add_Pagebreak ExportedFile
              .AddAttachment "\\ny" & strBranch & ".XLS"            
              Me!lblBranch.Caption = "Email Branch " & strBranch
          End With      
       End If
       Set iMsg = Nothing
Question by:zimmer9
    LVL 9

    Accepted Solution

    You could try the VB to C# converter, even though VBA is not VB.NET, they are similar languages in many ways.

    With regards to interfacing with Outlook, I use vbMAPI for anything I do with Outlook, it really makes Outlook programming a breeze. The developer claims it works well with C#.

    Good luck!
    LVL 20

    Assisted Solution


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now