点击按钮问题发送电子邮件

时间:2013-04-03 13:24:25

标签: ios

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

- (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.");
    }
}

我做错了你能告诉我吗

非常感谢你:)

2 个答案:

答案 0 :(得分:2)

你输入了吗?

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

使用了这种委托方法吗?

MFMailComposeViewControllerDelegate

非常简单

答案 1 :(得分: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"];
}