-2

我正在使用以下代码发送电子邮件

- (IBAction)email:(id)sender {
    if ([MFMailComposeViewController canSendMail]) {

        MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
        mailViewController.mailComposeDelegate = self;
        [mailViewController setSubject:@"Subject Goes Here."];
        [mailViewController setMessageBody:@"Your message goes here." isHTML:NO];

        [self presentModalViewController:mailViewController animated:YES];
        [mailViewController release];
    } else {
        NSLog(@"Device is unable to send email in its current state.");
    }
}

我做错了什么你能告诉我吗

非常感谢你 :)

4

2 回答 2

2

你进口了这个吗?

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

使用了这种委托方式?

MFMailComposeViewControllerDelegate

它很简单

于 2013-04-03T13:26:59.490 回答
0

.h文件中:

#import <MessageUI/MessageUI.h>

并使用MFMailComposeViewControllerDelegate委托

.m文件中:

// Method :

-(void)sendMailto:(NSString*)to WithSubject:(NSString*)subject withBody:(NSString*)body
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:subject];

        NSArray *toRecipients = [NSArray arrayWithObjects:to, nil];
        [mailer setToRecipients:toRecipients];

        NSString *emailBody = body;
        [mailer setMessageBody:emailBody isHTML:NO];

        [self presentModalViewController:mailer animated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fail !" message:@"Please configure your Mail application of device with your email id" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}



- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            activityView.hidden = YES;
            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            activityView.hidden = YES;
            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            activityView.hidden = YES;
            UIStoryboard * myStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
            CustomerAccountViewController *custAcnt = [myStoryboard instantiateViewControllerWithIdentifier:@"customerAcnt"];
            custAcnt.btnID = 1;
            [self.navigationController pushViewController:custAcnt animated:YES];
            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail failed");
            activityView.hidden = YES;
            break;

        default:
            NSLog(@"Mail not sent");
            activityView.hidden = YES;
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

// Call On Button Click 

- (IBAction)btnProceedClicked:(id)sender
{
    [self sendMailto:@"abc@gmail.com" WithSubject:@"Email Subject" withBody:@"Your Mail Body"];
}
于 2013-05-06T12:50:11.053 回答