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 Wednesday, November 03, 2010 9:54 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 03, 2014 9:17 AM
Points: 5, Visits: 75
Comments posted to this topic are about the item Sending E-Mail from SQL 2008 Express
Post #1015669
Posted Thursday, November 04, 2010 2:19 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: Wednesday, February 05, 2014 6:49 AM
Points: 861, Visits: 793
Nice one Andrew. I can hardy wait to test it.

Regards,
Iulan
Post #1015720
Posted Thursday, November 04, 2010 2:54 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 07, 2012 1:39 AM
Points: 7, 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)
.
Post #1015732
Posted Thursday, November 04, 2010 3:02 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, April 12, 2014 12:02 PM
Points: 55, Visits: 409
Nice Article.
We can create a linked server and access the msdb.dbo.sp_send_dbmail.
Post #1015735
Posted Thursday, November 04, 2010 3:35 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, March 20, 2014 7:32 AM
Points: 5,191, Visits: 1,368
Good article... Nice


Post #1015749
Posted Thursday, November 04, 2010 3:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, November 19, 2010 3:35 AM
Points: 1, 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.
Post #1015750
Posted Thursday, November 04, 2010 4:14 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, July 19, 2013 2:44 AM
Points: 77, Visits: 201
You have just introduced me to CLR...Thanks for a great article!!
Post #1015757
Posted Thursday, November 04, 2010 4:37 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 6:27 AM
Points: 1,378, Visits: 454
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.
Post #1015764
Posted Thursday, November 04, 2010 7:32 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 11:01 AM
Points: 519, Visits: 2,735
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);
}
}
}
}
}
}




Post #1015856
Posted Thursday, November 04, 2010 7:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 06, 2011 4:50 PM
Points: 4, 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.
Post #1015860
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse