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 开发者_StackOverflow社区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
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:@[@"example@email.com"]];
[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];
}
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@example.com"]];
[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.
Look for MFMailComposeViewController . Also refer this post
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:@"mail@address.com"]];
[self.navigationController presentModalViewController:mailViewController animated:YES];
[mailViewController release];
}
Use this short code if you are not able to hard code: < a href="mailto:example@example.com" >Send Mail< /a >
精彩评论