我正在使用以下代码发送电子邮件
- (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.");
}
}
我做错了你能告诉我吗
非常感谢你:)
答案 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"];
}