我正在使用ObjectiveC,我想发送一封电子邮件到我在sqlite数据库中的电子邮件地址列表。电子邮件阵列包含我要发送邮件的地址。 我告诉你我的代码
- (void)sendEmailButtonClicked: (id)sender {
// Email Subject
NSString *emailTitle = @"Test Email";
// Email Content
NSString *messageBody = @"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:emailArray];
NSLog(@"What are the emais %@",toRecipents);
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setToRecipients:toRecipents];
[mc setMessageBody:messageBody isHTML:NO];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
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];
}
我收到此错误:***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSArrayM mf_isLegalCommentedEmailAddress]:无法识别的选择器发送到实例0x78e5e180'
有人可以帮我吗?非常感谢你!
答案 0 :(得分:3)
首先它应该是:
NSArray *toRecipents = [NSArray arrayWithArray:emailArray];
但为什么不这样做:
[mc setToRecipients:emailArray];
假设电子邮件数组是一个有效的数组对象,因为我们无法从您的代码中看到您在何处/如何创建它。
修改强>
如果没有设置电子邮件帐户,您可能无法从模拟器发送电子邮件。请检查以下内容:
if ([MFMailComposeViewController canSendMail]) {
//Do your email stuff
}
else {
//Present an error etc ...
}
答案 1 :(得分:0)
您的阵列存在问题。发送给多个收件人时,此代码可以正常工作:
//Email
-(void)mailButton {
NSArray *emailArray = @[@"me@gmail.com", @"you@gmail.com", @"him@gmail.com", @"her@gmail.com", @"everyone@gmail.com"];
if ([MFMailComposeViewController canSendMail]) {
NSString *subject = @"Subject";
NSString *messageBody = [NSString stringWithFormat:@"Message Body"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:subject];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:emailArray];
[self presentViewController:mc animated:YES completion:NULL];
}
else {
UIAlertView *emailError = [[UIAlertView alloc] initWithTitle:@"Email Unavailable"
message:@"Sorry, were unable to find an email account on your device.\nPlease setup an account in your devices settings and try again."
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:nil];
[emailError show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
答案 2 :(得分:-1)
toRecipents
应该是一个数组,其中只包含一个电子邮件地址。
如下所示,
toRecipents = @[@"foo@example.com"];
如果您要向某些人发送电子邮件,则应使用setCcRecipients
方法。