ส่งอีเมลจาก gmail ด้วย C#

ทำการ อนุญาตแอปที่มีความปลอดภัยน้อย ใน 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

Posted in C#

C# Base64

นำสตริง 111 มาทำเป็น Base64 แล้ว ถอดกลับออกมา

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string plainText1 = "111";
            byte[] byteData1 = System.Text.ASCIIEncoding.ASCII.GetBytes(plainText1);
            string encodeText = System.Convert.ToBase64String(byteData1);
            Console.WriteLine(encodeText);

            byte[] byteData2 = System.Convert.FromBase64String(encodeText);
            string plainText2 = System.Text.ASCIIEncoding.ASCII.GetString(byteData2);
            Console.WriteLine(plainText2);
        }
    }
}

// MTEx
// 111

Dataset

โปรเจ็กส์แบบ Web ใช้คำสั่ง Field แล้วได้ Error

DataRow row0 = dt.Rows[0];
DateTime createDate = row0.Field<DateTime>("createdate");
'System.Data.DataRow' does not contain a definition for 'Field' and no extension method 'Field' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?)

ทางแก้ ให้ติดตั้ง System.Data.DataSetExtensions

PM> Install-Package System.Data.DataSetExtensions
Posted in C#

กรองเอาเฉพาะตัวเลข (digit) จาก string

ใช้ regular expression

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string ipString = "(66)123-456-7890 เบอร์มือถือ ";
                string opString = null;
                Console.WriteLine(ipString);

                Regex regex = new Regex(@"[^\d]");
                opString = regex.Replace(ipString, "");
                Console.WriteLine(opString);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

จะได้

(66)123-456-7890 เบอร์มือถือ
661234567890

ใช้ C# อ่านไฟล์ CSV

using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "test.csv";
            string text = System.IO.File.ReadAllText(filename, Encoding.GetEncoding(874)).Trim();
            string[] rows = text.Split('\n');
            Console.WriteLine(string.Format("There are {0} rows in '{1}'.", rows.Count(), filename));

            Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
            for (int r = 0; r < rows.Count(); r++)
            {
                //string[] cols = rows[r].Split(',');
                string[] cols = CSVParser.Split(rows[r]);

                for (int c = 0; c < cols.Count(); c++)
                {
                    // clean up the fields (remove " and leading spaces)
                    cols[c] = cols[c].TrimStart(' ', '"');
                    cols[c] = cols[c].TrimEnd('"');
                    Console.Write(cols[c]);
                }
                Console.WriteLine();
            }
        }
    }
}

Link

Posted in C#