C# Program Send Mail Google

Postingan kali  ini ane coba akan buat postingan yang berbau coding - coding gitu :) Setelah sekian lamanya kagak pernah memposting sesuatu tentang pemrograman :D
Bahasa pemrograman yang ane gunakan kali ini juga gak jauh beda sama dengan bahasa pemrograman di postingan tentang pemrograman terdahulu, ya masih dalam keluarga bahasa C, yaitu C#. C# memang ada yang berbasis console dan ada yang berbasis Windows Form, yang berbasis console codingannya gak jauh - jauh beda lah sama C++, maka dari itu ane pilih memposting C# yang berbasis Form aja, dan psostingan pertama C# Windows Form ane akan membahas tentang Program Send Mail Google. Dalam codingnya 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.
  1. 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
  2. Klik Kanan pada form --> View Code, kemudian tambahkan sebuah library mail
    using System.Net.Mail;
  3. Klik 2 kali label Change User, kemudian tambahkan code berikut
    textBoxfrom.Enabled = true;
    textBoxpass.Enabled = true;
    textBoxfrom.Text = "";
    textBoxpass.Text = "";
    CU.Visible = false;
  4. 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;
    }
  5. Klik 2 kali pada button Send Mail, kemudian isi code berikut
    Spoiler:
    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 codingan keseluruhannya jika digabung
Spoiler:
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;
        }
    }
}
Dan Ini SS'a gan
Spoiler:




Semoga Bermnafaat :)

Read this | Baca yang ini



Widget by [ Free Widget ]

0 comments:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Cheap Web Hosting