close

http://stackoverflow.com/questions/11863527/cannot-send-email-in-app-using-mfmailcomposeviewcontroller

First add and Import the MessageUI Framework

#import <MessageUI/MessageUI.h>

and Declare the MFMaileComposeViewControllerDelegate

@interfaceMailViewController:UIViewController<MFMailComposeViewControllerDelegate>

Write this code for sending the mail

-(IBAction)openMail:(id)sender 
{if([MFMailComposeViewController canSendMail]){MFMailComposeViewController*mailer =[[MFMailComposeViewController alloc] init];

    mailer.mailComposeDelegate =self;[mailer setSubject:@"xyz"];NSArray*toRecipients =[NSArray arrayWithObjects:@"fisrtMail@example.com",@"secondMail@example.com",nil];[mailer setToRecipients:toRecipients];NSData*pdfdata =[NSData dataWithContentsOfURL:"Your URL"][mailController addAttachmentData:pdfData
                             mimeType:@"application/pdf"
                             fileName:@"file.pdf"];NSString*emailBody =@"xyz";[mailer setMessageBody:emailBody isHTML:NO];[self presentModalViewController:mailer animated:YES];[mailer release];}else{UIAlertView*alert =[[UIAlertView alloc] initWithTitle:@"Failure"
                                                    message:@"Your device doesn't support the composer sheet"delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];[alert show];[alert release];}

}

and also write the delegate method of MFMailComposeViewControllerDelegate

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{switch(result){caseMFMailComposeResultCancelled:NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");break;caseMFMailComposeResultSaved:NSLog(@"Mail saved: you saved the email message in the drafts folder.");break;caseMFMailComposeResultSent:NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");break;caseMFMailComposeResultFailed:NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");break;default:NSLog(@"Mail not sent.");break;}// Remove the mail view[self dismissModalViewControllerAnimated:YES];}
arrow
arrow
    全站熱搜

    pcwiki 發表在 痞客邦 留言(0) 人氣()