【问题标题】:how to send mail by using smtp in asp.net如何在asp.net中使用smtp发送邮件
【发布时间】:2012-03-28 07:44:42
【问题描述】:
i need solution for this error 

我运行那个时候发生了一些错误:发送电子邮件失败。 SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是: 5.7.0 必须首先发出 STARTTLS 命令。 i1sm8651517pbj.70

using System;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Net.Mail;

    public partial class _Default : System.Web.UI.Page 
    {
        #region  "Send email"
        protected void btnSendmail_Click(object sender, EventArgs e)
        {
            // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
            // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();

            try
            {
                MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

                // You can specify the host name or ipaddress of your server
                // Default in IIS will be localhost 
                smtpClient.Host = "smtp.gmail.com";

                //Default port will be 25
                smtpClient.Port = 587;

                //From address will be given as a MailAddress Object
                message.From = fromAddress;

                // To address collection of MailAddress
                message.To.Add("muthu17green@gmail.com");
                message.Subject = "Feedback";

                // CC and BCC optional
                // MailAddressCollection class is used to send the email to various users
                // You can specify Address as new MailAddress("admin1@yoursite.com")
                message.CC.Add("muthu17green@gmail.com");
                message.CC.Add("muthu17green@gmail.com");

                // You can specify Address directly as string
                message.Bcc.Add(new MailAddress("muthu17green@gmail.com"));
                message.Bcc.Add(new MailAddress("muthu17green@gmail.com"));

                //Body can be Html or text format
                //Specify true if it  is html message
                message.IsBodyHtml = false;

                // Message body content
                message.Body = txtMessage.Text;

                // Send SMTP mail
                smtpClient.Send(message);

                lblStatus.Text = "Email successfully sent.";
            }
            catch (Exception ex)
            {
                lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
            }
        }
        #endregion

        #region "Reset"
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtMessage.Text = "";
            txtEmail.Text = "";
        }
        #endregion
    }

【问题讨论】:

    标签: c# asp.net email smtp


    【解决方案1】:
    public void SendMail()
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.To.Add(MailTo.Text);
        mail.From = new MailAddress(MailFrom.Text,"Invoice");
        mail.Subject = Subject.Text;
        mail.Body = Body.Text;
        mail.IsBodyHtml = true;
    
    
    
        string FileName = Path.GetFileName(FileUploadAttachments.PostedFile.FileName);
        Attachment attachment = new Attachment(FileUploadAttachments.PostedFile.InputStream ,FileName);
        mail.Attachments.Add(attachment);            
    
        SmtpClient client = new SmtpClient();
        client.Credentials = new System.Net.NetworkCredential("Your_Email@Email.com", "Your_Email_Password");
        client.Host = "smtpout.secureserver.net";
        client.Port = 80;
        try
        {
            client.Send(mail);
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }
    

    【讨论】:

    • 使用 C# Asp.Net 从 GODADDY 发送带有附件的电子邮件
    • 您可以在答案中为您的代码添加 cmets 和解释。
    • 您的回答不适用于 Gmail,也不能解决问题中的错误。
    【解决方案2】:

    我认为您忘记将 EnableSSL 属性设置为 true,这是 gmail 所必需的。

    这里是示例代码:

    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {              
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(txtFrom.Text);                
            msg.To.Add(new MailAddress(txtTo.Text));
            msg.Subject = txtSubject.Text;
            msg.Body = txtBody.Text;                           
    
            SmtpClient mySmtpClient = new SmtpClient();
            System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential(txtFrom.Text,txtPwd.Text);
            mySmtpClient.Host = "smtp.gmail.com";
            mySmtpClient.Port=587;
            mySmtpClient.EnableSsl = true;
            mySmtpClient.UseDefaultCredentials = false;
            mySmtpClient.Credentials = myCredential;                
    
            mySmtpClient.Send(msg);
            msg.Dispose();
            lberr.Text="Message sent successfully";
            clrtxt();
        }
        catch(SmtpException)
        {
            lberr.Text="SMTP Error handled";
        }
    }
    

    【讨论】:

      【解决方案3】:

      您似乎正在尝试使用GMail 发送电子邮件,这需要SSL
      看到这个Google reference post

      所以在您的 web.config 中,以这种方式启用 SSL:

      <system.net>
          <mailSettings>
            <smtp deliveryMethod="network">
              <network host="smtp.gmail.com" port="587" enableSsl="true" userName="YOURUSERNAME" password="YOURPASSWORD" />
            </smtp>
          </mailSettings>
        </system.net>
      

      或者,您可以设置它programmatically this way

      smtpClient.EnableSsl = true;
      

      【讨论】:

        【解决方案4】:

        你可以查看我的帖子,了解如何使用 gmail autentication 发送电子邮件

        send email with gmail smtp server

        【讨论】:

          【解决方案5】:

          你需要设置SmtpClient.Credentials属性:

          smtpClient.Credentials = new NetworkCredentials("yourUserName", "yourPassword");
          

          这是用于验证以发送消息的内容。您可能还需要确保启用 SSL:

          smtpClient.EnableSsl = true;
          

          SmtpClient.Credentials Property MSDN Reference

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-08-30
            • 1970-01-01
            • 2016-01-18
            • 2019-04-22
            • 1970-01-01
            • 2012-08-17
            • 1970-01-01
            相关资源
            最近更新 更多