Home
%3CLINGO-SUB%20id%3D%22lingo-sub-583018%22%20slang%3D%22en-US%22%3ETroubleshooting%20Database%20Mail%20issues%20in%20Azure%20SQL%20Managed%20Instance%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-583018%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20SQL%20Database%20-%20Managed%20Instance%20enables%20you%20to%20directly%20send%20the%20email%20messages%20to%20the%20external%20email%20servers.%20You%20can%20send%20email%20messages%20directly%20using%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fsql%252Frelational-databases%252Fsystem-stored-procedures%252Fsp-send-dbmail-transact-sql%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995704991689%26amp%3Bsdata%3DcgR4%252BU%252Fz8CKf6AQjKwBgrmtUxSg3tgOe0uA0T0wmWH4%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_send_dbmail%3C%2FA%3E%26nbsp%3Bprocedure%20or%20via%20SQL%20Agent%20Jobs%20as%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fazure%252Fsql-database%252Fsql-database-job-automation-overview%2523job-notifications%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705001693%26amp%3Bsdata%3DaIAF5F%252Fl5LiItagNexM0fAZX7qJC4ezAdxvwkaY7oc8%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ealerts%3C%2FA%3E.%20Learn%20more%20about%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fsql%252Frelational-databases%252Fdatabase-mail%252Fdatabase-mail%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705001693%26amp%3Bsdata%3DCKJGLuGCoVZuKiblkUSa1RCtEVKt%252F5AuqHENfLwsuQE%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDatabase%20Mail%20here%3C%2FA%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20previous%20article%2C%20I%20have%20explained%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-SQL-Database%2FSending-emails-in-Azure-SQL-Managed-Instance%2Fba-p%2F386235%22%20target%3D%22_self%22%3Ehow%20to%20configure%20mail%20profile%20and%20send%20emails%20on%20Managed%20Instance%3C%2FA%3E.%20Database%20mail%20is%20reliable%20API%20that%20you%20can%20use%20to%20deliver%20email%20messages%20directly%20within%20the%20Managed%20Instance%20to%20external%20email%20server%3B%20however%2C%20in%20some%20cases%20you%20might%20experience%20the%20issues%20due%20to%20incorrect%20configuration%20of%20mail%20account%2C%20network%20connection%2C%20etc.%3C%2FP%3E%0A%3CP%3EIn%20this%20article%20you%20will%20learn%20how%20to%20troubleshoot%20the%20potential%20issues%20with%20DbMail%20if%20you%20cannot%20send%20the%20emails.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1737946207%22%20id%3D%22toc-hId-1737946207%22%3ETroubleshooting%20Database%20mail%3C%2FH2%3E%0A%3CP%3EIf%20you%20are%20the%20experiencing%20the%20issues%20with%20sending%20e-mail%20messages%2C%20try%20some%20of%20the%20following%20troubleshooting%20steps%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECheck%20is%20the%20option%20'Database%20Mail%20XPs'%20enabled%20in%20%3CEM%3Esys.configurations%3C%2FEM%3E%20view.%20If%20not%2C%20you%20can%20enable%20it%20using%20the%20following%20script%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3Esp_configure%20'show%20advanced%20options'%2C%201%3B%20%20%0AGO%20%20%0ARECONFIGURE%3B%20%20%0AGO%20%20%0Asp_configure%20'Database%20Mail%20XPs'%2C%201%3B%20%20%0AGO%20%20%0ARECONFIGURE%20%20%0AGO%20%20%3C%2FPRE%3E%0A%3CUL%3E%0A%3CLI%3ECheck%20have%20you%20correctly%20configured%20email%20profile%20with%20correct%20email%20server%20name%2FIP%20address%2C%20port%2C%20and%20account%20information%20(username%20and%20password)%3C%2FLI%3E%0A%3CLI%3ECheck%20could%20you%20reach%20the%20mail%20server%20from%20Managed%20Instance.%0A%3CUL%3E%0A%3CLI%3ECheck%20in%20Network%20Security%20Group%20associated%20to%20the%20Managed%20Instance%20subnet%20do%20you%20have%20outbound%20rule%20that%20allows%20communication%20via%20TCP%20port%2025%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F122074i2910DEBA6907C840%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22db-mail-nsg.PNG%22%20title%3D%22db-mail-nsg.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20SQL%20Agent%20job%20that%20has%20one%20PowerShell%20task%20that%20executes%20command%20like%20%3CCODE%3Etns%20smpt.sendgrid.net%20-25%3C%2FCODE%3E%20(see%20the%20example%20below)%2C%20run%20the%20job%20and%20check%20the%20job%20output%20in%20the%20job%20history.%20This%20is%20public%20mail%20server%20that%20should%20be%20reached%20from%20your%20Managed%20Instance%2C%20unless%20if%20you%20have%20explicitly%20blocked%20that%20name%20and%2For%20port.%3C%2FLI%3E%0A%3CLI%3EReplace%20the%20name%20of%20the%20mail%20server%20and%2For%20port%20in%20the%20job%20with%20the%20mail%20server%20and%20port%20that%20you%20are%20using%20and%20repeat%20the%20previous%20step.%3C%2FLI%3E%0A%3CLI%3ECheck%20have%20you%20enabled%20the%20port%20that%20is%20used%20to%20communicate%20with%20the%20email%20server.%20Port%20should%20be%20added%20in%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fazure%252Fvirtual-network%252Ftutorial-filter-network-traffic%2523create-security-rules%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705011701%26amp%3Bsdata%3DmGDWi7VntcZrzla06JM0P2oAZoZ7YFUftfTR7KpRjDE%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOutbound%20security%20rules%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CLI%3ECheck%20the%20status%20of%20E-Mail%20messages%20sent%20with%20database%20mail%20in%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fsql%252Frelational-databases%252Fdatabase-mail%252Fcheck-the-status-of-e-mail-messages-sent-with-database-mail%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705011701%26amp%3Bsdata%3DSh%252BVFdyDrybRYwExKw0VFqXLHg3yXxz%252BJ%252F2Tm%252FyNYB4%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDatabase%20Mail%20Log%3C%2FA%3E%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fsql%252Frelational-databases%252Fsystem-catalog-views%252Fsysmail-event-log-transact-sql%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705021710%26amp%3Bsdata%3DhpbyMnEmc%252FWOQee5cinZaPi3ncikR347DNq2GXihuUo%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emsdb.dbo.sysmail_event_log%3C%2FA%3E%2C%20and%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fsql%252Frelational-databases%252Fsystem-catalog-views%252Fsysmail-faileditems-transact-sql%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705021710%26amp%3Bsdata%3Dv0wCZULdcinCwxrMbf6iDXmLiwQvO3asJsKs3h2YmE8%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emsdb.dbo.sysmail_faileditems%3C%2FA%3E.%3C%2FLI%3E%0A%3CLI%3EScript%20the%20email%20profile%20that%20you%20are%20using%20on%20Managed%20Instance%2C%20setup%20the%20identical%20email%20profile%20on%20SQL%20Server%20and%20try%20to%20send%20the%20email%20there.%20If%20possible%2C%20try%20to%20place%20SQL%20Server%20in%20Azure%20Virtual%20machine%20in%20the%20same%20VNet%20where%20your%20Managed%20Instance%20is%20placed%20(in%20different%20subnet)%20to%20ensure%20that%20you%20have%20similar%20networking%20environment.%3C%2FLI%3E%0A%3CLI%3EFind%20more%20information%20in%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fprevious-versions%252Fsql%252Fsql-server-2008-r2%252Fms188663(v%253Dsql.105)%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705031718%26amp%3Bsdata%3DKtUSz%252BesObIb45PIvAcpYD%252BU4AW8Ss2dGmBh4khNVJE%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ETroubleshooting%20Database%20Mail%3C%2FA%3E%26nbsp%3Barticle.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId--814210754%22%20id%3D%22toc-hId--814210754%22%3ETroubleshooting%20SQL%20Agent%20email%20alerts%3C%2FH2%3E%0A%3CP%3EIf%20you%20are%20the%20experiencing%20issues%20with%20sending%20e-mail%20alerts%20from%20SQL%20Agent%2C%20try%20some%20of%20the%20following%20troubleshooting%20steps%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECheck%20do%20you%20have%20an%20email%20profile%20called%26nbsp%3B%3CSTRONG%3EAzureManagedInstance_dbmail_profile%3C%2FSTRONG%3E.%3C%2FLI%3E%0A%3CLI%3ETry%20to%20send%20an%20email%20using%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fsql%252Frelational-databases%252Fsystem-stored-procedures%252Fsp-send-dbmail-transact-sql%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705041726%26amp%3Bsdata%3DV6vNVQYbHUllXjqXOy0rJgMACsyt0rKym1RlIgmRkMc%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_send_dbmail%3C%2FA%3E%26nbsp%3Bprocedure%20on%26nbsp%3B%3CSTRONG%3EAzureManagedInstance_dbmail_profile%3C%2FSTRONG%3E%26nbsp%3Bprofile%20with%20T-SQL%20script.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CPRE%3EDECLARE%20%40body%20VARCHAR(4000)%20%3D%20'The%20email%20is%20sent%20from%20'%20%2B%20%40%40SERVERNAME%3B%0AEXEC%20msdb.dbo.sp_send_dbmail%20%20%0A%20%20%20%20%40profile_name%20%3D%20'AzureManagedInstance_dbmail_profile'%2C%20%20%0A%20%20%20%20%40recipients%20%3D%20'ADD%20YOUR%20EMAIL%20HERE'%2C%20%20%0A%20%20%20%20%40body%20%3D%20'Add%20some%20text'%2C%20%20%0A%20%20%20%20%40subject%20%3D%20'Azure%20SQL%20Instance%20-%20test%20email'%20%3B%20%20%3C%2FPRE%3E%0A%3CUL%3E%0A%3CLI%3ERepeat%20the%20steps%20from%20the%20previous%20section%20to%20troubleshoot%20the%20potential%20database%20email%20issues.%3C%2FLI%3E%0A%3CLI%3ECheck%20is%20there%20some%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fen-us%252Fazure%252Fsql-database%252Fsql-database-managed-instance-transact-sql-information%2523sql-server-agent%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705041726%26amp%3Bsdata%3DuGxXYkVgp%252FviFDRqssnMAAnlgA1pE1fYcV2SeBKa7t4%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESQL%20Agent%20limitation%3C%2FA%3E%26nbsp%3Bthat%20is%20causing%20this%20issue.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId-928599581%22%20id%3D%22toc-hId-928599581%22%3E%3CSTRONG%3ERecommended%20links%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fsql%252Frelational-databases%252Fdatabase-mail%252Fdatabase-mail%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705051735%26amp%3Bsdata%3DcLzAiqCSHgCbzFIR3f8FJTFOTT92CldZwNDsShx6x4M%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDatabase%20Mail%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fsql%252Frelational-databases%252Fdatabase-mail%252Fconfigure-database-mail%26amp%3Bdata%3D02%257C01%257Cjovanpop%2540microsoft.com%257C8879382478cc44ff634e08d6dad2acb2%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636936995705051735%26amp%3Bsdata%3D2Pll7rhjddNVZLFgK9XMTmfC06E%252FPyUkSaGIpwgHS0c%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConfigure%20Database%20Mail%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22http%3A%2F%2Fwww.midnightdba.com%2FDBARant%2Fcomplete-troubleshooting-guide-for-sql-server-databasemail-dbmail%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EComplete%20troubleshooting%20guide%20for%20SQL%20Server%20Databas%20mail%26nbsp%3B%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fwww.sqlshack.com%2Ftroubleshooting-database-mail-failures%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ETroubleshooting%20database%20mail%20failures%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20the%20following%20script%20is%20shown%20the%20source%20code%20of%20SQL%20Agent%20job%20that%20can%20test%20network%20connectivity%20form%20Managed%20Instance%20to%20mail%20server.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EUSE%20%5Bmsdb%5D%0A%0ADECLARE%20%40jobId%20BINARY(16)%0AEXEC%20msdb.dbo.sp_add_job%20%40job_name%3DN'test%20DbMail'%2C%20%0A%20%20%40enabled%3D1%2C%20%0A%20%20%40notify_level_eventlog%3D0%2C%20%0A%20%20%40notify_level_email%3D0%2C%20%0A%20%20%40notify_level_netsend%3D0%2C%20%0A%20%20%40notify_level_page%3D0%2C%20%0A%20%20%40delete_level%3D0%2C%20%0A%20%20%40description%3DN'No%20description%20available.'%2C%20%0A%20%20%40job_id%20%3D%20%40jobId%20OUTPUT%0A%0AEXEC%20msdb.dbo.sp_add_jobstep%20%40job_id%3D%40jobId%2C%20%40step_name%3DN'Test%20mail%20server'%2C%20%0A%20%20%40step_id%3D1%2C%20%0A%20%20%40cmdexec_success_code%3D0%2C%20%0A%20%20%40on_success_action%3D1%2C%20%0A%20%20%40on_success_step_id%3D0%2C%20%0A%20%20%40on_fail_action%3D2%2C%20%0A%20%20%40on_fail_step_id%3D0%2C%20%0A%20%20%40retry_attempts%3D0%2C%20%0A%20%20%40retry_interval%3D0%2C%20%0A%20%20%40os_run_priority%3D0%2C%20%40subsystem%3DN'PowerShell'%2C%20%0A%20%20%40command%3DN'tnc%20smtp.sendgrid.net%20-port%2025'%2C%20%0A%20%20%40database_name%3DN'master'%2C%20%0A%20%20%40flags%3D0%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%0AEXEC%20msdb.dbo.sp_add_jobserver%20%40job_id%20%3D%20%40jobId%2C%20%40server_name%20%3D%20N'(local)'%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-583018%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20post%20will%20help%20you%20to%20troubleshoot%20potential%20issues%20with%20Database%20Mail%20feature%20in%20Azure%20SQL%20Database%20Managed%20Instance%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-583018%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EInteroperability%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMaintenance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EManaged%20Instance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Azure SQL Database - Managed Instance enables you to directly send the email messages to the external email servers. You can send email messages directly using sp_send_dbmail procedure or via SQL Agent Jobs as alerts. Learn more about Database Mail here

 

