C#Application发送电子邮件。错误[发送邮件失败。]

时间:2012-07-03 03:34:46

标签: c# email smtp gmail hotmail

我正在使用Visual Studio 2012 RC中的.net framework 4.5在C#中构建应用程序。

我正在向我的帐户发送电子邮件,以便提供应用反馈和问题。 这是一个小应用程序。

几天前我的应用程序运行良好。我正在接收电子邮件。

现在我发现异常弹出“发送邮件失败。”

我尝试了许多调整但没有效果。我的帐户不要求重新激活。我也关闭了我的防火墙..没有相同的例外工作。我附上了一个例外细节。太。  请看一下。

请帮帮我。我做了太多谷歌。

我的代码是

MailMessage message = new MailMessage();
try
{
    message.To.Add(new MailAddress(mailTo));
    message.From = new MailAddress(mailFrom);

    //Attachment attachment = new Attachment(FileUpload1.PostedFile.FileName);
    message.Subject = "McFarlane Industries Product's Registration Request";
    message.Body = packageRequestContent();
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.SubjectEncoding = System.Text.Encoding.UTF8;
    //message.Attachments.Add(attachment);


    SmtpClient client = new SmtpClient();
    client.Port = 587; // Gmail works on this port
    client.Host = "smtp.gmail.com";
    System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mailFrom, mailFromPass);
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = nc;
    client.Send(message);
    MessageBox.Show("Thank you for Registring...!", "Register Notification!", MessageBoxButtons.OK, MessageBoxIcon.Information);


}
catch (Exception ex)
{
    MessageBox.Show("Request not submitted. " + ex.Message);
}

例外详情为:

System.Net.Mail.SmtpException was unhandled
  HResult=-2146233088
  Message=Failure sending mail.
  Source=System
  StackTrace:
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at McFarlaneIndustriesPOSnamespace.Registration.sendMailUsingHotmail(String mailFrom, String mailFromPass, String mailTo) in e:\Works\McFarlane Industries\McFarlane Industries Point of Sale Source Code\McFarlaneIndustries\Registration.cs:line 73
       at McFarlaneIndustriesPOSnamespace.Registration.buttonSendRegistrationRequest_Click(Object sender, EventArgs e) in e:\Works\McFarlane Industries\McFarlane Industries Point of Sale Source Code\McFarlaneIndustries\Registration.cs:line 47
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at McFarlaneIndustriesPOSnamespace.Program.Main() in e:\Works\McFarlane Industries\McFarlane Industries Point of Sale Source Code\McFarlaneIndustries\Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.IOException
       HResult=-2146232800
       Message=Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
       Source=System
       StackTrace:
            at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
            at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
            at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
            at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
            at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
            at System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn)
            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=An existing connection was forcibly closed by the remote host
            Source=System
            ErrorCode=10054
            NativeErrorCode=10054
            StackTrace:
                 at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            InnerException:

1 个答案:

答案 0 :(得分:1)

我会尝试使用网络界面登录Hotmail / Gmail帐户。有时,Hotmail会随机锁定您的帐户并重置密码;特别是如果您使用该帐户发送批量电子邮件。