Friday, 29 April 2011

Sending Mail With Attachment in Win Forms Using C#



Sending Mail With Attachment in Win Forms Using C#



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Net;
using System.Net.Mail;

namespace mail_with_attachment
{
    public partial class Form1 : Form
    {
        ArrayList alAttachments;
        MailMessage mailMessage;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdlg = new OpenFileDialog();

            if (ofdlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    
                    textBox7.Text = ofdlg.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != null && textBox2.Text != null)
                {
                    mailMessage = new MailMessage(textBox1.Text, textBox4.Text);

                    mailMessage.Subject = textBox5.Text;
                    mailMessage.Body = textBox6.Text;
                    mailMessage.IsBodyHtml = true;

                    /* Set the SMTP server and send the email with attachment */

                    SmtpClient smtpClient = new SmtpClient();

                    // smtpClient.Host = emailServerInfo.MailServerIP;
                    //this will be the host in case of gamil and it varies from the service provider

                    smtpClient.Host = "smtp.gmail.com";
                    //smtpClient.Port = Convert.ToInt32(emailServerInfo.MailServerPortNumber);
                    //this will be the port in case of gamil for dotnet and it varies from the service provider

                    smtpClient.Port = 587;
                    smtpClient.UseDefaultCredentials = true;

                    //smtpClient.Credentials = new System.Net.NetworkCredential(emailServerInfo.MailServerUserName, emailServerInfo.MailServerPassword);
                    smtpClient.Credentials = new NetworkCredential(textBox1.Text, textBox2.Text);

                    //Attachment
                    Attachment attachment = new Attachment(textBox7.Text);
                    if (attachment != null)
                    {
                        mailMessage.Attachments.Add(attachment);
                    }

                   
                    //this will be the true in case of gamil and it varies from the service provider
                    smtpClient.EnableSsl = true;
                    smtpClient.Send(mailMessage);
                }

                string msg = "Message Send Successfully:";
                msg += "\n To :" + textBox4.Text;

                MessageBox.Show(msg.ToString());

                /* clear the controls */
                textBox1.Text = string.Empty;
                textBox2.Text = string.Empty;
                textBox3.Text = string.Empty;
                textBox4.Text = string.Empty;
                textBox5.Text = string.Empty;
                textBox6.Text = string.Empty;
                textBox7.Text = string.Empty;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Thursday, 28 April 2011

Change Form Shape in Win Forms



Change Form Shape in Win Forms



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        int intPosition = 0;
        String strScrollText = "This is scroll text";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 200;
            this.Refresh();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form f = new Form();
            f.Height = 350;
            f.Width = 350;
            f.BackColor = Color.Red;
           // f.TransparencyKey=;

            //Creating circle path
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddEllipse(10, 10, 300, 300);

            //Creating the region from the circle path
            f.Region = new Region(path);

            f.Show();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(intPosition <= strScrollText.Length)
            {          
                //intPosition = 0;
                this.Text = strScrollText.Substring(intPosition);
                intPosition = intPosition + 1;
            }
        }
    }
}

GDI Operation in Picture Box




GDI Operation in Picture Box 




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Graphics g;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog opndlg = new OpenFileDialog();

            opndlg.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

            if (opndlg.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(opndlg.FileName);
                this.Text = opndlg.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
            g = pictureBox1.CreateGraphics();
            Pen pen1 = new Pen(new SolidBrush(Color.Red),2);
            g.DrawLine(pen1, 250, 50, 400, 200);
        }
       
       

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            System.Drawing.Point[] p = new System.Drawing.Point[6];
            p[0].X = 0;
            p[0].Y = 0;
            p[1].X = 53;
            p[1].Y = 111;
            p[2].X = 114;
            p[2].Y = 86;
            p[3].X = 34;
            p[3].Y = 34;
            p[4].X = 165;
            p[4].Y = 7;
            g = pictureBox1.CreateGraphics();
            Pen pen1 = new Pen(new SolidBrush(Color.Red), 2);
            g.DrawPolygon(pen1, p);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            g = pictureBox1.CreateGraphics();
            Pen pen1 = new Pen(new SolidBrush(Color.Red), 2);
            g.DrawPie(pen1, 50, 50, 150, 150, 0, 170);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            g = pictureBox1.CreateGraphics();
            Pen pen1 = new Pen(new SolidBrush(Color.Red), 2);
            g.DrawArc(pen1, 150, 100, 150, 200, 150, 160);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            g = pictureBox1.CreateGraphics();
            Pen pen1 = new Pen(new SolidBrush(Color.Red), 2);
            g.DrawRectangle(pen1, 30, 30, 50, 60);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            g = pictureBox1.CreateGraphics();
            Pen pen1 = new Pen(new SolidBrush(Color.Red), 2);
            g.DrawEllipse(pen1, 50, 50, 100, 150);
        }
       
    }
}