How to send a mail using C#

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,
 
C

ChrisRM

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top