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
• 7,060 points
1,664 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,680 points

Related Questions In DevOps Tools

0 votes
0 answers

How can I sort my pre-conditions by execution in an automated test?

The question "How would I sort my ...READ MORE

Oct 15 in DevOps Tools by Anila
• 3,390 points
44 views
0 votes
1 answer

How can I monitor resource usage (CPU, memory, I/O) of running Docker containers?

The thing which is really important in terms of maintaining stable performance for the Docker ...READ MORE

answered Nov 4 in DevOps Tools by Gagana
• 2,450 points
33 views
0 votes
1 answer

How can I configure Docker to use a specific number of CPU cores for a container?

In your docker run command, set a specific number of CPU cores to use directly by the container. This helps in controlling resources, particularly with shared environments or using multiple ...READ MORE

answered 3 days ago in DevOps Tools by Gagana
• 2,450 points
39 views
+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
4,034 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,680 points
761 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,680 points
713 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP