如何使用smtp发送电子邮件而无需在gmail

时间:2018-12-21 08:28:06

标签: asp.net smtp smtplib

如何在不打开gmail帐户中的安全性较低的应用程序选项的情况下从asp.net应用程序发送电子邮件?例如,现在我正在使用下面的代码发送电子邮件。

   try{
        MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text);            
        mm.Subject = txtSubject.Text;
        mm.Body = txtBody.Text;

        if (fuAttachment.HasFile)
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
            mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }
        mm.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
    }
    catch (Exception ex)
    {
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent Failed.');", true);
    } 

0 个答案:

没有答案