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 reference.dev

Jul 10, 2018 in DevOps Tools by Nilesh
• 6,880 points
221 views

1 answer to this question.

0 votes

 Step 1 - Set variables for mail options.

  $sqlServer = 'YourServerName'

  $accountName = 'dbMailDefaultAcct'

  $accountDescription = 'Default dbMail Account'

  $originatingAddress = "$sqlServer@yourDomain.com"

  $replyToAddress = 'DO_NOT_REPLY@yourDomain.com'

  $smtpServer = 'smtpServer.yourDomain.com'

  $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

  $server.Configuration.Alter()

 

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

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

  $server.Mail.ConfigurationValues.Item('LoggingLevel').Alter()

 

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

  $account.Create()

 

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

  $account.MailServers.Item($sqlServer).Rename($smtpServer)

  $account.Alter()

 

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

  $mailProfile.Create()

 

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

  $mailProfile.AddAccount($accountName, 0)

  $mailProfile.AddPrincipal('public', 1)

  $mailProfile.Alter()

 

Step 9 - Configure the SQL Agent to use dbMail.

  $server.JobServer.AgentMailType = 'DatabaseMail'

  $server.JobServer.DatabaseMailProfile = $profileName

  $server.JobServer.Alter()

 

answered Jul 10, 2018 by Kalgi
• 2,620 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
41 views
0 votes
1 answer
0 votes
1 answer

For Gradle & Spring Boot, how do I choose dependency with implementation?

Below is the answer to your question: Both ...READ MORE

answered Jun 16, 2018 in DevOps Tools by shubham
• 6,890 points
63 views
0 votes
1 answer
+13 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 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
146 views
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,620 points
33 views
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,620 points
33 views