Live with Dot Net Just another Programming weblog

How to send mail using smtpclient

Posted on September 19, 2011

You can create a Mail Message dynamically and send the mail to recepient list through smtp client. You need to use System.Net.Mail to use the smtp client class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System.Configuration;
using System.Net.Mail;
 
        public static bool SendMailNotification()
        {
            MailMessage msg = new MailMessage();
            try
            {
                StringBuilder strBody = new StringBuilder("");
                strBody.Append("<span style='font-size: 9pt; font-family: Arial'>" + team + " Message body goes here<b>" + DateTime.Now.ToString("MMMM") + "</b><br /> ");
                strBody.Append("<br/><span style='font-size: 16pt; font-family: Arial'>Message body goes here<b>" + "</b></span><br /> <br />");
 
                string[] lsMailList = "Get mail receiptent list from database";
 
                foreach (string mail in lsMailList)
                {
                    msg.To.Add(mail);
                }
                msg.From = new MailAddress("from@domain.com");
                msg.Subject = "any Subject to the mail";
                AlternateView Body = AlternateView.CreateAlternateViewFromString(strBody.ToString(), null, "text/html");
                msg.AlternateViews.Add(Body);
                SendMail(msg);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
        public static void SendMail(MailMessage msg)
        {
            try
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Host =  ConfigurationSettings.AppSettings["MailServer"].ToString(); //relay server name
                smtp.Send(msg);
            }
            catch { }
        }

Below is a MySql trigger to insert/update other table

Posted on September 18, 2011

Trigger in backend is very useful to report the input data in many different way.. This will be last step in performance tuning when the data double up drastically.

DROP trigger IF EXISTS "Trigger Name";

delimiter $$
CREATE TRIGGER "Trigger Name"
AFTER INSERT ON "Table Name - where the trigger will be fired on Insert/Update"
FOR EACH ROW

BEGIN

Declare l_count, l_topErrorCount INT;
Declare l_Top1Error, l_Top2Error, l_Top3Error VARCHAR(150);

//To determine where you need to update or insert the value in other table
SELECT COUNT(*) INTO l_topErrorCount FROM "Table Name"
WHERE Team = NEW.Team
AND Date = NEW.ErrorDate
AND Report = NEW.Report;

////
You Logic goes here.....
//Retrive Top Error...//
////
IF l_topErrorCount > 0 THEN
UPDATE "Table Name"
SET Top1Error= l_Top1Error,
Top2Error=l_Top2Error,
Top3Error=l_Top3Error
WHERE Team = NEW.Team
AND Date = NEW.ErrorDate
AND Report = NEW.Report;
ELSE
INSERT INTO "Table Name"
(Date, Team, Report, Top1Error, Top2Error, Top3Error)
VALUES(NEW.ErrorDate, NEW.Team, NEW.Report, l_Top1Error, l_Top2Error, l_Top3Error);
END IF;

END;
$$

Dynamically adding ItemTemplate to the GridView holding controls

Posted on September 16, 2008

The code helps you to add Item template (or any part of templated column) to the Gridview dynamically. We has also set/add desired controls to the Item templates.

We have a class which will be used to set the item template control into the Gridview.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int index = 0; index < allRoles.Length; index++)
        {
            Table.Columns.Add(allRoles[index].Replace(" ", "_"));
 
            TemplateField tempField = new TemplateField();
            tempField.HeaderText = allRoles[index];
            tempField.ItemStyle.VerticalAlign = VerticalAlign.Middle;
            tempField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            //chBox.ID = allRoles[index].Replace(" ", "_");
            tempField.ItemTemplate = new GridViewTemplate(ListItemType.Item, allRoles[index].Replace(" ", "_"));
            if (IsPostBack)
            {
                grvManageSecurity.Columns.RemoveAt(1);
            }
            grvManageSecurity.Columns.Add(tempField);
        }

CLASS: Used to set the controls into Item template and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
//A customized class for displaying the Template Column
public class GridViewTemplate : ITemplate
{
    //A variable to hold the type of ListItemType.
    ListItemType _templateType;
 
    //A variable to hold the column name.
    string _columnName;
 
    //Constructor where we define the template type and column name.
    public GridViewTemplate(ListItemType type, string colname)
    {
        //Stores the template type.
        _templateType = type;
 
        //Stores the column name.
        _columnName = colname;
    }
 
    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {
        switch (_templateType)
        {
            case ListItemType.Header:
                //Creates a new label control and add it to the container.
                Label lbl = new Label();            //Allocates the new label object.
                lbl.Text = _columnName;             //Assigns the name of the column in the lable.
                container.Controls.Add(lbl);        //Adds the newly created label control to the container.
                break;
 
            case ListItemType.Item:
                //Creates a new text box control and add it to the container.
                CheckBox chkField = new CheckBox();
                chkField.ID = _columnName;
            //Allocates the new text box object.
                container.Controls.Add(chkField);                            //Adds the newly created textbox to the container.
                break;
 
            case ListItemType.EditItem:
                //As, I am not using any EditItem, I didnot added any code here.
                break;
 
            case ListItemType.Footer:
                CheckBox chkColumn = new CheckBox();
                chkColumn.ID = "Chk" + _columnName;
                container.Controls.Add(chkColumn);
                break;
        }
    }
 
    /// <summary>
    /// This is the event, which will be raised when the binding happens.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void tb1_DataBinding(object sender, EventArgs e)
    {
        TextBox txtdata = (TextBox)sender;
        GridViewRow container = (GridViewRow)txtdata.NamingContainer;
        object dataValue = DataBinder.Eval(container.DataItem, _columnName);
        if (dataValue != DBNull.Value)
        {
            txtdata.Text = dataValue.ToString();
        }
    }
}