Open Visual Studio.
New Project -> Windows Form Application.
Design form as shown below :-
using System.Net;
using System.Net.Mail;
namespace Email_Sending2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
if (d.ShowDialog() == DialogResult.OK)
{
textUpload.Text = d.FileName.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
MailMessage mm = new MailMessage();
mm.Subject = textSub.Text;
mm.To.Add(textTo.Text);
mm.From = new MailAddress(textOwnMail.Text);
mm.Body = textMsg.Text;
Attachment a = new Attachment(textUpload.Text);
mm.Attachments.Add(a);
SmtpClient sc = new SmtpClient("smtp.gmail.com");
sc.EnableSsl = true;
sc.Credentials = new NetworkCredential(textOwnMail.Text, textOwnPw.Text);
sc.Port = 587;
sc.Send(mm);
MessageBox.Show("Your email sent successfully.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
New Project -> Windows Form Application.
Design form as shown below :-
In the code file :-
using System.Net;
using System.Net.Mail;
namespace Email_Sending2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
if (d.ShowDialog() == DialogResult.OK)
{
textUpload.Text = d.FileName.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
MailMessage mm = new MailMessage();
mm.Subject = textSub.Text;
mm.To.Add(textTo.Text);
mm.From = new MailAddress(textOwnMail.Text);
mm.Body = textMsg.Text;
Attachment a = new Attachment(textUpload.Text);
mm.Attachments.Add(a);
SmtpClient sc = new SmtpClient("smtp.gmail.com");
sc.EnableSsl = true;
sc.Credentials = new NetworkCredential(textOwnMail.Text, textOwnPw.Text);
sc.Port = 587;
sc.Send(mm);
MessageBox.Show("Your email sent successfully.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
No comments:
Post a Comment