How can I send mail from an iPhone application

前端 未结 11 1727
甜味超标
甜味超标 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:31

    This is the code which can help u but dont forget to include message ui framewark and include delegates method MFMailComposeViewControllerDelegate

    -(void)EmailButtonACtion{
    
            if ([MFMailComposeViewController canSendMail])
            {
                MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
                controller.mailComposeDelegate = self;
                [controller.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation_bg_iPhone.png"] forBarMetrics:UIBarMetricsDefault];
                controller.navigationBar.tintColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
                [controller setSubject:@""];
                [controller setMessageBody:@" " isHTML:YES];
                [controller setToRecipients:[NSArray arrayWithObjects:@"",nil]];
                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                UIImage *ui = resultimg.image;
                pasteboard.image = ui;
                NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(ui)];
                [controller addAttachmentData:imageData mimeType:@"image/png" fileName:@" "];
                [self presentViewController:controller animated:YES completion:NULL];
            }
            else{
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"alrt" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil] ;
                [alert show];
            }
    
        }
        -(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
        {
    
            [MailAlert show];
            switch (result)
            {
                case MFMailComposeResultCancelled:
                    MailAlert.message = @"Email Cancelled";
                    break;
                case MFMailComposeResultSaved:
                    MailAlert.message = @"Email Saved";
                    break;
                case MFMailComposeResultSent:
                    MailAlert.message = @"Email Sent";
                    break;
                case MFMailComposeResultFailed:
                    MailAlert.message = @"Email Failed";
                    break;
                default:
                    MailAlert.message = @"Email Not Sent";
                    break;
            }
            [self dismissViewControllerAnimated:YES completion:NULL];
            [MailAlert show];
        }
    

提交回复
热议问题