Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 945871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:49:22+00:00 2026-05-15T22:49:22+00:00

EDIT : Problem with the email I get mailbox unavailable exception! DBEmail.cs using System;

  • 0

EDIT : Problem with the email I get
mailbox unavailable exception!
DBEmail.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Xml.Linq;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure()]
    public static void DBEmail(string Sender, string SendTo, string Subject, string Body, string mailServer)
    {
        System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
        m.From = new System.Net.Mail.MailAddress(Sender);
        m.To.Add(new System.Net.Mail.MailAddress(SendTo));
        m.Subject = Subject;
        m.Body = Body;
        System.Net.Mail.SmtpClient client = null;
        client = new System.Net.Mail.SmtpClient();
        client.Host = mailServer;
        client.UseDefaultCredentials = false;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Credentials = new System.Net.NetworkCredential("User@MySite.com", "password");
        client.Send(m);
    }
}

I have to run some nightly stored procedures. So I have created a .sql file which has basically –

exec proc1
exec proc2...

So is it fine if i just run them like that or they need to wrapped in begin end or something else… Fruther I have a batch file which i will schedule to run-

sqlcmd -S myserver.myserver.com -i 
D:\Scripts\StartProcs.sql -U username -P password -o D:\Scripts\log.txt

Is this the above script correct ? I mean it works it tested running it…but i just need to make sure if i am not missing any other issues here ? Because I saw some other sqlcmd commands which where really long and took lot of parameters…I just want to make sure i am not missing any important parameters..

Please correct me above with either .sql file or batch file…if i have not taken in consideration any issues? …Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:49:23+00:00Added an answer on May 15, 2026 at 10:49 pm

    The only problem I see is that with this technique (unless you have something built into the procs) you won’t be automatically notified if something goes wrong, or in that case if it completes successfully.

    In SQL Server Express you can add a CLR Function that you can use to send you an email reporting the results of the sprocs above.

    And here is how you would do it using all free tools.

    First Make sure CLR Integration is enabled by executing this

    sp_configure 'show advanced options', 1;
    GO
    RECONFIGURE;
    GO
    sp_configure 'clr enabled', 1;
    GO
    RECONFIGURE;
    GO
    

    You will also need to either sign the assembly or mark the DB as Trustworthy. To mark the DB Trustworthy make sure you are logged into SQL Server as a member of the SysAdmin role and Execute this

    ALTER DATABASE YourDBName SET TRUSTWORTHY ON;
    

    Next save the following as C:\Code\DBEmail.cs

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Server;
    
    public partial class StoredProcedures
    {
        [Microsoft.SqlServer.Server.SqlProcedure()]
        public static void DBEmail(string Sender, string SendTo, string Subject, string Body, string mailServer)
        {
            System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
            m.From = new System.Net.Mail.MailAddress(Sender);
            m.To.Add(new System.Net.Mail.MailAddress(SendTo));
            m.Subject = Subject;
            m.Body = Body;
    
            System.Net.Mail.SmtpClient client = null;
            client = new System.Net.Mail.SmtpClient();
            client.Host = mailServer;
    
            client.Send(m);
        }
    }
    

    Then find the C# Comand line compiler in the appropriate .Net Framework directory, in my case it is “C:\Windows\Microsoft.NET\Framework\v2.0.50727”) cd to it and execute this.

    csc.exe /target:library /out:C:\Code\DBEmail.dll C:\Code\DBEmail.cs
    

    Then copy the resulting C:\Code\DBEmail.dll to the SQL Server if you are not on it already and in Management studio execute this.

    CREATE ASSEMBLY [DBEmail]
    AUTHORIZATION [dbo]
    From 'C:\Code\DBEmail.dll'
    WITH PERMISSION_SET = External_Access
    

    Finally create a sproc to map to the CLR Code by executing this

    CREATE PROCEDURE [dbo].[usp_DBEmail]
        @Sender [nvarchar](255),
        @SendTo [nvarchar](255),
        @Subject [nvarchar](255),
        @Body [nvarchar](max),
        @mailserver [nvarchar](55)
    WITH EXECUTE AS CALLER
    AS
    EXTERNAL NAME [DBEmail].[StoredProcedures].[DBEmail]
    

    Then you can just wrap the code inside your scheduled sprocs in Try Catch and if an error occurs send youself the error message in the email like this.

    BEGIN TRY
      --Your SQL Stuff
    END TRY
    BEGIN CATCH
        Declare @error as nvarchar(max);
        SELECT @error = ERROR_NUMBER();
        Exec usp_DBEmail @Sender='sender@xyz.com', @SendTo='you@xyz.com', @subject='Whoops', @Body=@error, @mailserver='smtp_ip'
    END CATCH;
    

    Hope this is helpful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well you're creating two separate instances of Globals, to start… May 16, 2026 at 10:26 am
  • Editorial Team
    Editorial Team added an answer I would personally store the output of PHP time() in… May 16, 2026 at 10:26 am
  • Editorial Team
    Editorial Team added an answer Looking at the Javascript resources linked to the page, there… May 16, 2026 at 10:26 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm having a problem using imaplib on python 2.6 with the latest django svn.
I'm having a problem updating a row after an inline edit. My ColModel is:
I have a registration form which displays a users Name (textbox), Email (textbox) and
Update after Bounty was awarded A new solution is coming up to this problem.
I'm currently using DataAnnotations to validate my MVC 2 app. However, I've ran into
I'm using ASP.NET MVC 2.0 and I am trying to take advantage of the
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@
I'm setting up my web server on Amazon's EC2. My site used to run
Here's the problem in a nutshell: <bean id=handlerFactory class=com.westfieldgrp.audit.jdklogging.cpm.CPMHandlerFactory> <property name=schemaName value=${env.audit.databaseSchema} /> <property
(EDIT: I've edited my question to make it simpler, sorry if some answers are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.