Max
Max

Reputation: 13328

Sending e-mail in asp with usings

I have an email form on my website which is working great. The only thing I want to do is add usings for better coding, what is the best way to do this? The code I use is the following:

            try
            {
            //create message
            MailMessage msg = new MailMessage();
            msg.To.Add(txtTo.Text);
            msg.From = new MailAddress(txtFrom.Text);
            msg.Subject = string.Format("Van: " + txtName.Text + " | Email: " + txtFrom.Text + " | Onderwerp: " + txtSubject.Text);
            msg.Body = txtBericht.Text;

            SmtpClient smtp = new SmtpClient("");
            smtp.Host = "smtp.live.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;

            smtp.Credentials = new NetworkCredential("emailaddress", "pass");

                smtp.Send(msg);
                ClearTextboxes();
                lblError.ForeColor = Color.Green;
                lblError.Text = "Het verzenden van uw e-mail is gelukt!";
            }
            catch
            {
                lblError.ForeColor = Color.Red;
                lblError.Text = "Er ging iets mis met het versturen van de email."; 
            }

How can I use usings on this code? And is it safe to use fill in my emailaddress and password when I'm uploading my website, or should I use another method?

Thanks in advance!

EDIT:

I changed my code to the following:

  try
            {
            //create message
                using (MailMessage msg = new MailMessage())
                {

                     //create message
            MailMessage msg = new MailMessage();
            msg.To.Add(txtTo.Text);
            msg.From = new MailAddress(txtFrom.Text);
            msg.Subject = string.Format("Van: " + txtName.Text + " | Email: " + txtFrom.Text + " | Onderwerp: " + txtSubject.Text);
            msg.Body = txtBericht.Text;

                    using (SmtpClient smtp = new SmtpClient())
                    {
                        smtp.Host = "smtp.live.com";
                        smtp.Port = 587;
                        smtp.EnableSsl = true;

                        smtp.Credentials = new NetworkCredential("emailadress", "pass");

                        smtp.Send(msg);
                    }
                    ClearTextboxes();
                    lblError.ForeColor = Color.Green;
                    lblError.Text = "Het verzenden van uw e-mail is gelukt!";
                }          
            }
            catch
            {
                lblError.ForeColor = Color.Red;
                lblError.Text = "Er ging iets mis met het versturen van de email."; 
            }

Upvotes: 1

Views: 162

Answers (5)

Roy Ashbrook
Roy Ashbrook

Reputation: 854

    using(SmtpClient smtp = new SmtpClient()){
        //smtp options here
        using(MailMessage msg = new MailMessage()){
            //mail options here
            smtp.Send(msg);
        }
    }

Upvotes: 0

Kokulan Eswaranathan
Kokulan Eswaranathan

Reputation: 165

You could use using for both MailMessage and SMTP Client objects

           //create message
           using (SmtpClient smtp = new SmtpClient(""))
           using( MailMessage msg = new MailMessage())
           {

                // Your code

           }

Upvotes: 0

Ryan Gunn
Ryan Gunn

Reputation: 1169

I would suggest using the web.config for setting up the host, username and password. Example.

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="[email protected]">
            <network host="smtp.mail.com" 
                     userName="[email protected]" 
                     password="blog.dotnetclr.com" port="25"/>
        </smtp>
    </mailSettings>
</system.net>

Code Example:

Public Sub SendEmail([to] As String, subject As String, body As String)
    Dim mailMessage = New System.Net.Mail.MailMessage()
    mailMessage.[To].Add([to])
    mailMessage.Subject = subject
    mailMessage.Body = body

    Dim smtpClient = New SmtpClient()
    smtpClient.EnableSsl = True
    smtpClient.Send(mailMessage)
End Sub

Upvotes: 1

Nikita Silverstruk
Nikita Silverstruk

Reputation: 1117

http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.100).aspx

using (MailMessage msg = new MailMessage())
{
    //Your Code
}

Upvotes: 0

Richard Brown
Richard Brown

Reputation: 11436

Never put authentication information in your code. Put it in your web.config instead and reference it where you need it. That also saves you the trouble of hunting down everywhere you used it if you change your password.

Upvotes: 4

Related Questions