|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, January 14, 2013 10:42 AM
Points: 19,
Visits: 76
|
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, November 14, 2011 6:35 PM
Points: 67,
Visits: 21
|
|
A friend of mine took the Microsoft example proc and produced a more robust version. It defaults to using the servername from @@servername if one is not provided, and it can send file attachments:
- use
mastergoCREATE PROCEDURE [dbo].[sp_sendmail_CDOSYS] @To varchar(100) ,@Subject varchar(100)=" ",@Body varchar(4000) =" ",@Importance int = 1, -- 0=low, 1=normal, 2=high@CC varchar(100)= " " ,@Bcc varchar(100)= " " ,@Attachments varchar(8000)=NULL, /* seperated by ; */ @HTMLFormat int = 0,@From varchar(100)= null , @Server varchar(255) = null,@UserName varchar(255) = null,@password varchar(255) = null/*********************************************************************Created by : Corey EmbryDescription: Use CDOSYS to Send Mail using smtpBase Code was obtained from MS Article Q312839 This stored procedure takes the parameters and sends an e-mail. All the mail configurations are hard-coded in the stored procedure. Comments are added to the stored procedure where necessary.References to the CDOSYS objects are at the following MSDN Web site:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_messaging.aspexec sp_sendmail_CDOSYS @To = 'Coreye@avalamarketing.com' , @Subject = 'CDOSYStest' -- Optional, @Body ='this is a test' -- Optional, @Importance int = 1 -- Optional, @CC = 'mickey@disney.com' -- Optional, @Bcc = 'Minnie@disney.com' -- Optional , @Attachments = 'c:\test.txt' -- Optional ; Delimited, @HTMLFormat = 0 -- Optional Default Text-- , @From = 'Coreye@avalamarketing.com' -- Optional, @Server='avalafs1' -- Optional-- , @UserName= -- Optional-- , @Password= -- Optional***********************************************************************/ ASSELECT @Server = isnull(@Server, @@SERVERNAME)Select @From = isnull(@From, @@SERVERNAME)Declare @iMsg intDeclare @hr intDeclare @int intDeclare @tmp varchar(8000)Declare @source varchar(255)Declare @description varchar(500)Declare @output varchar(1000)--************* Create the CDO.Message Object ************************EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT--***************Configuring the Message Object ******************-- This is to configure a remote SMTP server.-- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_sendusing.aspEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'-- This is to configure the Server Name or IP address. -- Replace MailServerName by the name or IP of your SMTP Server.EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @Server if @UserName is not null and @password is not null beginEXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/SendUserName").Value', @UserName EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/SendPassword").Value', @Password end-- Save the configurations to the message object.-- EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null-- Set the e-mail parameters.EXEC @hr = sp_OASetProperty @iMsg, 'To', @ToEXEC @hr = sp_OASetProperty @iMsg, 'From', @FromEXEC @hr = sp_OASetProperty @iMsg, 'Subject', @SubjectEXEC @hr = sp_OASetProperty @iMsg, 'CC', @CC EXEC @hr = sp_OASetProperty @iMsg, 'BCC', @BCC/* if there are any attachments */ if len(@Attachments)>0 begin while len(@Attachments)>0 begin set @int = charindex(';',@Attachments) if @int > 0 begin set @tmp = left(@Attachments,@int-1) set @Attachments = right(@Attachments,(len(@Attachments)-@int)) end else begin set @tmp = @Attachments set @Attachments = '' end /* Add the Attachment */ Print @tmpEXEC @hr= sp_OAMethod @iMsg, 'AddAttachment', NULL, @tmp end end -- If you are using HTML e-mail, use 'HTMLBody' instead of 'TextBody'.if @HTMLFormat <> 1beginEXEC @hr = sp_OASetProperty @iMsg, 'TextBody', @BodyendelsebeginEXEC @hr = sp_OASetProperty @iMsg, 'HTMLBody', @Bodyend-- Save the configurations to the message object.EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', nullEXEC @hr = sp_OAMethod @iMsg, 'Send', NULL-- Sample error handling.IF @hr <>0 select @hrBEGINEXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUTIF @hr = 0BEGINSELECT @output = ' Source: ' + @sourcePRINT @outputSELECT @output = ' Description: ' + @descriptionPRINT @outputENDELSEBEGINPRINT ' sp_OAGetErrorInfo failed.'RETURNENDEND-- Do some error handling after each step if you have to.-- Clean up the objects created.EXEC @hr = sp_OADestroy @iMsgGO
Kindest Regards,
Clayton Groom covenanttechnologypartners.com
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Saturday, February 04, 2012 2:20 PM
Points: 11,
Visits: 58
|
|
This technique is an oldie but a goodie. One of Clinton Herring's contributions to "Script of the Day" a while back (I can't remember the date, but the script is dated 08/22/2001) does this too, and, might I add, is a bit more thorough than both examples posted above (Perhaps it gets overlooked because the script title is potentially misleading). In any event, Clinton's version of this idea uses an OLE automation implementation of the CDOSYS dll, includes error handling, a help/usage message, a clever technique to verify attachments, and, IMHO, is very well-factored. In addition to any attachments you specify, Clinton's script can also perform a user-defined SQL Query (passed as a parameter to the SPROC), pipe the query results out to a text file (to the drive wih the most free space!), and attach the report to the email! A great idea for automating server reports and emailing them to the DBA, or, generating application-specific reports and emailing them to your business stakeholders. See what you think! SQL 2K SMTP mail on Windows 2K http://www.sqlservercentral.com/scripts/contributions/510.asp Script Rating Total number of votes [86] By: Clinton Herring SP_SQLSMTPMail is an OLE automation implementation of the CDOSYS dll for Windows 2000 which utilizes a network SMTP server rather than an Exchange server/Outlook client. The stored procedure functions similar to xp_sendmail including the ability to run a query and attach the results. No MAPI profile is required. It is also a working, detailed example of an OLE automation implementation. This update corrects a problem when the proc is called twice in the same batch without an intervening 'Go'. The cause is the sp_OAStop. It needs to be removed or commented out. The stated method of operation in the BOL is incorrect. 11/5/2002 Some people have reported errors when running this stored procedure. They have not been failures of the stored procedure. They are errors related to improper configuration/permissions for the SQL server to use the local network SMTP relay server for either internal or out going mail. 11/20/2002 Fixes a problem related to the OSQL call to send an attached query. OSQL was not releasing its lock on the first output file it created until the session ended, hence, calling the proc in a cursor or loop prevented subsequent query attachments. 04/09/2003 Comment correction.
Best, Ken 
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 01, 2010 7:33 AM
Points: 6,
Visits: 24
|
|
Works great, just do not forget to remove the space in this line!
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas. microsoft.com/cdo/configuration/smtpserver").Value', 'MailServerName'
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 4:04 AM
Points: 95,
Visits: 62
|
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 5:03 PM
Points: 74,
Visits: 151
|
|
I am using very simple VBscript to achieve the same thing, and attachements are allowed as well after small modification -------------------------------------- Set oMsg = CreateObject("CDONTS.NewMail") oMsg.From = "abc" oMsg.To = "abc" oMsg.Subject = "you message" oMsg.Body = "you message" oMsg.Send Set oMsg = Nothing ------------------------------------- Julia
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 7:34 AM
Points: 21,
Visits: 87
|
|
Hi Clayton, I can't see how the Importance flag will work. You didn't call the method anywhere in the code?
Regads, Andy
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 11, 2005 9:20 AM
Points: 3,
Visits: 1
|
|
I like the suggestions, and the ability to attach a SQL query output to an email. I know the following works - EXEC ('master..xp_cmdshell ''isql /o' + @filename + ' /d' + @database + ' /Q"' + @SQLquery + '" /E''') But the only problem with the above is that you need SYSADMIN access to run XP_CMDSHELL I also do NOT want to use XP_SENDMAIL as it uses MAPI . Is there any other way to attach SQL queries to an email ??
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Today @ 10:51 AM
Points: 326,
Visits: 211
|
|
Has anyone been able to make the "Importance" or "Priority" work using CDOSYS ?
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, February 18, 2010 11:45 AM
Points: 14,
Visits: 4
|
|
I was using the sqlsmtpmail code and attaching a step to all of my jobs to exec the proc on failures. Problem is, how can I put the Job Name and/or step name that failed in the subject and/or body of the emails? I have everything else working. Thanks all!
|
|
|
|