Can you send emails as BCC from a business rule?

OSAdmin
Valued Contributor
Originally posted by Brad Kotecki

We have a business rule set up that emails users once completed. To do this we used the sample under Snippets>Other>General>Send mail.

Is there a way to BCC the email addresses on the email, rather than sending directly to everyone as a group?

1 REPLY 1

tsandi
Contributor

The utility Send email uses a starndard smtp client connection. 

I creted the Sub below starting from the original function but adding the ability to specify a bcc list as well. 

Public Sub SendMailWithBcc(ByVal si As SessionInfo, ByVal emailConnectionName As String, ByVal toEmailAddresses As List(Of String), ByVal bccEmailAddresses As List(Of String), ByVal subject As String, ByVal body As String, ByVal isBodyHtml As Boolean, ByVal attachmentFilePaths As List(Of String))
	Try
		Dim nameValueFormatBuilder As NameValueFormatBuilder = New NameValueFormatBuilder()
		Dim databaseServerConnection As DatabaseServerConnection = AppServerConfig.GetDatabaseServerConnection(si, emailConnectionName)

		If databaseServerConnection IsNot Nothing Then
			nameValueFormatBuilder.ApplyFormatString(databaseServerConnection.ConnectionString)
		End If

		Dim value As String = nameValueFormatBuilder.GetValue("SmtpHost")
		Dim valueAsInt As Integer = nameValueFormatBuilder.GetValueAsInt("SmtpPort", 587, allowNegativeNumbers:=True)
		Dim valueAsBool As Boolean = nameValueFormatBuilder.GetValueAsBool("EnableSSL", defaultValue:=False)
		Dim value2 As String = nameValueFormatBuilder.GetValue("SmtpSourceMailAccount")
		Dim value3 As String = nameValueFormatBuilder.GetValue("SmtpSourceMailAccountPassword")
		Dim valueWasFound As Boolean
		Dim address As String = nameValueFormatBuilder.GetValue("FromAddress", valueWasFound)

		If Not valueWasFound Then
			address = value2
		End If

		Using smtpClient As SmtpClient = New SmtpClient()
			smtpClient.Host = value
			smtpClient.Port = valueAsInt
			smtpClient.EnableSsl = valueAsBool
			smtpClient.Credentials = New NetworkCredential(value2, value3)

			Using mailMessage As MailMessage = New MailMessage()
				mailMessage.From = New MailAddress(address)

				If toEmailAddresses IsNot Nothing Then

					For Each toEmailAddress As String In toEmailAddresses

						If Not String.IsNullOrEmpty(toEmailAddress) Then
							mailMessage.[To].Add(toEmailAddress)
						End If
					Next
				End If
				
				If bccEmailAddresses IsNot Nothing Then

					For Each bccEmailAddress As String In bccEmailAddresses

						If Not String.IsNullOrEmpty(bccEmailAddress) Then
							mailMessage.Bcc.Add(bccEmailAddress)
						End If
					Next					
				End If

				mailMessage.Subject = subject
				mailMessage.Body = body
				mailMessage.IsBodyHtml = isBodyHtml
				
				If attachmentFilePaths IsNot Nothing Then

					For Each attachmentFilePath As String In attachmentFilePaths

						If Not String.IsNullOrEmpty(attachmentFilePath) Then
							Dim item As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(attachmentFilePath)
							mailMessage.Attachments.Add(item)
						End If
					Next
				End If
				
				smtpClient.Send(mailMessage)	
				
			End Using

		End Using


	Catch e As Exception
		Throw New XFException(si, e)
	End Try
End Sub		

The usage is the same as before, but because it is a custom sub you can simply add it to your business rule and call it directly:

SendMailWithBcc(si, 
				"OneStreamEmail", 
				New List(Of String) From {"toname@gmail.com"},
				New List(Of String) From {"toname@hotmail.com"},
				"TestBcc",
				"TestBody",
				False,
				Nothing)

 Remember to add those two additional imports in the business rule:

Imports System.Net.Mail
Imports System.Net