2013-06-01 471 views
0

我遇到問題。我編寫了一個程序,讓你從.txt列表中發送電子郵件到該地址。它有效(我在德國),但對其他人(在其他國家)它不起作用。對於他們來說,只有通過Gmail發送電子郵件纔有效任何幫助?無法通過Hotmail發送電子郵件,只能通過Gmail發送郵件

下面是代碼:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void cmdDistribute_Click(object sender, EventArgs e) 
    { 
     FileStream fs; 
     StreamReader sr; 

     if (Receiverlist.Text == "") 
     { 
      MessageBox.Show("Please type in the path of the list with the receiver! (.txt)", "ERROR : CANT FIND RECEIVERLIST!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      return; 
     } 

     fs = new FileStream(Receiverlist.Text, FileMode.Open); 
     sr = new StreamReader(fs); 

     string zeile; 

     if (senderID.Text == "") 
     { 
      MessageBox.Show("Please type in your login information!", "ERROR : NO EMAIL ADDRESS!"); 
      fs.Close(); 
      return; 
     } 

     else if (SenderPassword.Text == "") 
     { 
      MessageBox.Show("Please type in your login information!", "ERROR : NO PASSWORD!"); 
      fs.Close(); 
      return; 
     } 

     MessageBox.Show("While sending the emails this programm won´t response till it has send \nall emails! This could take a while, so please be patient...", "ALERT", MessageBoxButtons.OK, MessageBoxIcon.Warning); 

     string mailProvider = ""; 
     int port = 0; 

     try 
     { 
      if (ProviderGmail.Checked) 
      { 
       mailProvider = "smtp.gmail.com"; 
       port = 587; 
      } 
      else if (providerHotmail.Checked) 
      { 
       mailProvider = "smtp.live.com"; 
       port = 587; 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Can´t connect to the GMail/Hotmail server. \n Please contact TheFlash on skype!", "ERROR : SMTPSERVER", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      fs.Close(); 
      return; 
     } 

     if (EmailSubject.Text == "") 
     { 
      MessageBox.Show("Please type in the subject of the email!.", "ERROR : EMPTY SUBJECT", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      fs.Close(); 
      return; 
     } 

     int n = 1; 

     while (sr.Peek() != -1) 
     { 
      zeile = sr.ReadLine(); 

      try 
      { 

       System.Net.Mail.MailAddress DistributorMail = new System.Net.Mail.MailAddress(senderID.Text); 
       System.Net.Mail.MailAddress Receiver = new System.Net.Mail.MailAddress(zeile); 
       System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(DistributorMail, Receiver); 
       email.Subject = EmailSubject.Text; 
       email.Body = EmailBody.Text; 

       System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(mailProvider,port); 
       client.EnableSsl = true; 
       client.UseDefaultCredentials = false; 
       client.Credentials = new System.Net.NetworkCredential(senderID.Text, SenderPassword.Text); 

       if (checkBox1.Checked) 
       { 
        if (EmailAttachment.Text == "") 
        { 
         MessageBox.Show("Please type in the path of your attachment!.", "ERROR : EMPTY ATTACHMENT PATH", MessageBoxButtons.OK, MessageBoxIcon.Error); 
         fs.Close(); 
         return; 
        } 
        else 
        { 
         MessageBox.Show("If you add an attachment to your mail, it will take longer to send it!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
         email.Attachments.Add(new System.Net.Mail.Attachment(EmailAttachment.Text)); 
        } 
       } 
       else 
       { 
        EmailAttachment.Enabled = false; 
       } 

       try 
       { 
        client.Send(email); 
        label9.Text = "Emails sent : " + n; 
        n = n + 1; 

        if (sr.Peek() == -1) 
         MessageBox.Show("Finished!", "TheFlash´s Email Distributor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       } 
       catch (Exception Ex) 
       { 
        label9.Text = "Couldn´t send the emails!"; 
        fs.Close(); 
        return; 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("This is not an valid email address.", "ERROR : WRONG EMAIL FORMAT", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       fs.Close(); 
       return; 
      } 

     } 
     fs.Close(); 
     return; 

    } 

    private void cmdExit_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 

    private void cmdAbout_Click(object sender, EventArgs e) 
    { 
     new AboutBox1().Show(); 
    } 
} 
+0

你使用TLS或只是基本的SSL連接? –

+0

請閱讀[本回答](http://meta.stackexchange.com/a/130208/213671)瞭解爲什麼我編輯了您的標題。 – gunr2171

+0

@MikePrecup連接到基本的SSL,它的代碼人... – iAmFastAndYou

回答

0

OK,對於某種原因它看起來像你的用戶可能已經鎖定了他們的Hotmail帳戶的。所以他們可以嘗試通過正常的網頁登錄,看看他們是否被要求解開一個難題來解鎖它。有關此的詳細信息,請參閱:

Why am I repeatedly locked out of my Hotmail account, and what can I do about it?

我不知道是否有可能圍繞此代碼。已經有,你可以在這裏看看過去的問題:

SMTP Client Sending error in C# app

Send email via Hotmail to gmail

另見:

Sending Email From C# Via Hotmail/Outlook/Windows Live

facing issues while sending mail using smtp.live.com in c#

+0

謝謝,這可能是問題。我有點困惑,因爲他們都有這個問題:/而且我試了兩天以上沒有睡覺來修復那個hahahaha omg im太愚蠢了^^再次感謝你,先生,祝你有美好的一天! – iAmFastAndYou

+0

不客氣。不要感到愚蠢:)最好是不要吞下這樣的例外,因爲當涉及診斷/修復問題時,它們將成爲你的朋友。你也有美好的一天。 – davmos

+0

肯定不要覺得蠢。我也有這個問題,這就是爲什麼我在答案的另一部分中寫了博客文章的原因。很容易錯過。 –