Assistance with code for sending emails adding "dear (user)"

Russclarto

New member
Joined
Jun 25, 2025
Messages
1
Programming Experience
Beginner
i am using a button to send an email to a user using an ost file but would like VS to add "dear (user)" at the beginning before the template copying the (user) part from a text box

this is what i currently use to send the email

' Determine which listbox has the selected item
Dim selectedItem As String = If(ListBoxTemplates.SelectedItem?.ToString(), ListBoxTemplatesFavourites.SelectedItem?.ToString())
Dim selectedFile As String = Path.Combine(ProgramLocation & "\" & TemplateFolderName, selectedItem) & ".oft"

' Start Outlook application
Dim outlookApp As New Outlook.Application()

' Path to your .oft template
'Dim templatePath As String = "C:\Projects\Templates"

Dim mailItem As Outlook.MailItem = CType(outlookApp.CreateItemFromTemplate(selectedFile), Outlook.MailItem)

' Set sender (on behalf of)
Dim SendEmailFrom As String = TextBoxSendEmailFrom.Text.Trim()
If Not String.IsNullOrEmpty(SendEmailFrom) Then
mailItem.SentOnBehalfOfName = SendEmailFrom
End If

' Create email from template
'Dim mailItem As MailItem = CType(outlookApp.CreateItemFromTemplate(templatePath), MailItem)

' Insert "Dear," at the start of the body
Dim currentBody As String = mailItem.HTMLBody
Dim greeting As String = "<p>Dear,</p>"

' Your custom footer
Dim footer As String = "<hr><p style='font-family:Arial;font-size:10pt;color:gray;'>This is my custom footer signature.<br>Company Name<br>www.example.com</p>"

' Update body
mailItem.HTMLBody = greeting & currentBody & footer

' Display the mail for review
mailItem.Display()

This works ok~

Dim selectedItem As String = If(ListBoxTemplates.SelectedItem?.ToString(), ListBoxTemplatesFavourites.SelectedItem?.ToString())
Dim selectedFile As String = Path.Combine(ProgramLocation & "\" & TemplateFolderName, selectedItem) & ".oft"

Try
Dim outlookApp As New Outlook.Application
Dim mailItem As Outlook.MailItem = CType(outlookApp.CreateItemFromTemplate(selectedFile), Outlook.MailItem)

' Set recipient email
Dim emailAddress As String = TextBoxEmailAddress.Text.Trim()
If Not String.IsNullOrEmpty(emailAddress) Then
mailItem.To = emailAddress
End If

' Set sender (on behalf of)
Dim SendEmailFrom As String = TextBoxSendEmailFrom.Text.Trim()
If Not String.IsNullOrEmpty(SendEmailFrom) Then
mailItem.SentOnBehalfOfName = SendEmailFrom
End If

mailItem.Display(False)
 
Back
Top