How can I send mail from an iPhone application

前端 未结 11 1907
甜味超标
甜味超标 2020-11-22 11:47

I want to send an email from my iPhone application. I have heard that the iOS SDK doesn\'t have an email API. I don\'t want to use the following code because it will exit my

11条回答
  •  暖寄归人
    2020-11-22 12:26

    Below code is used in my application to send email with an attachment here the attachments is an image .You can send any type of file only thing is to keep in mind is that you had to specify the correct 'mimeType'

    add this to your .h file

    #import 
    

    Add MessageUI.framework to your project file

    NSArray *paths = SSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"myGreenCard.png"];
    
    
    
    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:@"Green card application"];
    [controller setMessageBody:@"Hi , 
    This is my new latest designed green card." isHTML:YES]; [controller addAttachmentData:[NSData dataWithContentsOfFile:getImagePath] mimeType:@"png" fileName:@"My Green Card"]; if (controller) [self presentModalViewController:controller animated:YES]; [controller release];

    Delegate method is as shown below

      -(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error;
    {
        if (result == MFMailComposeResultSent) {
            NSLog(@"It's away!");
        }
        [self dismissModalViewControllerAnimated:YES];
    }
    

提交回复
热议问题