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
Comments posted to this topic are about the item Sending E-Mail from SQL 2008 Express
Iulian -207023
Iulian -207023
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3553 Visits: 1248
Nice one Andrew. I can hardy wait to test it.

Regards,
Iulan
amet gbas
amet gbas
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 39
hi..

tested it an get this error while trying to run stored procedure;


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.Security.SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Net.Mail.SmtpClient.set_Port(Int32 value)
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)
.
blessonblessons
blessonblessons
SSC-Addicted
SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)

Group: General Forum Members
Points: 423 Visits: 420
Nice Article.
We can create a linked server and access the msdb.dbo.sp_send_dbmail.
Anipaul
Anipaul
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12615 Visits: 1407
Good article... Nice



ashwin.nprabhu
ashwin.nprabhu
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 6
The article is very interesting, but just wanted to ask if the same thing can be explained using C# as reference for people like me.
AJN
AJN
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 201
You have just introduced me to CLR...Thanks for a great article!!
Mike Is Here
Mike Is Here
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2154 Visits: 516
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.
dmbaker
dmbaker
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2120 Visits: 3644
ashwin.nprabhu (11/4/2010)
The article is very interesting, but just wanted to ask if the same thing can be explained using C# as reference for people like me.


I don't think I would code it this way, but here's a more-or-less exact translation into C#. One issue is that the SMTP port is an integer, not a string. I'm not sure how that's working in VB.NET, guess is that VB implicitly casts it to the correct type. Don't know if you need namespace in a CLS proc either, but the code in the class should be "right".

This is .NET 4.0, 2.0 or 3.5 should be pretty easy to translate into if you need to.


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

namespace YourNamespaceHere
{
public class SQLCLREmail
{
[Microsoft.SqlServer.Server.SqlProcedure]
public void SendMail(string recipients, string CC, string BCC, string subject, string from, string body, string strAttachments, string strSMTPServer, int intSMTPServerPort, string strSMTPServerUser, string strSMTPServerPwd)
{
using (MailMessage MailMsg = new MailMessage())
{
MailMsg.From = new MailAddress(from);
MailMsg.Subject = subject;
MailMsg.Body = body;
MailMsg.IsBodyHtml = true;

if (!String.IsNullOrWhiteSpace(recipients))
{
string[] strTo = recipients.Split(';');
foreach (string strRecip in strTo)
{
MailMsg.To.Add(new MailAddress(strRecip));
}
}

if (!String.IsNullOrWhiteSpace(CC))
{
string[] strCCTo = CC.Split(';');
foreach (string strCCRecip in strCCTo)
{
MailMsg.To.Add(new MailAddress(strCCRecip));
}
}

if (!String.IsNullOrWhiteSpace(BCC))
{
string[] strBCCTo = BCC.Split(';');
foreach (string strBCCRecip in strBCCTo)
{
MailMsg.To.Add(new MailAddress(strBCCRecip));
}
}

if (!String.IsNullOrWhiteSpace(strAttachments))
{
string[] strAttach = strAttachments.Split(';');
foreach (string strFile in strAttach)
{
MailMsg.Attachments.Add(new Attachment(strFile.Trim()));
}
}

if (!String.IsNullOrWhiteSpace(strSMTPServer))
{
SmtpClient smtp = new SmtpClient();
if (!String.IsNullOrWhiteSpace(strSMTPServerUser))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(strSMTPServerUser, strSMTPServerPwd);
smtp.Host = strSMTPServer;
smtp.Port = intSMTPServerPort;

smtp.Send(MailMsg);
}
else
{
smtp.Host = "localhost";
smtp.Port = 25;
smtp.Send(MailMsg);
}
}
}
}
}
}





Jonathan manley
Jonathan manley
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 9
Use the web based VB.NET to C# Converter:
http://www.developerfusion.com/tools/convert/vb-to-csharp/

Works pretty good, and gets most of this syntax converted.
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