Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send email using MFMailComposeViewController in simulator

Tags:

ios

iphone

I am new to ios app development, Below is the code I used to send an email.

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

Unfortunately delegate methods are never triggered , Can any one please suggest how can i check my email via simulator?

like image 649
Mahesh Babu Avatar asked Aug 19 '10 09:08

Mahesh Babu


3 Answers

You CANNOT send mails through Simulator.

Instead you can install the application in device and try from there.

Simulator just displays the composer but wont allow you to send mails. Sent Successfully is just the acknowledgment that your code is fine and there is no issue that terminates it while sending.

like image 168
Suresh Varma Avatar answered Dec 04 '22 08:12

Suresh Varma


As far as i know, you cannot send mail from Simulator.. The MFMailComposeViewController uses the mailbox configured in iPhone's Mail app to send the mail. The simulator does not have the Mail app.

like image 28
Swapnil Luktuke Avatar answered Dec 04 '22 06:12

Swapnil Luktuke


You can able to send mail using the Gmail connectivity you can send mail to user for that you need to insert the some amount of code and setting in your code following code which use for sending a mail.

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

And following files copy into your project.

enter image description here

For downloading a sample code here.

like image 28
Nimit Parekh Avatar answered Dec 04 '22 08:12

Nimit Parekh