我用xcode创建了邮件发送功能。
- (void)onShareBtn:(UIButton *)button
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Hello!"];
// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"first@gmail.com",
nil];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@gmail.com",
nil];
NSArray *bccRecipients = [NSArray arrayWithObjects:@"third@gmail.com",
nil];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
[picker setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
// Attach an image to the email.
NSString *path = [[NSBundle mainBundle] pathForResource:@"share"
ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png"
fileName:@"share"];
// Fill out the email body text.
NSString *emailBody = @"It is raining!";
[picker setMessageBody:emailBody isHTML:NO];
// Present the mail composition interface.
[self presentModalViewController:picker animated:YES];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
if(error)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error" message:[NSString stringWithFormat:@"error %@",[error description]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissViewControllerAnimated:YES completion:NULL];
}
else
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
}
此代码只能显示邮件表单,当我点击发送按钮时,它无法正确地将邮件发送给收件人。 此功能仅显示发送邮件表单。 如何为正确发送给收件人的邮件更新此代码?