My Code:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public SmtpClient client = new SmtpClient();
public MailMessage msg = new MailMessage();
public System.Net.NetworkCredential smtpCreds =
new System.Net.NetworkCredential("myusername@gmail.com", "mypassword");
public Form1()
{
InitializeComponent();
}
public void SendEmail(string sendTo, string sendFrom, string subject, string body)
{
try
{
//Setup SMTP Host Here
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = smtpCreds;
client.EnableSsl = true;
//Convert strings to MailAddress
MailAddress to = new MailAddress(sendTo);
MailAddress from = new MailAddress(sendFrom);
//Setup message settings
msg.Subject = subject;
msg.Body = body;
msg.From = from;
msg.To.Add(to);
//Send email!
client.Send(msg);
}
catch (Exception ex)
{
//This is just in case you type something wrong or if it cannot connect to the server
//It will pop up with the reason why it will not work
MessageBox.Show(ex.Message, "ERROR");
}
}
private void button1_Click(object sender, EventArgs e)
{
SendEmail("myusername@gmail.com", "myusername@gmail.com", "Hello ", "IT WORKS!");
}
}
}
Could anyone help?
Thanks!

New Topic/Question
Reply



MultiQuote







|