oMailItem.Subject = "From Database"

Man Hin Tam 1 Reputation point
2021-11-11T06:52:17.163+00:00
        Dim ol As New Outlook.Application()
        Dim ns As Outlook.NameSpace
        Dim fdMail As Outlook.MAPIFolder

        ns = ol.GetNamespace("MAPI")
        ns.Logon(, , True, True)

        'creating a New MailItem object
        Dim newMail As Outlook.MailItem

        'gets defaultfolder for my Outlook Outbox
        fdMail = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox)

        'assign values to the newMail MailItem
        newMail = fdMail.Items.Add(Outlook.OlItemType.olMailItem)
        newMail.Subject = "Subject"
        newMail.Body = "Body"
        newMail.To = "******@somewhere.com"
        newMail.SaveSentMessageFolder = fdMail

        newMail.SaveAs(Environ("HOMEPATH") & "\My Documents\" & newMail.Subject & ".msg", Outlook.OlSaveAsType.olMSGUnicode)

How can i get the Subject Body from Database?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,778 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
5,463 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,331 Reputation points
    2021-11-11T09:01:20.093+00:00

    Hi Man Hin,
    insert (after fdMail = ..) and try following code for SQL database. ConnectionString to SQL Server is set in project settings (cnSQL).

    Try
      Using cmd As New SqlCommand("SELECT * FROM Tab1", New SqlConnection(My.Settings.cnSQL))
        cmd.Connection.Open()
        Dim rdr = cmd.ExecuteReader
        While rdr.Read
          'assign values to the newMail MailItem '
          newMail = CType(fdMail.Items.Add(Outlook.OlItemType.olMailItem), Outlook.MailItem)
          newMail.Subject = rdr.GetString(0) ' 0 - munber of Field "Subject" in readed row '
          newMail.Body = "Body"
          newMail.To = "******@somewhere.com"
          newMail.SaveSentMessageFolder = fdMail
          newMail.SaveAs(Environ("HOMEPATH") & "\My Documents\ " & newMail.Subject & ".msg", Outlook.OlSaveAsType.olMSGUnicode) 
        End While
      End Using
    Catch ex As Exception
      MsgBox(ex.ToString)
    End Try
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.