views:

6771

answers:

3

I create new ASP.NET web application that use SMTP to send message. The problem is the smtp was not authenticated from who send the message.

My Question is: How can I make SMTP authenticated in my program? does C# have a class that have attribute for enter username and password?

+7  A: 
using System.Net;
using System.Net.Mail;


SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential = 
    new NetworkCredential("username", "password"); 
MailMessage message = new MailMessage(); 
MailAddress fromAddress = new MailAddress("[email protected]"); 

smtpClient.Host = "mail.mydomain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;

message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("[email protected]"); 

try
{
    smtpClient.Send(message);
}
catch(Exception ex)
{
    //Error, could not send the message
    Response.Write(ex.Message);
}

U can use the above code.

Arief Iman Santoso
+1  A: 

How do you send the message?

The classes in the System.NeSystem.t.Mail namespace (which is probably what you should use) has full support for authentication, either specified in Web.config, or using the SmtpClient.Credentials property.

Tor Haugen
+1  A: 

Set the Credentials property before sending the message.

OJ