How can I send mail from an iPhone application

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

    Heres a Swift version:

    import MessageUI
    
    class YourVC: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            if MFMailComposeViewController.canSendMail() {
                var emailTitle = "Vea Software Feedback"
                var messageBody = "Vea Software! :) "
                var toRecipents = ["pj@veasoftware.com"]
                var mc:MFMailComposeViewController = MFMailComposeViewController()
                mc.mailComposeDelegate = self
                mc.setSubject(emailTitle)
                mc.setMessageBody(messageBody, isHTML: false)
                mc.setToRecipients(toRecipents)
                self.presentViewController(mc, animated: true, completion: nil)
            } else {
                println("No email account found")
            }
        }
    }
    
    extension YourVC: MFMailComposeViewControllerDelegate {
        func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
            switch result.value {
            case MFMailComposeResultCancelled.value:
                println("Mail Cancelled")
            case MFMailComposeResultSaved.value:
                println("Mail Saved")
            case MFMailComposeResultSent.value:
                println("Mail Sent")
            case MFMailComposeResultFailed.value:
                println("Mail Failed")
            default:
                break
            }
            self.dismissViewControllerAnimated(false, completion: nil)
        }
    }
    

    Source

    0 讨论(0)
  • 2020-11-22 12:23

    If you want to send email from your application, the above code is the only way to do it unless you code your own mail client (SMTP) inside your app, or have a server send the mail for you.

    For example, you could code your app to invoke a URL on your server which would send the mail for you. Then you simply call the URL from your code.

    Note that with the above code you can't attach anything to the email, which the SMTP client method would allow you to do, as well as the server-side method.

    0 讨论(0)
  • 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 <MessageUI/MFMailComposeViewController.h>
    

    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 , <br/>  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];
    }
    
    0 讨论(0)
  • 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];
        }
    
    0 讨论(0)
  • 2020-11-22 12:37

    I wrote a simple wrapper called KRNSendEmail that simplify sending email to one method call.

    The KRNSendEmail is well documented and added to CocoaPods.

    https://github.com/ulian-onua/KRNSendEmail

    0 讨论(0)
提交回复
热议问题