xcode iphone使用smtp服务器发送电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10914020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:31:05  来源:igfitidea点击:

iphone to send email using smtp server?

iphoneobjective-cxcodeios4

提问by Nilesh .S. Joshi

in my application i am able to send email using smtp server,for that i have enter my correct email id and password. but when i enter my gmail or yahoo account details i am not able to send the mail. as i have set my relayHost = @"smtp.gmail.com"; then also i am not able to send the mail.

在我的应用程序中,我可以使用 smtp 服务器发送电子邮件,为此我输入了正确的电子邮件 ID 和密码。但是当我输入我的 gmail 或 yahoo 帐户详细信息时,我无法发送邮件。因为我已经设置了我的 relayHost = @"smtp.gmail.com"; 然后我也无法发送邮件。

please help me out this.

请帮我解决这个问题。

following is my code :

以下是我的代码:

-(void)sendEMAIL{

   SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];

    testMsg.fromEmail = str_uname;
    NSLog(@"str_Uname=%@",testMsg.fromEmail);

    testMsg.toEmail = str_info;

    NSLog(@"autoemail=%@",testMsg.toEmail);

    testMsg.relayHost = @"smtp.gmail.com";

    testMsg.requiresAuth = YES;

    testMsg.login = str_uname;
    NSLog(@"autoelogin=%@",testMsg.login);

    testMsg.pass = str_password;
    NSLog(@"autopass=%@",testMsg.pass);

   testMsg.subject = @"Schedule Sms And Email";

   testMsg.wantsSecure = YES; 

   NSString *sendmsg=[[NSString alloc]initWithFormat:@"%@",str_info2];
   NSLog(@"automsg=%@",sendmsg);

   testMsg.delegate = self;

   NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
                           sendmsg, kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

    testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
    [testMsg send];
}

-(void)messageSent:(SKPSMTPMessage *)message{ 
   [message release];     
}

-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{
   [message release];
}

采纳答案by sinh99

you can also source code from hear https://github.com/kailoa/iphone-smtpIts a awesome code using smtp server.

您还可以从https://github.com/kailoa/iphone-smtp 中获取源代码这是使用 smtp 服务器的很棒的代码。

http://iphonesdksnippets.com/send-email-with-attachments-on-iphone.htmluse this link is also good....Just download framework from there and use that code.Its nice too.

http://iphonesdksnippets.com/send-email-with-attachments-on-iphone.html使用此链接也不错....只需从那里下载框架并使用该代码。它也很好。

回答by Nico

you can manage relay host for yahoo, gmail, hotmail etc. in this way-

您可以通过这种方式管理 yahoo、gmail、hotmail 等的中继主机-

 NSArray *arr1 = [fromEmail componentsSeparatedByString:@"@"];
NSArray *arr2 = [[arr1 objectAtIndex:1] componentsSeparatedByString:@"."];

if ([arr2 containsObject:@"gmail"]) {
    smtp_message.relayHost = @"smtp.gmail.com";
}
else if ([arr2 containsObject:@"yahoo"]) {
    smtp_message.relayHost = @"smtp.mail.yahoo.com";
}
else if ([arr2 containsObject:@"hotmail"] || [arr2 containsObject:@"live"]) {
    smtp_message.relayHost = @"smtp.live.com";
}
else
{
    NSString *smtpRelay = [[NSString alloc]init];
    smtpRelay = [NSString stringWithFormat:@"smtp.%@.com",[arr2 objectAtIndex:0]];
      smtp_message.relayHost = smtpRelay;
}