How to send mail from iphone app?

馋奶兔 提交于 2019-12-11 16:01:51

问题


I am try to send mail from my app. I want to type(dynamically) recipient id,ccid,sub and message.

Thanks

Senthilkumar


回答1:


on iOS 3+ Import #import in your view controller header.

Then:

-(void)showMailPanel {
    MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];

        // only on iOS < 3
    //if ([MFMailComposeViewController canSendMail] == NO)
    //  [self launchMailApp]; // you need to 

    mailComposeViewController.mailComposeDelegate = self;
    [mailComposeViewController setToRecipients:[NSArray arrayWithObjects:@"email address1",@"email address 2",nil]];
    [mailComposeViewController setSubject:@"your subject"];
    [mailComposeViewController setMessageBody:@"your body" isHTML:YES];
    mailComposeViewController.delegate = self;
    [self.navigationController presentModalViewController:mailComposeViewController animated:YES];

    [mailComposeViewController release];

}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{   
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Result: sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: failed");
            break;
        default:
            NSLog(@"Result: not sent");
            break;
    }
    [controller dismissModalViewControllerAnimated:YES];
}

-(void)launchMailApp {
{
    NSString *recipients = @"dest_email";
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, subject];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}



回答2:


You need things like this:

//Import this in your .h file
#import <MessageUI/MessageUI.h>

...

    MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease];
    mailController.mailComposeDelegate = self;
    [mailController setSubject:[NSString stringWithFormat:@"Report a problem - #%@", yourUserID]];
    [mailController setToRecipients:[NSArray arrayWithObject:@"support@yourWebsite.com"]];
    [self presentModalViewController:mailController animated:YES];

Besides, the parent view controller (the delegate) needs to conform to the MFMailComposeViewControllerDelegate protocol:

    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
        //Extra implementation here...
        [self dismissModalViewControllerAnimated:YES];
    }


来源:https://stackoverflow.com/questions/4942976/how-to-send-mail-from-iphone-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!