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);
}
}
}
}
}