Csharp ile email Gönderme

Bu yazımda sizlere C# ile email gönderme işleminin sadece kodlarını yazacağım arkadaşlar herhangi bir sorunuz olursa hem mail adresimden hem de buradan sorularınızı sorabilirsiniz.. İşte kodlar ve programın ekran görüntüsü:



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.Net.Mail;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;
namespace gymsoftware
{
    public partial class email : Form
    {
        public email()
        {
            InitializeComponent();
        }

     
        MySqlConnection baglan = new MySqlConnection("server=;database=;user=;password=;");
     
     
     

        private void email_Load(object sender, EventArgs e)
        {
            baglan.Open();

         
            MySqlCommand kmm = new MySqlCommand("select x  from y", baglan);
            MySqlDataAdapter da = new MySqlDataAdapter();
            da.SelectCommand = kmm;
            DataTable dt = new DataTable();
            da.Fill(dt);
            comboTo.DataSource = dt;
            comboTo.ValueMember = "x";
         
            comboTo.DisplayMember = "y";
       

            baglan.Close();
        }
     
        private void buttonsend_Click(object sender, EventArgs e)
        {
            try
            {
                string smtp = "";
                if (combosmtp.Text == "Gmail")
                    smtp = "smtp.gmail.com";
                else if (combosmtp.Text == "Hotmail")
                    smtp = "smtp.live.com";

                SmtpClient client = new SmtpClient(smtp, 587);
                client.Credentials = new System.Net.NetworkCredential(textBoxFrom.Text, textPass.Text);
                MailMessage mymail = new MailMessage();
                mymail.To.Add(new MailAddress(comboTo.SelectedValue.ToString()));
                mymail.From = new MailAddress(textBoxFrom.Text);
                mymail.Subject = textsub.Text;
                mymail.Body = textmail.Text;
                client.EnableSsl = true;
                client.Send(mymail);
            }
            catch (Exception) { MessageBox.Show("Connection establishment failed, check your mail address or password,please.!"); }

        }
        public bool IsValidEmail(string email)
        {
         
            string pattern = @"^[-a-zA-Z0-9][-A-Za-z0-9_@.'][-.a-zA-Z0-9][-A-Za-z0-9_@.']*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
    (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$";
         
            Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
         
            bool valid = false;

       
            if (string.IsNullOrEmpty(email))
            {
                valid = false;
            }
            else
            {
                valid = check.IsMatch(email);
            }
         
            return valid;
        }


        private void buttonconnect_Click(object sender, EventArgs e)
        {
            if(combosmtp.Text==""){MessageBox.Show("Select your mail server");}
            else if (textPass.Text == "") { MessageBox.Show("Type your password"); }
            else
            {
                bool a = IsValidEmail(textBoxFrom.Text);
                if (a == false) MessageBox.Show("Invalid E-Mail");
                else
                {
                    groupmail.Enabled = true;
                    ovalShape2.Enabled = true;
                    textfrom.Text = textBoxFrom.Text;

                }
            }
        }

        private void buttonexit_Click(object sender, EventArgs e)
        {
            DialogResult cevap;
            cevap = MessageBox.Show("Are you sure you are exit?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (cevap == DialogResult.Yes) { Application.Exit(); }
         
        }
    }
}

Leave a Reply