In the previous article, I have explained how to configure mail profile and send emails on Managed Instance. Database mail is reliable API that you can use to deliver email messages directly within the Managed Instance to external email server; however, in some cases you might experience the issues due to incorrect configuration of mail account, network connection, etc.

In this article you will learn how to troubleshoot the potential issues with DbMail if you cannot send the emails.

 

Troubleshooting Database mail

If you are the experiencing the issues with sending e-mail messages, try some of the following troubleshooting steps:

  • Check is the option 'Database Mail XPs' enabled in sys.configurations view. If not, you can enable it using the following script:
sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
sp_configure 'Database Mail XPs', 1;  
GO  
RECONFIGURE  
GO  
  • Check have you correctly configured email profile with correct email server name/IP address, port, and account information (username and password)
  • Check could you reach the mail server from Managed Instance.
    • Check in Network Security Group associated to the Managed Instance subnet do you have outbound rule that allows communication via TCP port 25:

db-mail-nsg.PNG

 

    • Create SQL Agent job that has one PowerShell task that executes command like tns smpt.sendgrid.net -25 (see the example below), run the job and check the job output in the job history. This is public mail server that should be reached from your Managed Instance, unless if you have explicitly blocked that name and/or port.
    • Replace the name of the mail server and/or port in the job with the mail server and port that you are using and repeat the previous step.
    • Check have you enabled the port that is used to communicate with the email server. Port should be added in the Outbound security rules
  • Check the status of E-Mail messages sent with database mail in Database Mail Logmsdb.dbo.sysmail_event_log, and msdb.dbo.sysmail_faileditems.
  • Script the email profile that you are using on Managed Instance, setup the identical email profile on SQL Server and try to send the email there. If possible, try to place SQL Server in Azure Virtual machine in the same VNet where your Managed Instance is placed (in different subnet) to ensure that you have similar networking environment.
  • Find more information in Troubleshooting Database Mail article.

