After a little help from youtube I was able to figure out the code for your own personal email client using the Gmail smtp client. The code below is in C# using Visual Studio 2017. I created a form in windows form or wpf with the appropriate elements (labels, boxes and buttons). The code is executed when clicking on the Send button on the form. Hope this helps someone.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
namespace Email_App_WinForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void sendButton_Click(object sender, EventArgs e)
{
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("youremail@gmail.com", "*******");
//capture the email info.
MailMessage msg = new MailMessage(
msg.From = new MailAddress(fromTextBox.Text);
msg.To.Add(toTextBox.Text);
msg.Subject = subjectTextBox.Text;
msg.Body = msgTextBox.Text;
client.Send(msg);
MessageBox.Show("Message has been sent");
Close();
}
catch (Exception ex)
{
MessageBox.Show("Unable to send message because of: " + ex.Message);
}
}
No comments:
Post a Comment