Monday, February 12, 2007

System.Net.Mail.SmtpClient How To Send User Name and Password?

In my line of stuff I do, I often need to send automated email messages from both my web and desktop (or service) applications.

On .NET 2.0 framework there is now a new namespace called System.Net.Mail, so we should all take advantage of it. So I started to work on it. One problem; most of SMTP servers we deal with require some type of credentials.

If you look at the MSDN docs for the SMTP class, the MSDN's description and example of how to do this is really poor in the document. I would say that 99% of the time, we just want to send a user name and password along with the SMTP traffic, and do not need to rely on default user credentials in the CredentialsCache. It took me a bit of efforts to find how to do this, but it is actually simple. Make sure to include the following namespaces first.

The following example also shows how I am using the Properties object to pull the Application configuration file information. Since SMTP config is something that needs change from time to time, it is best to store the information in a configuration file. The only sticky issue is of course, the password saved in plain text, but in my case it is a closed application so this isn't an issue for me, and even if that leaks, it is not an immediate threat.

using System.Net.Mail;
using System.Net;
using System.Security.Principal;


private void SendEmail(string subject, string body)
{
string smtpServer = Properties.Settings.Default.SMTPServerBlankIfDontWantEmail;
if ((smtpServer == null) (smtpServer == "")) return;
try
{
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress (Properties.Settings.Default.EmailTo));
msg.From =(new MailAddress(Properties.Settings.Default.EmailFrom));
msg.Body = body;
SmtpClient c = new SmtpClient(Properties.Settings.Default.SMTPServerBlankIfDontWantEmail,
Properties.Settings.Default.SMTPPort);
string token = "mmonitor";

c.Credentials = new NetworkCredential
(Properties.Settings.Default.SMTPUserName,
Properties.Settings.Default.SMTPPassword);


// c.SendAsync(msg, token); // Don't hang the rest of the stuff is SMTP hangs.
c.Send(msg);
this.toolStripStatusLabel2.Text = "Sent email";
}
catch (System.Exception ex)
{
this.toolStripStatusLabel2.Text = ex.Message;
}
}


Saturday, February 10, 2007

SQLExpress Logs Too Many "Starting up Database" Message

I wrote an application that uses SQLExpress to save some data transfer information. It puts an entry every several minutes. When I checked the Application Log, I got tons of message that said "Starting up Database " which is Event ID: 17137.

I researched this and the reason why the SQL Express does this is to conserve resource by making database "Auto Close" by default. In my case, I constantly use this database and also a remote application status display pulls the data to report the progress, it would not make much sense for the database to close so quickly.

The way to turn the message off is to disable "Auto Close"mode, and where you do that is in the Database's property (use SQL Server Management Express). You will find the Options node and the Auto Close is pretty much at the top of the list of options you can turn "false".

Thursday, February 08, 2007

Running SQL 2000-5 on A Domain Controller - No!

We all want to save money. We would rather not have to have many computers in our offices. Buying a server is one thing but cost of the ownership to keep and maintain the box is yet anotehr.

At any rate, this question comes up fairly often because of that: "Can you run a SQL Server 2005 on a Domain Controller". The Microsoft's answer is basically NO but do it at your own risk (link).

The issues stem from basically how the SQL Service starts up in what security role.

Some key points of the reasons why they are against are;
  • You cannot promote (dcpromo) or demote the domain controller's role after SQL runs. This basically means that you need to set up the DC and its role first before slapping on the SQL.
  • SQL service must run on a domain acount and not on a local machine's system account or Network account.