Troubleshooting SQL Agent email alerts

If you are the experiencing issues with sending e-mail alerts from SQL Agent, try some of the following troubleshooting steps:

  • Check do you have an email profile called AzureManagedInstance_dbmail_profile.
  • Try to send an email using sp_send_dbmail procedure on AzureManagedInstance_dbmail_profile profile with T-SQL script.
DECLARE @body VARCHAR(4000) = 'The email is sent from ' + @@SERVERNAME;
EXEC msdb.dbo.sp_send_dbmail  
    @profile_name = 'AzureManagedInstance_dbmail_profile',  
    @recipients = 'ADD YOUR EMAIL HERE',  
    @body = 'Add some text',  
    @subject = 'Azure SQL Instance - test email' ;  
  • Repeat the steps from the previous section to troubleshoot the potential database email issues.
  • Check is there some SQL Agent limitation that is causing this issue.

Recommended links

In the following script is shown the source code of SQL Agent job that can test network connectivity form Managed Instance to mail server.

 

USE [msdb]

DECLARE @jobId BINARY(16)
EXEC msdb.dbo.sp_add_job @job_name=N'test DbMail', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=0, 
		@notify_level_netsend=0, 
		@notify_level_page=0, 
		@delete_level=0, 
		@description=N'No description available.', 
		@job_id = @jobId OUTPUT

EXEC	msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Test mail server', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_success_step_id=0, 
		@on_fail_action=2, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'PowerShell', 
		@command=N'tnc smtp.sendgrid.net -port 25', 
		@database_name=N'master', 
		@flags=0


EXEC msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'