How can I create an object of Microsoft SqlServer Management Smo Mail MailServer class

0 votes

I am trying to create an object of Microsoft.SqlServer.Management.Smo.Mail.MailServer class in PowerShell. The reason I need to do this is because I am trying to setup DBMail on the fresh instance of SQL using PowerShell. I am successfully able to create the MailAccount with the default mail server. But I need to create my own.

Could anyone help me to point to any example or

Jul 10, 2018 in DevOps Tools by Nilesh
• 7,050 points

1 answer to this question.

0 votes

 Step 1 - Set variables for mail options.

  $sqlServer = 'YourServerName'

  $accountName = 'dbMailDefaultAcct'

  $accountDescription = 'Default dbMail Account'

  $originatingAddress = "$"

  $replyToAddress = ''

  $smtpServer = ''

  $profileName = 'dbMailDefaultProfile'

  $profileDescription = 'Default dbMail profile'


. Step 2 - Load the SMO assembly and create the server object, connecting to the server

  [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null

  $server = New-Object 'Microsoft.SqlServer.Management.SMO.Server' ($sqlServer)


Step 3 - Configure the SQL Server to enable Database Mail.

  $server.Configuration.DatabaseMailEnabled.ConfigValue = 1



Step 4 - Alter mail system parameters if desired, this is an optional step.

  $server.Mail.ConfigurationValues.Item('LoggingLevel').Value = 1



Step 5 - Create the mail account.

# ArgumentList contains the mail service, account name, description,

# display name and email address.

  $account = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Mail.MailAccount `

   -Argumentlist $server.Mail, $accountName, $accountDescription, $sqlServer, $originatingAddress

  $account.ReplyToAddress = $replyToAddress



Step 6 - Set the mail server now that the account is created.




Step 7 - Create a public default profile.

# ArgumentList contains the mail service, profile name and description.

  $mailProfile = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Mail.MailProfile `

   -ArgumentList $server.Mail, $profileName, $profileDescription



Step 8 - Associate the account to the profile and set the profile to public

  $mailProfile.AddAccount($accountName, 0)

  $mailProfile.AddPrincipal('public', 1)



Step 9 - Configure the SQL Agent to use dbMail.

  $server.JobServer.AgentMailType = 'DatabaseMail'

  $server.JobServer.DatabaseMailProfile = $profileName



answered Jul 10, 2018 by Kalgi
• 2,680 points

Related Questions In DevOps Tools

0 votes
2 answers
0 votes
1 answer

How do I update configuration of a Cassandra cluster ?

Here is a way through which you ...READ MORE

answered Jul 26, 2018 in DevOps Tools by Atul
• 10,240 points
0 votes
0 answers

How can I use Tomcat in Eclipse IDE?

Hi everyone, Can anyone tell me, how can I ...READ MORE

Mar 12, 2020 in DevOps Tools by akhtar
• 38,230 points
0 votes
1 answer
+1 vote
1 answer
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
0 votes
1 answer

Can I use Bluemix devops services with a dedicated Bluemix?

DevOps services isn't currently working in dedicated. READ MORE

answered Jul 11, 2018 in DevOps Tools by Kalgi
• 2,680 points
0 votes
1 answer

How to bootstrap droplets using Terraform?

Using passwords on instances is an absolute ...READ MORE

answered Jul 12, 2018 in DevOps Tools by Kalgi
• 2,680 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP