如何在Xcode中的应用程序内发送电子邮件?

时间:2013-06-25 15:08:39

标签: ios email send

我是xcode的新手,我想知道如何在应用中发送电子邮件!我的代码如下,但我不断收到错误“没有可见@interface for'jakem'声明选择器'presentViewControllerAnimated:'”。我的代码完全错了吗?或者我只是忘记声明选择器,我如何声明选择器?我已经在互联网上研究了至少一个小时,但没有任何工作。有人请帮助我!

    -(IBAction)sendEmail{

    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    [composer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
    [composer setToRecipients:[NSArray          arrayWithObjects:@"FrankMurphy.CEO@RomansXIII.com", nil]];
    [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:composer 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];
    }
    else {
    [self dismissViewControllerAnimated:YES];
    }
    }

5 个答案:

答案 0 :(得分:7)

在.h头文件中

....

 #import <UIKit/UIKit.h>


#import <MessageUI/MessageUI.h>



@interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate> // Add the delegate
- (IBAction)showEmail:(id)sender;



@end
<。>在.m实现文件中.....

- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = @"Test Email";
// Email Content
NSString *messageBody = @"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"info@finetechnosoft.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;
}


// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}

答案 1 :(得分:0)

检查您是否是MFMailComposeViewControllerDelegate。 你这样做

@interface YouClassName : UIViewController <MFMailComposeViewControllerDelegate> 

@end

答案 2 :(得分:0)

我认为你使用的是错误的方法。试试

[self presentViewController:(UIViewController *) animated:(BOOL) completion:(void)completion];

而不是:

[self presentViewController:composer animated:YES];

答案 3 :(得分:0)

使用此处的代码在xcode中的应用内发送电子邮件

Send email from iOS app using SendGrid

答案 4 :(得分:-1)

我为Sendgrid工作。我们有一个Objective-c库,可让您快速从应用内部发送电子邮件https://github.com/sendgrid/sendgrid-objc。您可以使用cocoapods在项目中快速安装库。

然后从您的(IBAction)发送电子邮件将如下所示:

-(IBAction)sendEmail{

sendgrid *msg = [sendgrid user:@"username" andPass:@"password"];
msg.to = @"FrankMurphy.CEO@RomansXIII.com";
msg.from = @"me@bar.com";
msg.text = @"hello world";   
msg.html = @"<h1>hello world!</h1>";

[msg sendWithWeb];

}