Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Sending E-Mail from SQL 2008 Express Expand / Collapse
Author
Message
Posted Thursday, November 4, 2010 7:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 11:27 AM
Points: 5, Visits: 78
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);
}
}

}
}
}

Post #1015861
Posted Thursday, November 4, 2010 7:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 6, 2011 4:50 PM
Points: 4, 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.
Post #1015864
Posted Thursday, November 4, 2010 9:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 28, 2014 10:55 AM
Points: 13, 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
Post #1015985
Posted Thursday, November 4, 2010 10:56 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 11:42 AM
Points: 821, Visits: 2,028
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.



ATB

Charles Kincaid

Post #1016041
Posted Thursday, November 4, 2010 11:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 6, 2011 4:50 PM
Points: 4, 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.
Post #1016049
Posted Thursday, November 4, 2010 11:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 11, 2012 9:05 AM
Points: 1, 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)
.
Post #1016072
Posted Thursday, November 4, 2010 11:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 6, 2011 4:50 PM
Points: 4, 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.
Post #1016080
Posted Thursday, November 4, 2010 7:46 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1016353
Posted Thursday, November 4, 2010 8:24 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 8:03 AM
Points: 7, 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.
.
Post #1016368
Posted Thursday, November 4, 2010 11:58 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, October 16, 2014 1:00 PM
Points: 55, Visits: 416
@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).
Post #1016387
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse