SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sending E-Mail from SQL 2008 Express


Sending E-Mail from SQL 2008 Express

Author
Message
APrisk2
APrisk2
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 93

using System.Net;
using System.Net.Mail;


public class SQLCLREmail
{
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void SendEmail(string recipients, string CC, string BCC, string subject, string @from, string body, string strAttachments, string strSMTPServer, string strSMTPServerPort, string strSMTPServerUser,
string strSMTPServerPwd)
{

using (MailMessage MailMsg = new MailMessage()) {

MailMsg.From = new MailAddress(@from);
MailMsg.Subject = subject;
MailMsg.Body = body;
MailMsg.IsBodyHtml = true;

if (!recipients.Equals(string.Empty)) {
string strRecip = null;
string[] strTo = recipients.Split(";");
foreach ( strRecip in strTo) {
MailMsg.To.Add(new MailAddress(strRecip));
}
}

if (!CC.Equals(string.Empty)) {
string strCCRecip = null;
string[] strCCTo = CC.Split(";");
foreach ( strCCRecip in strCCTo) {
MailMsg.CC.Add(new MailAddress(strCCRecip));
}
}

if (!BCC.Equals(string.Empty)) {
string strBCCRecip = null;
string[] strBCCTo = BCC.Split(";");
foreach ( strBCCRecip in strBCCTo) {
MailMsg.BCC.Add(new MailAddress(strBCCRecip));
}
}

if (!strAttachments.Equals(string.Empty)) {
string strFile = null;
string[] strAttach = strAttachments.Split(";");
foreach ( strFile in strAttach) {
MailMsg.Attachments.Add(new Net.Mail.Attachment(strFile.Trim()));
}
}

if (!strSMTPServer.Equals(string.Empty)) {
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
{
if (!strSMTPServerUser.Equals(string.Empty)) {
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(strSMTPServerUser, strSMTPServerPwd);
}
smtp.Host = strSMTPServer;
smtp.Port = strSMTPServerPort;
smtp.Send(MailMsg);
}
} else {
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "localhost";
smtp.Port = 25;
smtp.Send(MailMsg);
}
}

}
}
}


Jonathan manley
Jonathan manley
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 9
mbova407 (11/4/2010)
Good Article. Two suggestions

I would make the attribute appear in the same code window as the method. At first it looks confusing because you have
<Microsoft.SqlServer.Server.SqlProcedure()> _

That ends in a continuation character that goes nowhere.

Then maybe a little more explanation as to why this attribute is needed.

Represents an abstraction of the caller's context, which provides access to the SqlPipe, SqlTriggerContext, and WindowsIdentity objects.

or
The SqlContext object can be used to obtain the trigger context (for code executing in a trigger) and the pipe object (when available, such as in stored procedures).

Definitions are taken from the MSDN.


The Actual VB code should look like this:

Public Class SQLCLREmail
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub SendEmail(ByVal recipients As String _
, ByVal CC as String _
, ByVal BCC as String _
, ByVal subject As String _
, ByVal from As String _
, ByVal body As String _
, ByVal strAttachments as String _
, ByVal strSMTPServer as String _
, ByVal strSMTPServerPort as String _
, ByVal strSMTPServerUser as String _
, ByVal strSMTPServerPwd as String)


With the start of the Sub going directly under the Attribute hence the line continuation.
John Pluchino
John Pluchino
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 60
The source code displayed in a very small window which did not scroll right far enough to see all of the code on certain lines that were long. Is there a way to expand the viewing window? Is there a way to copy the source code samples? I am new at this so my question may seem trivial but it is a show stopper for me. Thanks for any hints you might provide...John
Charles Kincaid
Charles Kincaid
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4513 Visits: 2384
Very good article. I had fogotten that the CLR was available in Express. There are all kinds of things that you can do in the CLR that can't (or should not) be done in regulare SQL. I say that as you can do much more extensive error trapping and security checking in an easier manner in C# or VB than the SQL syntax. Grunting through strings is procedural and this can give you better speed.

ATBCharles Kincaid
Jonathan manley
Jonathan manley
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 9
JPluchino (11/4/2010)
The source code displayed in a very small window which did not scroll right far enough to see all of the code on certain lines that were long. Is there a way to expand the viewing window? Is there a way to copy the source code samples? I am new at this so my question may seem trivial but it is a show stopper for me. Thanks for any hints you might provide...John


Using your mouse start at the upper left side and you should get a text selector mouse icon, left click and hold, then drag you mouse to the bottom.
karen.scott260
karen.scott260
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 10
When I try to execute the sp in SQL this is the error that I get. Does anyone know what the problem could be?


Msg 6522, Level 16, State 1, Procedure p_SendEMail, Line 0
A .NET Framework error occurred during execution of user-defined routine or aggregate "p_SendEMail":
System.NullReferenceException: Object reference not set to an instance of an object.
System.NullReferenceException:
at SQLCLREmail.SendEmail(String recipients, String CC, String BCC, String subject, String from, String body, String strAttachments, String strSMTPServer, String strSMTPServerPort, String strSMTPServerUser, String strSMTPServerPwd)
.
Jonathan manley
Jonathan manley
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 9
karen.scott260 (11/4/2010)
When I try to execute the sp in SQL this is the error that I get. Does anyone know what the problem could be?


Msg 6522, Level 16, State 1, Procedure p_SendEMail, Line 0
A .NET Framework error occurred during execution of user-defined routine or aggregate "p_SendEMail":
System.NullReferenceException: Object reference not set to an instance of an object.
System.NullReferenceException:
at SQLCLREmail.SendEmail(String recipients, String CC, String BCC, String subject, String from, String body, String strAttachments, String strSMTPServer, String strSMTPServerPort, String strSMTPServerUser, String strSMTPServerPwd)
.

Check the values that are being passed in with profiler or something to make sure they have a value and are not null.
For example make sure that you are passing in a from, recipents, body, smtpserver, port user and password.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)

Group: General Forum Members
Points: 340435 Visits: 42644
I have to ask because I just don't know... Hopefully, one of you do. Is CDONTS and sp_OA* available in SQL Server Express?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
mytrashcan
mytrashcan
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 26
This has started me on my way to looking into CLR linked procs as well; however, I cant get this to work.
I am using smtp.gmail.com as my smtp server, also with my email ID and password as my smtp User and PWD and keeping the port at 25. I get the following error:

Msg 6522, Level 16, State 1, Procedure p_SendEMail, Line 0
A .NET Framework error occurred during execution of user-defined routine or aggregate "p_SendEMail":
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.157.109:25
System.Net.Sockets.SocketException:
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
System.Net.WebException:
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
System.Net.Mail.SmtpException:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at SQLCLREmail.SendEmail(String recipients, String CC, String BCC, String subject, String from, String body, String strAttachments, String strSMTPServer, String strSMTPServerPort, String strSMTPServerUser, String strSMTPServerPwd)

Any and all help is greatly appreciated.
.
blessonblessons
blessonblessons
Old Hand
Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)

Group: General Forum Members
Points: 371 Visits: 420
@mytrashcan the reason for error is related port number/ipaddress of smtp server/firewall setting.
The port number used by gmail is 587 rather than 25 and to find ipaddress ping "smtp.gmail.com" (you can even use smtp.gmail.com).
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search