使用asp在asp中发送电子邮件

时间:2013-03-11 20:48:50

标签: c# asp.net visual-studio email

我的网站上有一个email form,效果很好。我唯一想做的就是添加usings以获得更好的编码,最好的方法是什么?我使用的代码如下:

            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."; 
            }

如何在此代码中使用usings? 当我上传我的网站时,使用填写我的电子邮件地址和密码是否安全,或者我应该使用其他方法?

提前致谢!

编辑:

我将代码更改为以下内容:

  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."; 
            }

5 个答案:

答案 0 :(得分:4)

永远不要在您的代码中放置身份验证信息。将它放在web.config中,并在需要的地方引用它。如果您更改密码,这也可以为您节省在使用它时无处不在的麻烦。

答案 1 :(得分:1)

我建议使用web.config来设置主机,用户名和密码。实施例。

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="name@domain.com">
            <network host="smtp.mail.com" 
                     userName="name@domain.com" 
                     password="blog.dotnetclr.com" port="25"/>
        </smtp>
    </mailSettings>
</system.net>

代码示例:

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

您可以对MailMessage和SMTP客户端对象使用

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

                // Your code

           }

答案 4 :(得分:0)

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