【问题标题】:Sending email via asp.net webpage通过 asp.net 网页发送电子邮件
【发布时间】:2013-06-24 16:20:44
【问题描述】:

我特地做了这个教程作为基础:http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp

我成功调试了错误,但它似乎不起作用...请帮助...

预览:

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

公共部分类 Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

} protected void SendMail() { // Gmail Address from where you send the mail var fromAddress = "ml27.santos@gmail.com"; // any address where the email will be sending var toAddress = YourEmail.Text.ToString(); //Password of your gmail address const string fromPassword = "Password"; // Passing the values and make a email formate to display string subject = YourSubject.Text.ToString(); string body = "From: " + YourName.Text + "\n"; body += "Email: " + YourEmail.Text + "\n"; body += "Subject: " + YourSubject.Text + "\n"; body += "Question: \n" + Comments.Text + "\n"; // smtp settings var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); smtp.Timeout = 20000; } // Passing values to smtp object smtp.Send(fromAddress, toAddress, subject, body); } protected void Button1_Click(object sender, EventArgs e) { try { //here on button click what will done SendMail(); DisplayMessage.Text = "Message sent!"; DisplayMessage.Visible = true; YourSubject.Text = ""; YourEmail.Text = ""; YourName.Text = ""; Comments.Text = ""; } catch (Exception) { } }}

【问题讨论】:

  • 您需要提供更多关于 WHAT 不起作用以及遇到的错误的详细信息
  • 发生了哪些错误以及发生在哪里..
  • 没有邮件发送...

标签: asp.net web


【解决方案1】:

【讨论】:

  • 我用587成功了,但是是谷歌... var client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("acct@gmail.com", "pw"), EnableSsl = true };
  • 登录您的帐户并检查已发送文件夹。如果smtp.Send方法没有抛出异常,说明连接正确。
【解决方案2】:

您是否尝试过评论那篇文章/联系作者?

我猜这里有两件事:

A.你没有在 catch 块中做任何事情。您应该考虑在 catch 块中关注

catch(Exception ex){
DisplayMessage.Text = ex.Message;
DisplayMessage.Visible = true;
}

B.检查您使用的 gmail 帐户是否启用了 pop3/imap 的 smtp。

【讨论】:

    【解决方案3】:

    试试这个

        protected void SendMail()
        {
            // any address where the email will be sending
            var toAddress = YourEmail.Text.ToString();
            //Password of your gmail address
            const string fromPassword = "Password";
            // Passing the values and make a email formate to display
            string subject = YourSubject.Text.ToString();
            string body = "From: " + YourName.Text + "\n";
            body += "Email: " + YourEmail.Text + "\n";
            body += "Subject: " + YourSubject.Text + "\n";
            body += "Question: \n" + Comments.Text + "\n";
    
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("ml27.santos@gmail.com");
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = new System.Net.NetworkCredential("ml27.santos@gmail.com", fromPassword);
            message.From = fromAddress;
            message.To.Add(toAddress);
            message.Subject = subject;
            message.Priority = MailPriority.High;
            message.Body = body;
            message.IsBodyHtml = true;
            smtpClient.Send(message);
        }
    

    【讨论】:

      猜你喜欢
      • 2013-01-16
      • 2012-11-17
      • 1970-01-01
      • 2015-12-07
      • 2011-06-01
      • 2011-07-02
      • 2021-04-05
      • 1970-01-01
      • 2011-01-04
      相关资源
      最近更新 更多