ทำการ อนุญาตแอปที่มีความปลอดภัยน้อย ใน gmail ก่อน

สร้างโปรเจ็กส์แบบ WindowsForm
เพิ่ม button1
using System;
using System.Net.Mail;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string displayName = "Phaisarn Sutheebanjard";
string userName = "xxxxx@gmail.com";
string password = "yyyyy";
string mailto = "mr.phaisarn@gmail.com";
using (SmtpClient smtp = new SmtpClient())
{
// email
MailMessage mail = new MailMessage();
mail.From = new MailAddress(userName, displayName);
mail.To.Add(mailto);
mail.Subject = "Greeting";
mail.Body = string.Format("Hello World!");
// smtp
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(userName, password);
smtp.Send(mail);
MessageBox.Show("Mail Sent");
}
}
}
}
Link