Heba
Heba

Reputation: 11

Sending in-app email

I need to send an email without exiting the application as the subject and body are already set by the application. I already know how to send an email but I need to exit the application to the email application and click on send to go back to my application. Can't I just send the email without exiting or at least without needing to click on send button, can't there be some sort of a framework that auto-sends the email?

Kind Regards, Heba

Upvotes: 1

Views: 2013

Answers (5)

Filip
Filip

Reputation: 51

Use this short code if you are not able to hard code: < a href="mailto:[email protected]" >Send Mail< /a >

Upvotes: 0

Matjan
Matjan

Reputation: 3607

Here's the code:

(Don't forget to add the messageUI framework to your project!!!)

First import the message framework:

#import <MessageUI/MessageUI.h>

then mark your self as a delegate like this

@interface MYViewController () <MFMailComposeViewControllerDelegate>

then to pull up the composer:

- (IBAction)supportPressed:(id)sender
{
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"[email protected]"]];
        [composeViewController setSubject:@"example subject"];
        [self presentViewController:composeViewController animated:YES completion:NULL];
    }
}

Then to handle the delegate callback and dismiss the composer:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    //Add an alert in case of failure
    [self dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 3

simeon
simeon

Reputation: 4626

You can use MFMailComposeViewController to send in-app email. You can use this code inside one of your view controllers (for example, in response to a button press). Note that you will need to add the MessageUI framework to your app.

#import <MessageUI/MessageUI.h>

The code to present the mail view controller:

MFMailComposeViewController *mail = [[[MFMailComposeViewController alloc] init] autorelease];

mail.mailComposeDelegate = self;

[mail setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];
[mail setSubject:@"Set The Subject Here"];    

[self presentModalViewController:mail animated:YES];

See the documentation for how to implement the mailComposeDelegate - you can use this to dismiss the modal view controller when the email is sent or the user has canceled the task.

Upvotes: 2

aegzorz
aegzorz

Reputation: 2219

To send email within the app you can use the MFMailComposeViewController class.

Like this:

if( [MFMailComposeViewController canSendMail] ) {
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    [mailViewController setRecipients:[NSArray arrayWithObject:@"[email protected]"]];
    [self.navigationController presentModalViewController:mailViewController animated:YES];
    [mailViewController release];
}

Upvotes: 0

user745098
user745098

Reputation:

Look for MFMailComposeViewController . Also refer this post

Upvotes: 0

Related Questions