Click here to monitor SSC
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
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 93
Comments posted to this topic are about the item Sending E-Mail from SQL 2008 Express
Iulian -207023
Iulian -207023
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

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

Regards,
Iulan
amet gbas
amet gbas
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: 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
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

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

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



ashwin.nprabhu
ashwin.nprabhu
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: 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 Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

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

Group: General Forum Members
Points: 1454 Visits: 513
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
Mr or Mrs. 500
Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)Mr or Mrs. 500 (571 reputation)

Group: General Forum Members
Points: 571 Visits: 3439
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
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 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