2012-07-03 217 views
1

我正在Visual Studio 2012 RC中使用.NET Framework 4.5在C#中構建應用程序。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: 
+0

我使用非常類似的代碼給你,它的工作原理。您應該登錄到Gmail中的帳戶並嘗試手動發送郵件。 – Michael

+0

-2146233088是十六進制的80131500。如果您在錯誤代碼進行搜索,還有周圍像防火牆等... http://answers.microsoft.com/en-us/windows/forum/windows_vista-windows_programs/error-code-80131500/d24818da溝通問題噸命中-7ba6-4e48-bfe9-6db321e3d5d6 – bryanmac

+0

請顯示例外文本。 – Shymep

回答

1

我會嘗試登錄到使用Web界面的Hotmail/Gmail帳戶。有時Hotmail會隨機鎖定您的帳戶並重置密碼;特別是如果您使用該帳戶發送批量電子郵件。

+0

stacktrace說hotmail,但代碼說smtp.gmail.com – Blorgbeard

+0

@Blorgbeard Hm。不過,我想不出還有什麼會導致這種情況,除了一個鎖定的帳戶。 – McGarnagle

+0

@Blorgbeard。是的,我糾正了錯誤。其實我也使用hotmail。但他們都是同樣的錯誤。無論如何,我已糾正錯誤。我從郵件地址進入hte hotmail。我的錯。 –