【问题标题】:Unable to send email through my app无法通过我的应用发送电子邮件
【发布时间】:2012-08-28 06:54:43
【问题描述】:

我是 iphone 开发新手。我正在构建一个可以发送电子邮件的应用程序。我正在使用以下代码

- (IBAction)btnsendPressAction:(id)sender {

    if([MFMailComposeViewController canSendMail]){

        MFMailComposeViewController *mailer=[[MFMailComposeViewController alloc]init];
        mailer.mailComposeDelegate=self;
        [mailer setSubject:@"A Message for testing"];
      NSArray *toRecipients = [NSArray arrayWithObjects:@"rajshetty2125@gmail.com", @"secondMail@example.com", nil];
        [mailer setToRecipients:toRecipients];

        UIImage *image=[UIImage imageNamed:@"logo.png"];
        NSData *imageData=UIImagePNGRepresentation(image);
        [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"logo.png"];
        NSString *emailBody=@"This is test email";
        [mailer setMessageBody:emailBody isHTML:NO];
        [self presentModalViewController:mailer animated:YES];
        mailer.modalPresentationStyle = UIModalPresentationPageSheet ;


    }
    else{

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"failure" message:@"Alert" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

我的问题是 mailComposeController 委托没有被调用,我无法通过我的应用程序发送电子邮件。我浏览了许多链接,但每个地方都找到了相同的方法。谁能帮我解决这个问题?

【问题讨论】:

  • 我还在我的代码中添加了代理的正确方法,我没有放在这里。
  • 检查您使用的设备中是否配置了邮件ID。
  • 我正在测试我的模拟器,我可以知道如何配置吗?
  • 好的,您将无法通过模拟器发送邮件并在设备中进行配置:support.apple.com/kb/ht4810
  • 这意味着我无法在模拟器中测试它??

标签: objective-c ios xcode email mfmailcomposeviewcontroller


【解决方案1】:

您将无法从模拟器发送电子邮件。您只能使用在邮件应用程序中配置的任何帐户在 iOS 设备上检查此功能。

【讨论】:

  • 好的,我会检查一次真实设备并回复您
  • 是的,好的。我可能需要一些时间在设备上进行测试,因为到目前为止我还没有设备
  • 它在设备中正常工作,但我想附加的图像没有附加
  • 在模拟器中,我可以发送邮件,但实际邮件并没有发送到收件人的邮件地址。那是一切看起来都很正常,只是邮件不会'无法交付
【解决方案2】:

在这里使用link 和你在设备中添加(配置)一个帐户,然后只有你可以发送邮件。

【讨论】:

  • 这将允许调用委托吗?
  • 您可以使用该代码,它对我有用。您需要设置委托并同时符合 MFMailComposerDelegate.i 下来请告诉我这有什么问题??
【解决方案3】:

这个 sn-p 对我有用:

if ([MFMailComposeViewController canSendMail]) {
    self.mailController = [[MFMailComposeViewController alloc] init];
    [self.mailController setSubject:@"Invitation"];
    [self.mailController setMessageBody:@"Message" isHTML:NO];
    self.mailController.mailComposeDelegate = self;

    [self presentModalViewController:self.mailController animated:YES];
}

但请确保您在接口中实现委托

@interface MyClass() <MFMailComposeViewControllerDelegate>

并检查您的回调方法:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}

【讨论】:

  • 是的,它在模拟器上工作,我的委托被调用,但您不会在您的电子邮件帐户上收到电子邮件,尝试创建您的 MFMailComposeViewController 的属性,也许 ARC 在他们发送委托或之前释放您的 mailViewController某事代码中不需要收件人地址,可以从通讯录中选择或手动输入。
【解决方案4】:

嘿,我不确定,但可能有帮助,替换你的最后一行

mailer.modalPresentationStyle = UIModalPresentationPageSheet ;

[self presentModalViewController: mailer animated:YES];

【讨论】:

  • 当我用第一行检查代码时,我已经在我的应用程序中进行了个人测试,它没有显示邮件视图。当我尝试使用我的第二行时,它是鞋子操作表,我什至可以发送邮件。所以它不是被否决的答案
猜你喜欢
  • 2016-06-24
  • 1970-01-01
  • 2013-02-21
  • 2012-01-26
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多