【问题标题】:how to send email using asp.net and html page如何使用 asp.net 和 html 页面发送电子邮件
【发布时间】:2017-09-20 10:57:16
【问题描述】:

我有一个contact.html 页面,其中有用于用户名、电子邮件、电话、主题、消息和提交按钮的textboxex 页面。

<form class="contact-page-form-1">
      <div class="row clearfix">
          <div class="col-md-6 col-sm-6 col-xs-12 form-group">
              <input type="text" name="username" placeholder="Your Name*" id="username" required>
          </div>

          <div class="col-md-6 col-sm-6 col-xs-12 form-group">
                <input type="email" name="email" placeholder="Email Address*" id="email" required>
          </div>

          <div class="col-md-6 col-sm-6 col-xs-12 form-group">
                <input type="text" name="phone" placeholder="Phone Num" id="phone" required>
          </div>

          <div class="col-md-6 col-sm-6 col-xs-12 form-group">
                <input type="text" name="subject" placeholder="Subject" id="subject" required>
          </div>

          <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 form-group">
                <textarea name="message" placeholder="Your Message..." id="message"></textarea>
          </div>

          <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 form-group">
                  <button class="theme-btn btn-style-one" onclick="javascript:SendMail()" type="submit" name="submit-form">Send Message</button>
          </div>

      </div>
</form>

当我们点击按钮时,javascript 函数被调用,我调用了 VisionMail.aspx 中的 webmethod。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script type="text/javascript">
    function SendMail() {
        var uname = document.getElementById("username").value;
        var email = document.getElementById("email").value;
        var phone = document.getElementById("phone").value;
        var subject = document.getElementById("subject").value;
        var message = document.getElementById("message").value;
        var URL = "VisionMail.aspx?uname=" + uname + "&email=" + email + "&ph=" + phone + "&sub=" + subject + "&msg=" + message + "&count=India";
                    window.open(URL);
    }
</script>  

我用于发送邮件的 WebMethod 位于 VisionMail.aspx 中,如下所示:-

protected void Page_Load(object sender, EventArgs e)
    {
        string uname = Request.QueryString["uname"].ToString();
        string email = Request.QueryString["email"].ToString();
        string phone = Request.QueryString["ph"].ToString();
        string subject = Request.QueryString["sub"].ToString();
        string message = Request.QueryString["msg"].ToString();
        string country = Request.QueryString["count"].ToString();

        string close = @"<script type='text/javascript'>
                            window.returnValue = true;
                            window.close();
                            </script>";


        using (MailMessage mail = new MailMessage())
        {
            mail.From = new MailAddress("abc@gmail.com");
            mail.To.Add("info@xyz.com");
            mail.Subject = subject;
            mail.Body = "Mr. " + uname + "<br/>" + "Email ID : " + email + " Phone No. " + phone + "<br/> <h4>He/She is Sending Message From Contact Page (" + country + "). <br/>" + message;
            mail.IsBodyHtml = true;

            using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
            {
                try
                {
                    smtp.Credentials = new NetworkCredential("abc@gmail.com", "abc123");
                    smtp.EnableSsl = true;
                    smtp.Send(mail);

                    base.Response.Write(close);
                }
                catch (Exception ex)
                {
                    base.Response.Write(close);
                }
            }
        }
    }

此代码在本地系统上运行良好,并且邮件正在发送,但是当我在服务器上发布它时它不起作用,当我单击按钮发送邮件时,邮件不会发送并且 webmethod 存在的页面被下载。有什么解决办法吗?

【问题讨论】:

  • 我不确定,但是当您启用 SSL 时,端口不应该是 465 吗?我认为如果不是,那么问题可能与网络相关......比如代理或任何东西......希望它有所帮助

标签: javascript jquery html asp.net


【解决方案1】:

试试下面的代码。

使用 (SmtpClient smtp = new SmtpClient("smtp.gmail.com")) {

试试 {

  smtp.Credentials = new NetworkCredential("visioncomn@gmail.com", "visioncomptel123");

  smtp.EnableSsl = smtp.Port == 587 || smtp.Port == 465;

  smtp.Send(mail);

  base.Response.Write(close);

} 捕捉(例外前) { base.Response.Write(关闭); } }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2013-09-08
    • 1970-01-01
    • 2011-09-22
    • 2011-11-21
    相关资源
    最近更新 更多