\

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);
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
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;
        }
    }
}
 
<div class="preloader"><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>

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

May be like this posts

  • Unix Timestamp เวลามาตราฐานที่ใช้ในการเปรียบเทียบทั่วโลก
    16/04/2014 - 1 Comments
    Unix Timestamp คืออะไร? Unix Time คือเวลาที่บวก 1 เพิ่มทุกๆวินาที…
  • [โปรแกรม searchหาชื่อสินค้า] SQLite บน C# เอาไว้อ่านไฟล์ .db ใน WinForm มันง่ายมั่กๆ
    04/09/2013 - 1 Comments
    วันก่อนผมเขียนเกี่ยวกับการสร้าง app ดึงข้อมูลจากฐานข้อมูล SQL Server 2008 r2…
  • ยุคสมัยแห่ง Automate เมื่อ Selenium เข้ามาเป็น QA แทนมนุษย์ (Part 1)
    22/06/2017 - 0 Comments
    ยอมรับสักทีเถอะว่ายุคนี้ ปี 2017 มันเริ่มเข้าสู่ยุคหุ่นยนต์แล้ว ไหนจะรถไร้คนขับ ไหนจะ AlphaGo…
  • [WinForm C#] Basic Keyboard Reader
    01/10/2013 - 0 Comments
    Almost of any software which have builded for user's comfortable have special shortcut keys, so today i would…
  • [VS2013] TIPเล็กๆ วิธี Break exceptiion ขณะ Runtime
    26/09/2013 - 0 Comments
    ในโหมด Debug ของ Visual Studio 2012 หลายๆคนคงจะคุ้นเคยกับ Message box ที่จะคอยเด้งขึ้นมาเตือนเมื่อเกิด…
  • วิธีการเป็น Administrator บน PC ของตัวเอง
    25/08/2013 - 0 Comments
    ผมว่าออกจะน่าขำไปสักนิด กับการที่เครื่องคอมพิวเตอร์ของตัวเองแท้ๆแต่กลับ log in เข้าใช้ได้เพียง User…
  • Win10 วิธี Install .Net Framework 3.5 (คิดว่าง่ายหรอ หึหึ)
    23/02/2017 - 0 Comments
    Windows 10 ของแท้ ลิกขสิทธิ์ก็ใช่ว่าจะ perfect ไปทุกอย่าง หลังจาก Install สำเร็จจะพบว่า feature ของ .net…