rakesh
rakesh

Reputation:

Email solution for web application

I have developed a web application in ASP.NET. I want to integrate email solution in my application.

Here are my core requirements: 1. Sending email as soon as user registers in website. 2. Sending notification emails to all registered users.

For the second requirement, we can consider it as bulk email as we are sending mails to all users.

Please give me best solution. Do we need to set up SMTP server?

Upvotes: 0

Views: 731

Answers (4)

Sunil Acharya
Sunil Acharya

Reputation: 1183

I've found it easiest way in asp.net and its working at many place. I'm System.Net.Mail library to send email.

    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("[email protected]");
    msg.To.Add("[email protected]);

    msg.IsBodyHtml = true;
    msg.Subject = "Example Email";
    msg.Priority = MailPriority.High;
    msg.Body = "Hello world...";

    smtp.Host = "smtp.domainName";
    smtp.EnableSsl = true;
    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
    NetworkCred.UserName = "[email protected]";
    NetworkCred.Password = "password";

    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;

    try
        {
            ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
            smtp.Send(msg);
        }

        catch (Exception ex)
        {}

I hope this code work for you...

Best of luck and happy coding :)

Upvotes: 0

Jagd
Jagd

Reputation: 7307

I've found it easiest to wrapper the System.Net.Mail library in a helper class, and then that has allowed me to simply include my EmailHelper class in any project that needs the ability to send out emails.

Here's my regurgitated Send() method in my EmailHelper. You can see that it is quite easy to use.

public bool Send() {
    bool emailSent = false;

    if (_to.Count > 0) {
        MailMessage msg = new MailMessage();
        SmtpClient mail = new SmtpClient("your.email.host");

        msg.From = new MailAddress(_fromAddress, _fromName);

        foreach (String to in _to) {
            msg.To.Add(new MailAddress(to));
        }

        foreach (String cc in _cc) {
            msg.CC.Add(new MailAddress(cc));
        }

        msg.Subject = _subject;
        msg.Body = _body;
        msg.IsBodyHtml = true;
        mail.Send(msg);
        emailSent = true;
    }
    return emailSent;
}

Note that the _fromAddress, _fromName, etc. are just private attributes of the EmailHelper class. The private attributes _to and _cc are both just lists of type string.

Upvotes: 0

David
David

Reputation: 25450

Yes, you need to have an SMTP server, though it doesn't have to be setup on the same machine as the webserver (it can even be provided by a third party service).

Personally I would suggest that you create an email queue system - rather then the ASP.NET script directly using the System.Net namespace to create and send an email using a specific SMTP server, you have a database table (i.e. Subject, Body, TO address).

A seperate job then reads from that table and sends the email. The reasons for the seperate table/job are:

  • It avoids having the SMTP operation occur inside of an HTTP transaction - if the SMTP server is overloaded or worse, unavailable, your webpage will appear to hang.

  • If the SMTP server is temporary unavailable or overloaded, you have the ability to add a retry column to your table.

  • If you SMTP server limits of the number of emails that can be sent in a time period (spam prevention) you can configure your job to only attempt to send the top x emails on each interation.

  • If you want to send delayed emails, it's simply a matter of adding a "SendAfter" column

Upvotes: 4

Dillie-O
Dillie-O

Reputation: 29725

Typically no. Make sure that the web server has the SMTP service installed on the system and you can use system.net.mail class. This should handle all of your basic e-mail sending needs.

If you start to run into issues with a large volume of registered users or issues with having too many BCC addresses in your bulk mail, then you will want to look into a basic mail manager (something that could send your bulk messages in bursts) or a full fledged e-mail server.

Upvotes: 1

Related Questions