【问题标题】:How can i check if the smtp server ( as gmail.com / hotmail.fr ) exists?如何检查 smtp 服务器(如 gmail.com / hotmail.fr)是否存在?
【发布时间】:2023-03-03 11:56:01
【问题描述】:

您好,我正在为 iOS 编写 Objective-c 中的程序,我只想通过 ping 或对其执行 HTTP 请求来检查 smtp 服务器(在 @ 之后)是否存在并返回状态( 200)。

我现在正在做的是获取 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!");
        }

【问题讨论】:

  • 您想通过端口 80 与运行在端口 20 上的 SMTP 服务器通信 HTTP?我建议你去了解一下 TCP/IP。
  • 为什么要检查 SMTP 服务器是否存在?你想解决什么问题?

标签: objective-c http nsurlrequest http-status-code-200


【解决方案1】:

如果您有域,则该域的邮件服务器由 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。

【讨论】:

  • 你会建议他如何他这样做吗?
【解决方案2】:

我不确定你想在这里做什么。但也许你错过了什么:

@ 符号后面的部分不一定是 SMTP 服务器。 SMTP 服务器是通过向 DNS 查询返回 SMTP 服务器的 MX 记录(邮件交换)来确定的。

在 windows 上,您可以在命令行上执行:

nslookup -type=mx gmail.com

获取 SMTP 服务器。

更多详情请见http://en.wikipedia.org/wiki/MX_record

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多