|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, April 26, 2013 9:30 PM
Points: 5,
Visits: 63
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Saturday, March 16, 2013 9:53 AM
Points: 847,
Visits: 768
|
|
Nice one Andrew. I can hardy wait to test it.
Regards, Iulan
|
|
|
|
|
Forum 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) .
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Wednesday, February 06, 2013 4:46 AM
Points: 55,
Visits: 391
|
|
Nice Article. We can create a linked server and access the msdb.dbo.sp_send_dbmail.
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Today @ 6:03 AM
Points: 4,786,
Visits: 1,335
|
|
|
|
|
|
Forum 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.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 9:24 AM
Points: 70,
Visits: 193
|
|
| You have just introduced me to CLR...Thanks for a great article!!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 6:38 AM
Points: 1,326,
Visits: 420
|
|
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.
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 6:53 AM
Points: 503,
Visits: 2,495
|
|
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); } } } } } }
|
|
|
|
|
Forum 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.
|
|
|
|