\

Facebook


วันอาทิตย์ที่ 29 กันยายน พ.ศ. 2556

[WinForm c#] Basic send E-mail with Google Sevice


In this tutorial, I gonna show you how to implement basic WinForm with sending E-mail function. Gmail provide you STM port for sending new e-mail to another mail inbox, so we need Gmail account to access though this example code.

First, you must regist GOOGLE MAIL account to use their mail server service. If you have own hosting mail server, just get the same configuration of server side to apply this tutorial.

Google normally use port 587 for send and recieve fresh e-mail, and you can also use Class SmtpClient, which from System.Net.Mail, to declare nesessery property before start sending method.

Create new project call "SendMail" and add new Form into solution, Add componets like sample images below. There are including textEdit controls, LabelEdit and button. Put into the same position and give them EventHandler . Into event simpleButton1_Click(), i should start tring send mail by get valus inside controls and put into MailMessage class and begin connection.

 
 
 
 Totally C# send mail method : 

MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress(textEdit1.Text);
                mail.To.Add(textEdit2.Text);
                mail.Subject = textEdit5.Text;
                mail.Body = memoEdit1.Text;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential(textEdit1.Text, textEdit3.Text);
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SendEmail
{
    public partial class mail : Form
    {
        public mail()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!CheckEmperty())
            {
                MessageBox.Show("Plesae fill all blank input form");
                return;
            }
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress(textEdit1.Text);
                mail.To.Add(textEdit2.Text);
                mail.Subject = textEdit5.Text;
                mail.Body = memoEdit1.Text;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential(textEdit1.Text, textEdit3.Text);
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

                labelControl4.Text = "success";
                labelControl4.Appearance.ForeColor = System.Drawing.Color.Green;
            }
            catch (Exception ex)
            {
                labelControl4.Text = "fail";
                labelControl4.Appearance.ForeColor = System.Drawing.Color.Red;
            }
        }
        private bool CheckEmperty()
        {
            if (textEdit2.Text.Equals(""))
            {
                return false;
            }
            else if(textEdit1.Text.Equals(""))
            {
                return false;
            }
            else if (textEdit3.Text.Equals(""))
            {
                return false;
            }
            else if (textEdit5.Text.Equals(""))
            {
                return false;
            }
            else if (memoEdit1.Text.Equals(""))
            {
                return false;
            }
            
            return true;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            textEdit1.Text = textEdit2.Text = textEdit3.Text = textEdit5.Text = memoEdit1.Text = "";
            labelControl4.Text = ": Result :";
            labelControl4.Appearance.ForeColor = System.Drawing.Color.Black;
        }
    }
}

ไม่มีความคิดเห็น:

May be like this posts