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

Russclarto

New member
Joined
Jun 25, 2025
Messages
4
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)
 
Add whichever textbox's content is the user info you want to use.

VB.NET:
Expand Collapse Copy
Dim greeting As String = $"<p>Dear {userInfo.Text},</p>"
 
we need it to be clear that we cannot use an oft file with a salutation and footer



' 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)
 
You're creating a normal string as the greeting; you need to insert the recipient's name into the greeting string. Wherever that is, if located on your Windows form with the email address, then insert the contents of the recipient's name. If there is a matching contact to the email address in Outlook, then search for the contact and retrieve their name from that.
 
Back
Top