5

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

请帮我解决这个问题。

以下是我的代码:

-(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];
}
4

2 回答 2

2

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

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

于 2012-07-11T07:38:17.460 回答
-1

您可以通过这种方式管理 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;
}
于 2013-11-13T13:36:49.860 回答