Shravan
Shravan

Reputation: 493

Auto email notifications in c# dotnet using sql server 2005

I have a requirement in an ERP application,(Application is developed in asp.net4.0 using c# and sql server 2005)

When ever we receive order from client we register the order with a unique Serialnumber(Serial Number is primary key generates auomatically) Now we want to send a notification basically an email which states your order has been registered with Serial Number : XXXXXX.

Can i know what will be the best possible solution for this ????

Do i have to send email through database or through application .

Please Help me !!!

Upvotes: 0

Views: 3841

Answers (5)

huhu78
huhu78

Reputation: 389

If you want to send email notification with minimum effort use Database Mail (configure it!) and sp_send_mail. You will be able to send simple text message with possible query result as an attachment.

If you need rich HTML with company logo image build .NET Windows Service (or ConsoleApp and schedule it). I recommend you to use Actionmailer.NET Standalone to build HTML messages using MVC logic, templates and Razor engine. Using Actionmailer.NET Standalone you can have it without using IIS. See: http://geeksharp.com/2011/07/06/actionmailer-0-6-released/

Upvotes: 0

Remus Rusanu
Remus Rusanu

Reputation: 294227

You should use sp_send_db_mail and absolutely not attempt to instantiate one form or another of 'SMTP' class in the C# application. The reasons are multiple, but primarily:

  • you need to ensure that the mail is delivered transactionally: the call to sp_send_db_mail must be placed in the same DB transaction that registered the serial number. If the registration rolls back, the mail is never sent.
  • you need to ensure that the mail is delivered reliably: if there is a failure in attempting to contact your outgoing SMTP endpoint then there must be retry logic. Database Mail, which is behind the sp_send_db_mail call, ensures this, and also offers tracking and logging for troubleshooting purposes (which emails could not be delivered and why).
  • you need to call the SMTP asynchronously, after your HTTP request is complete. You do not want your page load to become unresponsive as the page processing is idling in response from the SMTP outgoing relay. sp_send_db_mail ensures this as well, the actual mail delivery occurs after your transaction has completed and does not involve the C# client in any way.

Upvotes: 1

Josh Anderson
Josh Anderson

Reputation: 6005

ScottGu has a great article on this. It's a little old, but here is what he says:

.NET 2.0 includes much richer Email API support within the System.Net.Mail code namespace. I've seen a few questions from folks wondering about how to get started with it. Here is a simple snippet of how to send an email message from “[email protected]” to multiple email recipients (note that the To a CC properties are collections and so can handle multiple address targets):

MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");

message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));

message.CC.Add(new MailAddress("[email protected]"));
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();
client.Send(message);

System.Net.Mail reads SMTP configuration data out of the standard .NET configuration system (so for ASP.NET applications you’d configure this in your application’s web.config file). Here is an example of how to configure it:

  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

Upvotes: 1

Paul Sullivan
Paul Sullivan

Reputation: 2875

You can definitely do both (as long as SQL is 2005+)

See this discussino:

SQL calling CLR routine

and sending mail from within your application is as simple as:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("[email protected]");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("[email protected]");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Send(message);

Upvotes: 0

IAmTimCorey
IAmTimCorey

Reputation: 16757

You can definitely send email using C#. Here is really comprehensive article on how to do it:

http://www.emailarchitect.net/easendmail/kb/csharp.aspx

Basically what I do is create a wrapper function that makes it very simple to send an email using a setup custom designed for my environment. For example, you might create a method that accepts the new serial number and looks up all of the information based upon that. Then you could just pass in the new value and have the auto-formatted email sent out to the customer.

Upvotes: 0

Related Questions