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.
Spoiler:
using System.Net.Mail;
textBoxfrom.Enabled = true; textBoxpass.Enabled = true; textBoxfrom.Text = ""; textBoxpass.Text = ""; CU.Visible = false;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
textBox1.Text = file;
}
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:







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 :)