2010-08-19 36 views
20

Tôi mới phát triển ứng dụng ios, Dưới đây là mã tôi đã sử dụng để gửi email.Không thể gửi email bằng MFMailComposeViewController trong trình mô phỏng

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; 
    controller.mailComposeDelegate = self; 
    [controller setSubject:@"My Subject"]; 
    [controller setMessageBody:@"Hello there." isHTML:NO]; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 



    - (void)mailComposeController:(MFMailComposeViewController*)controller 
        didFinishWithResult:(MFMailComposeResult)result 
           error:(NSError*)error { 

      if (result == MFMailComposeResultSent) { 

       NSLog(@"It's away!"); 
      } 

      [self dismissModalViewControllerAnimated:YES]; 
    } 

Rất tiếc, các phương thức ủy quyền sẽ không bao giờ được kích hoạt, Có thể bất kỳ ai đề xuất cách tôi có thể kiểm tra email của mình qua trình mô phỏng không?

+0

tôi đã phải đối mặt với problem..but cùng khi tôi đã cố gắng trên điện thoại của tôi nó làm việc tốt .. nhờ lukya cho lời giải thích. –

+0

Đối với nhiệm vụ của tôi, tôi chỉ cần hiển thị nhà soạn nhạc. Nhưng tôi không thể làm như vậy. MFMailComposeViewController * composeVC = [[MFMailComposeViewController alloc] init]; . Dòng này bật lên bộ điều khiển cảnh báo –

Trả lời

51

Bạn KHÔNG THỂ gửi thư qua Trình mô phỏng.

Thay vào đó, bạn có thể cài đặt ứng dụng trong thiết bị và thử từ đó.

Trình mô phỏng chỉ hiển thị nhà soạn nhạc nhưng không cho phép bạn gửi thư. Gửi thành công chỉ là sự thừa nhận rằng mã của bạn là tốt và không có vấn đề mà chấm dứt nó trong khi gửi.

+1

Đây là câu trả lời chính xác, và không bị lừa bởi hoạt hình thư đã gửi, email * không * thực sự được gửi. –

+0

Đối với nhiệm vụ của tôi, tôi chỉ cần hiển thị nhà soạn nhạc. Nhưng tôi không thể làm như vậy. MFMailComposeViewController * composeVC = [[MFMailComposeViewController alloc] init]; . Dòng này bật lên bộ điều khiển cảnh báo. –

7

Theo tôi biết, bạn không thể gửi thư từ Mô phỏng .. MFMailComposeViewController sử dụng hộp thư được định cấu hình trong ứng dụng Thư của iPhone để gửi thư. Trình giả lập không có ứng dụng Thư.

+0

Đối với tác vụ của tôi, tôi chỉ cần hiển thị trình soạn nhạc. Nhưng tôi không thể làm như vậy. MFMailComposeViewController * composeVC = [[MFMailComposeViewController alloc] init]; . Dòng này bật lên bộ điều khiển cảnh báo –

3

Bạn có thể gửi thư bằng kết nối Gmail, bạn có thể gửi thư cho người dùng để bạn cần chèn một số mã và đặt mã của bạn sau mã sử dụng để gửi thư.

- (IBAction)sendMessageInBack:(id)anObject{ 

    NSLog(@"Start Sending"); 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.pdf"]; 



    NSData *dataObj = [NSData dataWithContentsOfFile:writableDBPath]; 

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

    testMsg.fromEmail = @"Your mail id"; 

    testMsg.toEmail = @"sender mail ids"; 

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

    testMsg.requiresAuth = YES; 

    testMsg.login = @"Uour mail id"; 

    testMsg.pass = @"your pass"; 

    testMsg.subject = @"Test application "; 

    testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS! 

    // Only do this for self-signed certs! 

    // testMsg.validateSSLChain = NO; 

    testMsg.delegate = self; 

    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, 

           @"Some text to include in body",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; 




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

    [testMsg send]; 



} 


-(void)messageSent:(SKPSMTPMessage *)message{ 
    [message release]; 
    NSLog(@"delegate - message sent"); 
} 



-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{ 
    [message release]; 
    // open an alert with just an OK button 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to send email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert show]; 
    [alert release]; 
    NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]); 
} 

Và các tệp sau được sao chép vào dự án của bạn.

enter image description here

Đối với tải một mẫu code here.

+0

Api này hoạt động tốt mà không có tệp đính kèm. Nó sẽ sụp đổ khi bạn đính kèm nhiều hơn hai tập tin. Tôi nghĩ MFMailComposeViewController tốt hơn SKPSMTPMessage –

Các vấn đề liên quan