用asp发送电子邮件

本文关键字:电子邮件 asp | 更新日期: 2023-09-27 18:03:19

我在我的网站上有一个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."; 
            }

用asp发送电子邮件

永远不要在代码中添加身份验证信息。把它放到你的网里。相反,配置并在需要时引用它。如果您更改了密码,这也为您省去了查找使用过密码的所有地方的麻烦。

我建议使用web。配置设置主机、用户名和密码。例子。

<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

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

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

您可以同时使用使用MailMessage和SMTP Client对象

           //create message
           using (SmtpClient smtp = new SmtpClient(""))
           using( MailMessage msg = new MailMessage())
           {
                // Your code
           }
    using(SmtpClient smtp = new SmtpClient()){
        //smtp options here
        using(MailMessage msg = new MailMessage()){
            //mail options here
            smtp.Send(msg);
        }
    }