How to send a mail using C#

  • Thread starter Thread starter jamilabkh
  • Start date Start date
J

jamilabkh

Hi all,

I need to know how to authenticate a user before sending a mail using a
C# application.

The source code i use is this:

private void sendMailButton_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new
MailMessage(toTextBox.Text, fromTextBox.Text, subjectTextBox.Text,
bodyTextBox.Text);

SmtpClient obj = new SmtpClient(SMTPTextBox.Text);
obj.Send(mailMessage);
MessageBox.Show("Message Sent");

}
catch (Exception ex)
{
MessageBox.Show("message not sent");

}
}

I need to add the authentication part. This is working but with no
authentication for the user or the mail sender address. I am using a
local SMTP server.
Thanks,
 
This is based upon VS2005 and .NET 2.0
You need to set the Credentials property of your SmtpClient to an instance
of a NetworkCredentails object.
SmtpClient obj = new SmtpClient(SMTPTextBox.Text);
! obj.UseDefaultCredentials = False;
! obj.Credentials = new NetworkCredentials( "UserId",
"Password");
! // From here on the SmtpClient should do all your heavy
lifting
obj.Send(mailMessage);
MessageBox.Show("Message Sent");

Chris
 
Back
Top