Email sending script from C#

Sending an email from C# script is quite easy thing. Most popular two ways to send an email from C# scripts are as follows:
1.)
public string sendingMail(string strFrom, string strTo, string strSubject, string strMessage)
{
MailMessage myMessage = new MailMessage(new MailAddress(strFrom), new MailAddress(strTo));
myMessage.IsBodyHtml = true;
myMessage.Priority = MailPriority.Normal;
myMessage.Body = strMessage;
myMessage.Subject = strSubject;
SmtpClient client = new SmtpClient(““, 25);
try
{
client.Send(myMessage);
}
catch(Exception ex)
{
return ex.ToString();
}
return “Message Has Been Sent”;
}

2.)
public string AuthenticatedMailSend(string mailTo, string Subject, string MessageBody)
{
System.Net.Mail.MailMessage email = new MailMessage(“TEST@test.com”, mailTo);
email.Subject = Subject;
email.IsBodyHtml = true;
email.Body = MessageBody;

System.Net.Mail.SmtpClient mailClient = new SmtpClient();
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(““, ““);
mailClient.Host = ““;
mailClient.Port = 25;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;

try
{
mailClient.Send(email);
}
catch (Exception ex)
{
return ex.ToString();
}

return “1”;
}

You ccan use any of the above methods, some mail server only supports authenticated email send by script. In that case, go for 2nd method.

Don’t forget to reference “using System.Net.Mail;” namespace for sending an email.

Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah

SQL Server Truncate Transaction Log script

Many time, it happens in live sql server which has big transaction log file, you run DBCC SHRINKFILE command and see no effect of it on the transaction log file. You can use following magical five line at that time and you will done. It shrinks the trucated log File to minimum size possible.
————————————————————————-
USE DBName
GO
DBCC SHRINKFILE(transactionlogfilename, 1)
BACKUP LOG WITH TRUNCATE_ONLY
DBCC SHRINKFILE(transactionlogfilename, 1)
————————————————————————
Happy Working!!!!

Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah