2012-06-06 176 views
5

在我的應用程序中,我可以使用smtp服務器發送電子郵件,因爲我輸入了正確的電子郵件ID和密碼。 但是當我輸入我的Gmail或雅虎帳戶的詳細信息,我無法發送郵件。 因爲我設置了我的relayHost = @「smtp.gmail.com」;那麼我也無法發送郵件。iphone使用smtp服務器發送電子郵件?

請幫我解決這個問題。

下面是我的代碼:

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

的問題是,Gmail和雅虎使用自定義認證爲他們的SMTP服務。 – CarlJ

回答

-1

可以管理中繼主機的雅虎,Gmail,Hotmail服務等。在此way-

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; 
} 
相關問題