C# Program Send Mail Google

Postingan kali  ini saya coba akan buat postingan yang berbau koding - koding gitu :) Bahasa pemrograman yang akan saya bahas disini yaitu C#. C# memang ada yang berbasis console dan ada yang berbasis Windows Form, yang berbasis console kodingannya gak jauh - jauh beda lah sama C++, maka dari itu saya pilih memposting C# yang berbasis Form aja, dan postingan pertama C# Windows Form saya akan membahas tentang Program Send Mail Google. Dalam kodingnya kita akan menggunakan SMTP dari google, yaitu "smtp.gmail.com". Nanti setelah Program dijalankan, akan ada pengecekan terhadap email yang kita gunakan untuk mengirim email, apakah email yang kita gunakan itu valid atau tidak.





  • Silahkan Buat desain Formnya terlebih dahulu
    Spoiler:


    Disini menggunakn 6 textbox, 2 Button, 6 Label, dan 3 GroupBox, 1 Tools OpenFileDialog.
    Ubah Name TextBox pertama menjadi textBoxfrom
    TextBox kedua menjadi textBoxpass
    TextBox ketiga menjadi textBoxto
    TextBox keempat menjadi textBoxsub
    TextBox kelima menjadi textBoxBody
    TextBox keenam menjadi textBox1

    Ubah name label Change User menjadi CU dan Set visible label'a menjadi False





  • Klik Kanan pada form --> View Code, kemudian tambahkan sebuah library mail


  • using System.Net.Mail; 
    



  • Klik 2 kali label Change User, kemudian tambahkan code berikut


  • textBoxfrom.Enabled = true;
    textBoxpass.Enabled = true;
    textBoxfrom.Text = "";
    textBoxpass.Text = "";
    CU.Visible = false;
    




  • Klik 2 kali pada button Browse, kemudian tambahkan code berikut


  • DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
           string file = openFileDialog1.FileName;
           textBox1.Text = file;
    }
    
    




  • Klik 2 kali pada button Send Mail, kemudian isi code berikut


  • try
                {
                    if (textBoxto.Text == "" || textBoxsub.Text == "" || textBoxBody.Text == "")
                    {
                        MessageBox.Show("Periksa Kolom Dengan Benar", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
    
                    else if (textBox1.Text == "")
                    {
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                        mail.From = new MailAddress(textBoxfrom.Text);
                        mail.To.Add(textBoxto.Text);
                        mail.Subject = textBoxsub.Text;
                        mail.Body = textBoxBody.Text;
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential(textBoxfrom.Text, textBoxpass.Text);
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                        MessageBox.Show("Mail Send", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        textBoxfrom.Enabled = false;
                        textBoxpass.Enabled = false;
                        textBoxto.Text = "";
                        textBoxsub.Text = "";
                        textBoxBody.Text = "";
                        CU.Visible = true;
                    }
    
                    else
                    {
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                        mail.From = new MailAddress(textBoxfrom.Text);
                        mail.To.Add(textBoxto.Text);
                        mail.Subject = textBoxsub.Text;
                        mail.Body = textBoxBody.Text;
                        Attachment attachment;
                        attachment = new Attachment(textBox1.Text);
                        mail.Attachments.Add(attachment);
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential(textBoxfrom.Text, textBoxpass.Text);
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                        MessageBox.Show("Mail Send", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        textBoxfrom.Enabled = false;
                        textBoxpass.Enabled = false;
                        textBoxto.Text = "";
                        textBoxsub.Text = "";
                        textBoxBody.Text = "";
                        textBox1.Text = "";
                        CU.Visible = true;
                    }
                }
    
                catch (Exception)
                {
                    MessageBox.Show("Silahkan Periksa Email atau Password Anda", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
    

    Dan ini kodingan keseluruhannya jika digabung
    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;
    
    namespace smtp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DialogResult result = openFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    string file = openFileDialog1.FileName;
                    textBox1.Text = file;
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    if (textBoxto.Text == "" || textBoxsub.Text == "" || textBoxBody.Text == "")
                    {
                        MessageBox.Show("Periksa Kolom Dengan Benar", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
    
                    else if (textBox1.Text == "")
                    {
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                        mail.From = new MailAddress(textBoxfrom.Text);
                        mail.To.Add(textBoxto.Text);
                        mail.Subject = textBoxsub.Text;
                        mail.Body = textBoxBody.Text;
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential(textBoxfrom.Text, textBoxpass.Text);
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                        MessageBox.Show("Mail Send", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        textBoxfrom.Enabled = false;
                        textBoxpass.Enabled = false;
                        textBoxto.Text = "";
                        textBoxsub.Text = "";
                        textBoxBody.Text = "";
                        CU.Visible = true;
                    }
    
                    else
                    {
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                        mail.From = new MailAddress(textBoxfrom.Text);
                        mail.To.Add(textBoxto.Text);
                        mail.Subject = textBoxsub.Text;
                        mail.Body = textBoxBody.Text;
                        Attachment attachment;
                        attachment = new Attachment(textBox1.Text);
                        mail.Attachments.Add(attachment);
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential(textBoxfrom.Text, textBoxpass.Text);
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                        MessageBox.Show("Mail Send", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        textBoxfrom.Enabled = false;
                        textBoxpass.Enabled = false;
                        textBoxto.Text = "";
                        textBoxsub.Text = "";
                        textBoxBody.Text = "";
                        textBox1.Text = "";
                        CU.Visible = true;
                    }
                }
    
                catch (Exception)
                {
                    MessageBox.Show("Silahkan Periksa Email atau Password Anda", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
    
            private void CU_Click(object sender, EventArgs e)
            {
                textBoxfrom.Enabled = true;
                textBoxpass.Enabled = true;
                textBoxfrom.Text = "";
                textBoxpass.Text = "";
                CU.Visible = false;
            }
        }
    } 
    

    Ini merupakan screenshot ketika program dijalankan
    Spoiler:







    Semoga Bermnafaat :)


    Blog, Updated at: 11:05 AM

    0 komentar:

    Post a Comment

    Berkomentarlah dengan bijak dan sopan. Tidak diperbolehkan menggunakan kata-kata yang mengandung SARA. Komentar akan melalui moderation terlebih dahulu, jika terdapat kata-kata yang tidak sesuai ketentuan, maka komenar tidak dapat ditampilkan. Mari Saling berkomentar dengan baik :)