无法使用目标c发送邮件?

时间:2017-05-10 11:16:09

标签: objective-c

我非常擅长客观ci我试图在用户点击发送按钮时发送电子邮件。每当我点击按钮时if条件不满足并且退出循环也不会发送电子邮件。

这是我的.h文件

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface complaintsviewcontroller : UITableViewController<MFMailComposeViewControllerDelegate>

这是我的.m文件

- (IBAction)sendbutton:(id)sender {
    if ([MFMailComposeViewController canSendMail])
    {
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"testing ";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"noreply@etowns.in"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];

    // 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;
    }
    [self dismissViewControllerAnimated:YES completion:NULL];
}

任何人都可以建议我在上面的编码中犯了什么错误?

1 个答案:

答案 0 :(得分:0)

根据您需要检查的标准 MFMailComposeViewController能够在发送之前使用以下条件发送您的邮件。

if ([MFMailComposeViewController canSendMail]){
     // configure your email part here 
     // if not checking, this misleads the standards or leads uncertainty further  
}

如果您尚未在 settings -> mail 下配置邮件邮件帐户,则会出现如下警告。

enter image description here

最终解决方案:如果您使用 gmail,yahoo 或任何人获得上述弹出窗口configure the mail account,那么mailcomposer将能够发送电子邮件代表。