2012-11-02 80 views
-2

你好,我正在爲objective-c爲iOS做一個程序,我想只檢查s​​mtp服務器(@之後)是否存在通過ping它或做一個HTTP請求,並得到狀態(200 )。如何檢查smtp服務器(如gmail.com/hotmail.fr)是否存在?

我在做什麼現在它得到UITextField.text,切割它,得到的子@後,做這...但它是不工作...

 NSError *error = nil; 
     NSURL *url = [NSURL URLWithString:@"gmail.com"]; 
     NSURLRequest *request = [NSURLRequest requestWithURL:url];; 
     NSURLResponse *response = nil; 
     NSData *data=[[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]]; 
     NSString* retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     // you can use retVal , ignore if you don't need. 
     NSInteger httpStatus = [((NSHTTPURLResponse *)response) statusCode]; 
     NSLog(@"responsecode: %d", httpStatus); 
     // there will be various HTTP response code (status) 
     // you might concern with 404 
     if(httpStatus == 200) 
     { 
      NSLog(@"FEELIX CONTENT"); 
      // do your job 
     } 
     else 
     { 
      NSLog(@"FOCK!"); 
     } 
+2

您想要將端口80上的HTTP與端口20上運行的SMTP服務器交談?我建議你去學習TCP/IP。 – mattjgalloway

+1

你爲什麼要檢查一個SMTP服務器是否存在?你想解決什麼問題? – CodeCaster

回答

0

如果你有一個域,該域的郵件服務器由MX DNS記錄指定。例如:

$ host -t MX gmail.com 
gmail.com mail is handled by 5 gmail-smtp-in.l.google.com. 
gmail.com mail is handled by 30 alt3.gmail-smtp-in.l.google.com. 
gmail.com mail is handled by 10 alt1.gmail-smtp-in.l.google.com. 
gmail.com mail is handled by 40 alt4.gmail-smtp-in.l.google.com. 
gmail.com mail is handled by 20 alt2.gmail-smtp-in.l.google.com. 

該編號是優先顯示。要檢查服務器,請檢查您是否可以連接到該服務器的端口25。

+0

你打算向他建議*他怎麼做? – trojanfoe

0

我不確定你要在這裏做什麼。但是也許你錯過了一些東西:

@符號背後的部分並不一定是SMTP服務器。 通過向DNS查詢返回SMTP服務器的MX記錄(郵件交換)來確定SMTP服務器。

上,你可以在命令行窗口做:

nslookup -type=mx gmail.com 

獲得SMTP的服務器。

有關更多詳細信息,請參見http://en.wikipedia.org/wiki/MX_record

相關問題