Friday 24 June 2011

How to get month name from from SQL Server datetime field?

1) Below query return month name (from January to December) based on your record date.

select DATENAME(month,fieldname) from tablename 

2) Below query is used to get only three charecter from the selected month
select Convert(varchar(3),DATENAME(month,empdate)) from empdt  (or)
select Cast(DATENAME(month,empdate) as varchar(3)) from empdt

Sending email in ASP.NET using SMTP configuration from web.config

Configure your email settings in Web.Config with your account details.
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" port="25" userName="youremail@gmail.com" password="yourpassword" defaultCredentials="false"/>
</smtp>          
</mailSettings>
</system.net>    

Send email with above SMTP settings like this
protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage("frmid@yourdomain.com", "tomailid@anydomain.com");
        SmtpClient mailClient = new SmtpClient();
        //Assign your Mail Body text
        msg.Body = "test mail body";
        //Assign Subject of the e-mail
        msg.Subject = "subject";       
        mailClient.EnableSsl = true;
 //if you use yahoo then change mailClient.EnableSsl = false;
       //Send mail using Web.config setting
        mailClient.Send(msg);
    }