Link to home
Start Free TrialLog in
Avatar of Sanjay Gandhi
Sanjay GandhiFlag for India

asked on

Send an email from ASP.NET Page

Hi,

I want to send email thru a website application I am developing. I need the code for this, and would like to know how to place SMTP host name, email-id, and password in the code from where the mail must go.

Thanks.
Avatar of binaryevo
binaryevo
Flag of United States of America image

internal class Mailer
    {
        private const int Timeout = 180000;

        private readonly string _host;
        private readonly int _port;
        private readonly string _user;
        private readonly string _pass;
        private readonly bool _ssl;

        public string Sender { get; set; }
        public string Recipient { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public string AttachmentFile { get; set; }

        public Mailer()
        {
            _host = ConfigurationManager.AppSettings["MailServer"];
            _port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            _user = ConfigurationManager.AppSettings["MailAuthUser"];
            _pass = ConfigurationManager.AppSettings["MailAuthPass"];
            _ssl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSSL"]);
        }

        public void Send()
        {
            // We do not catch the error here... let it pass direct to the caller
            Attachment att = null;
            var message = new MailMessage(Sender, Recipient, Subject, Body) {IsBodyHtml = true};
            var smtp = new SmtpClient(_host, _port);
            
            if (!String.IsNullOrEmpty(AttachmentFile))
            {
                if (File.Exists(AttachmentFile))
                {
                    att = new Attachment(AttachmentFile);
                    message.Attachments.Add(att);
                }
            }

            if (_user.Length > 0 && _pass.Length > 0)
            {
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(_user, _pass);
                smtp.EnableSsl = _ssl;
            }

            smtp.Timeout = Timeout;
            smtp.Send(message);

            if (att != null)
                att.Dispose();
            message.Dispose();
            smtp.Dispose();
        }
    }

Open in new window

Avatar of gery128
Apart from the solution suggested by binaryevo, I would like to add one more thing.

Encrypt your <appSettings> section of Web.Config for better security.
Agreed with gery128, security is of utmost importance.
Avatar of Sanjay Gandhi

ASKER

Your code must be fine, but its giving me some errors. Let me tell you I am quite new to ASP.NET, which is why also I need help. So please tell me why am I getting these errors, attached.
Errors.png
1)  Your button, BtCheckMail doesnt have an event associated with the button click.  Open up
design view of your webform and double click the button to resolve this.  It will take you to codebehind where the event will be created.

2)  Add the following to the top of the class in the using statements section:

using System.Net.Mail;

Open in new window

Sorry but I still find the errors appearing. I am attaching the code and the errors, plus I have also opened the event on Button, but if I try to use Send over there, it does not accept. Yes I've yet not added the SMTP link, which I can only try once my code is error free.

Thanks.
SendMail.zip
ASKER CERTIFIED SOLUTION
Avatar of gery128
gery128
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, sorry for delayed response. Got busy travelling and in an assignment.
My programmer could make really good use of it. Works, thanks.