我的机器不会在VIsual studio 2012上使用Gmail帐户发送电子邮件

时间:2013-03-22 03:21:14

标签: asp.net smtp gmail

已经过了几天,试图解决这个问题。 我正在使用Visual Studio 2012.我想在asp.net C#中使用(gmail帐户)发送电子邮件。 它本身的代码没有问题(我尝试过不同的代码)。 但我认为这与我的机器有关。

此外,以下是目前为止所做的步骤: 1-关闭两者(防火墙,防病毒) 2-以管理员身份运行Visual Studio。 3-使用Localhost(iis 7) 4-尝试不同的端口(586,25,465) 我仍然收到错误消息。

这是异常消息(错误):

System.Net.Mail.SmtpException was unhandled by user code
  HResult=-2146233088
  Message=Failure sending mail.
  Source=System
  StackTrace:
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at _Default.Page_Load(Object sender, EventArgs e) in d:\ThesisIIS\WebSite12\Default.aspx.cs:line 40
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: System.Net.WebException
       HResult=-2146233079
       Message=Unable to connect to the remote server
       Source=System
       StackTrace:
            at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
            at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
            at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
            at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
            at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
            at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
            at System.Net.Mail.SmtpClient.GetConnection()
            at System.Net.Mail.SmtpClient.Send(MailMessage message)
       InnerException: System.Net.Sockets.SocketException
            HResult=-2147467259
            Message=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:c03::6c]:587
            Source=System
            ErrorCode=10060
            NativeErrorCode=10060
            StackTrace:
                 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
                 at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
            InnerException: 

代码:

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("serveremail@gmail.com");
        myMailMessage.To.Add("youremail@yahoo.com");// 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("serveremail@gmail.com", "**YOURPASSWORD**");
        smtpServer.EnableSsl = true;
        smtpServer.Send(MyMailMessage);

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

如果有人能给我一个想法,我将非常感激。

0 个答案:

没有答案