asp.net - 在 asp.net 中发送电子邮件 (Gmail)

标签 asp.net smtp gmail email

我尝试在 asp.net c# 上使用 gmail 帐户 发送电子邮件,但我不断收到此错误:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond [2607:f8b0:400c:c01::6c]:587

我尝试了来自不同资源的不同代码,但它们都不适用于我的项目 更重要的是,我已经尝试了这些步骤,但仍然不起作用:

  1. 在 VS 2012 服务器和本地主机 (iis 7) 上尝试端口 25、465、587。
  2. 防火墙设置为关闭,防病毒设置为关闭。
  3. 以管理员身份启动 Visual Studio。

请问谁能告诉我这个错误背后的原因是什么。如果有可能改变某些东西,我应该从哪里开始..

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

public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var myMailMessage = new System.Net.Mail.MailMessage();
        myMailMessage.From = new System.Net.Mail.MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1b2a4b3b7a4b3a4aca0a8ad81a6aca0a8adefa2aeac" rel="noreferrer noopener nofollow">[email protected]</a>");
        myMailMessage.To.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9de4f2e8eff8f0fcf4f1dde4fcf5f2f2b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a>");// Mail would be sent to this address
        myMailMessage.Subject = "Feedback Form";
        myMailMessage.Body = "Hello";

        var smtpServer = new System.Net.Mail.SmtpClient("smtp.gmail.com");
        smtpServer.Port =587;
        smtpServer.Credentials = new System.Net.NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63100611150611060e020a0f23040e020a0f4d000c0e" rel="noreferrer noopener nofollow">[email protected]</a>", "**YOURPASSWORD**");
        smtpServer.EnableSsl = true;
        smtpServer.Send(MyMailMessage);

        Response.Redirect("Default.aspx");
    }
}

最佳答案

试试这个,
添加命名空间

系统.net; 系统.net.邮件;

 protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage MyMailMessage = new MailMessage();

        MyMailMessage.From = new MailAddress("from");

        MyMailMessage.To.Add("to");

        MyMailMessage.Subject = "Feedback Form";

        MyMailMessage.Body = "This is the test message from xx for testing mail send";

        MyMailMessage.IsBodyHtml = true;

        SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");

        SMTPServer.Port = 587;

        SMTPServer.Credentials = new System.Net.NetworkCredential("username","password");

        SMTPServer.EnableSsl = true;

        try
        {

            SMTPServer.Send(MyMailMessage);

           //Response.Redirect("Thankyou.aspx");

        }

        catch (Exception ex)
        {



        }

关于asp.net - 在 asp.net 中发送电子邮件 (Gmail),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15558804/

相关文章:

java - 使用 gmail 上下文小工具访问附件

javascript - 无法使用 nodemailer (gmail) 启动邮件线程(回复邮件)

Javascript 确定用户在页面上的位置

c# - 在 ASP.net C# 中,如何将参数从 Javascript 函数传递给 onclick 处理程序

c# - asp.net缓存多线程锁webparts

javascript - 如何在meteor中设置多个smtp设置?

php - SMTP 服务器响应 : 530 5. 7.0 必须先发出 STARTTLS 命令

c# - Startup.cs 中的 Global.asax Application_Error 等效项

asp.net - ASP.NET 中的 HTML 转 PDF

java - 有没有支持esmtp管道的java api?