This is part of an ELC Tech Network

iPhone Coding Tutorial – In Application Emailing

IMG_0001A lot of applications you see have an email button. When you click it then it will leave the application and take you to the Mail application. It can get really annoying leaving the application and then going back in after your done sending the email. This is just a great way to show off your app and make your app look more professional and make it easier on the user by filling in the To and Subject for them. They are also able to change the From to whatever email then want you to receive an email from just like you can do in the Mail app. So todays tutorial is gonna show you how to email within your application. We will be using the MessageUI framework and we will also include an attachment in the email. Theres a screenshot to the left of how it will look.

So lets get started…

1. Create A New View Based Application
You can name yours whatever you want, in the tutorial I will be referring it as the NameViewControllers.

2. Import The Frameworks

The first thing we need to do is import the framework. So go to your Frameworks folder in the Files In Pain section on the left. Open the folder and right click one of the frameworks and click “Reveal In Finder.” Heres a screenshot on what you should do.

Screen shot 2009-11-18 at 7.58.04 PM

Go ahead and look for the “MessageUI.framework” and highlight it then drag it into your frameworks folder. Make sure that when you click Add on the thing that makes sure you want to import it that “Copy items into destination group’s folder (if needed)” is not check. Thats a very important step or you will have big time problems and you do this with any frameworks you would ever use in the future.

3. Implementing The Code

Now that we have imported the framework lets get into some coding. So go ahead and jump in the NameViewController.H and make an IBAction for a button. Then copy that code and paste it in the NameViewController.M with curly brackets. Also make sure to add the button in Interface Builder and link it up with a Touch Up Inside method. You guys are at the point to knowing how to hook up actions and dragging stuff into Interface Builder. After that we want to on the top of the NameViewController.h import the framework. So on the top do #import “MessageUI/MessageUI.h” and the reason why we do this is because we must import out MessageUI framework to make calls to the associated header files. Now we need to also put in the delegate protocols for this framework. @interface NameViewController: UIViewController do this code <MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>. Heres the code here for the NameViewController.H

#import <MessageUI/MessageUI.h>

@interface MailComposerViewController : UIViewController

<MFMailComposeViewControllerDelegate,UINavigationControllerDelegate>  {

}

-(IBAction)pushEmail;

@end


Now after your done with the .H jump into the .M viewcontroller. Now in your button action code do this:

-(IBAction)pushEmail {

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

mail.mailComposeDelegate = self;

if ([MFMailComposeViewController canSendMail]) {

//Setting up the Subject, recipients, and message body.

[mail setToRecipients:[NSArray arrayWithObjects:@"email@email.com",nil]];

[mail setSubject:@"Subject of Email"];

[mail setMessageBody:@"Message of email" isHTML:NO];

Advertisement

//Present the mail view controller

[self presentModalViewController:mail animated:YES];

}

//release the mail

[mail release];

}

//This is one of the delegate methods that handles success or failure

//and dismisses the mail

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

{

[self dismissModalViewControllerAnimated:YES];

if (result == MFMailComposeResultFailed) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Message Failed!” message:@”Your email has failed to send” delegate:self cancelButtonTitle:@”Dismiss” otherButtonTitles:nil];

[alert show];

[alert release];

}

}


What if we wanted to include an image attachment to the email? Well its quite simple. Just add this to the code right under the part where you set up the Recipient, Subject, and the Message.

UIImage *pic = [UIImage imageNamed:@"Funny.png"];

NSData *exportData = UIImageJPEGRepresentation(pic ,1.0);

[mail addAttachmentData:exportData mimeType:@"image/jpeg" fileName:@"Picture.jpeg"];

Now we are setting up the MailComposer in the first part of the code in the action. Then we call a didFinishWithResult method where we are setting up if the email fails or sends. Also it sets up a Cancel button for you so we have to call the dismiss method so that it works. In the attachment code just edit the imageNamed:@”" with your images name.That is basically it! The source code is below for the people that just doesn’t wanna copy and paste or type… I am just jking with you guys. Happy iCoding!

iPhone Tutorial – In Application Emailing

This entry was posted in iPhone Programming Tutorials. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

33 Comments

  1. David
    Posted November 19, 2009 at 1:11 pm | Permalink

    Thanks, that was a great tutorial. It can be so simple to do things on the iPhone when you know the right frameworks and protocols.

    Talking of frameworks, if you ctrl click on the frameworks folder in xcode, then choose Add, Existing Frameworks… and then select MessageUI from the list, is that doing the same thing as your section 2?

    • AppStoreMod
      Posted November 19, 2009 at 6:55 pm | Permalink

      Yea you can do it that way too. There is different ways to doing it.

  2. Julian
    Posted November 20, 2009 at 2:59 am | Permalink

    This is one of the last hings Ineeded for my App, all the other tutorials in the internet didn’t really work!

    Great Job!

  3. 1223
    Posted November 21, 2009 at 1:40 pm | Permalink

    Hmm, this doesnt seem to work for me, even the downloaded version…

    • AppStoreMod
      Posted November 26, 2009 at 6:18 pm | Permalink

      Try re-downloading it now. I fixed the errors. Also make sure you build the app on your iPhone/iPod Touch.

  4. Alex
    Posted November 24, 2009 at 12:10 am | Permalink

    It wasn’t working for me either – there are a few problems with the code.

    1. In the attachment section, ‘pic’ is undeclared. I think it should be ‘picture’ but I still can’t get the attachment to work
    2. I don’t know what [mailComposer release] is supposed to refer to but it’s undeclared.
    3. The IB connection between the button and the method is not set up correctly. You need to make the connection to ‘pushMail’ instead of ‘sendEmail’ (which doesn’t exist)

    And yes – you need to run it on a real device since the simulator doesn’t have email functionality.

    • AppStoreMod
      Posted November 26, 2009 at 6:19 pm | Permalink

      I have fixed the errors in the post and source code. You can re-download it and try it now.

  5. Ipodmail
    Posted November 24, 2009 at 3:26 am | Permalink

    I am also having the same problems as Alex, could someone shine a light on this topic ?

    • AppStoreMod
      Posted November 26, 2009 at 6:20 pm | Permalink

      I have fixed the errors in the post and source code. You can just try and re-download it and try it now.

  6. dan
    Posted November 24, 2009 at 5:52 pm | Permalink

    [mailComposer release]; should be [mail release];

  7. Posted November 25, 2009 at 3:06 am | Permalink

    you always make things really easy :) thanks

    i wonder how much memory it takes this mail controller, should I worry what else there’s loaded, while this is modally presented?

  8. nadam
    Posted November 29, 2009 at 9:19 am | Permalink

    Hi!
    I was wondering how would you do that lets say I have two buttons one says DAD other says MUM any time i press MUM it will send an email with a pic of flowers,if press DAD it sends a pic with a pint of beer. Any ideas?

  9. babydeveloper
    Posted December 2, 2009 at 9:44 pm | Permalink

    Hi!

    i was looking for something like that for my app , and i download the sourceCode (the new version) , i send an email but it doesn’t work :(
    what i am doing wrong , or is because of me ???

    thanks!

  10. Posted December 4, 2009 at 9:56 am | Permalink

    Really appreciate you showing us the code for this! Makes it real clear for me!

  11. Posted December 4, 2009 at 11:30 pm | Permalink

    Any luck with the image attachment? I can’t get it to load, even after making the changes to the code for “pic” etc.

  12. Momeks
    Posted December 6, 2009 at 3:04 pm | Permalink

    Plz include a sample code of your tutorial for solve any problems ..

  13. parijat
    Posted December 7, 2009 at 3:54 am | Permalink

    is there any way to trap the event on composer field like taping or double tapping

  14. Posted December 7, 2009 at 8:22 am | Permalink

    Thanks for this tutorial AppStoreMod, you really have a neck for making things really easy and simple.
    I guess the right way to say it is you are straight to the point in your tutorial instead of trying to show off a little here and there.
    Perfection :)

    I am wondering what is the role of “UINavigationControllerDelegate” in this case, because when I remove that, it still works.

  15. Momeks
    Posted December 8, 2009 at 4:11 am | Permalink

    guys ! i am have some problem .. who can attach his sample code [emailing tutorial] ?

  16. Momeks
    Posted December 8, 2009 at 4:16 am | Permalink

    Ops i found my problem . it works fine now :)

  17. Posted December 30, 2009 at 1:35 pm | Permalink

    I keep on failing when i put in: !!!

    Help anyone???

  18. Posted December 30, 2009 at 1:36 pm | Permalink
  19. Posted December 30, 2009 at 1:36 pm | Permalink

    MFMailComposeViewControllerDelegate,UINavigationControllerDelegate

  20. Chris
    Posted January 11, 2010 at 12:16 pm | Permalink

    I’m running into trouble – I have a similar solution where i have a modal view with a send email button that cretaes the modal email composer view. I guess it’s a problem to set this initial modal view as a delegate for the modal email composer too. When I send a dismissModalView to the mail controller in the callback method, is it possible that i send the dismiss to the initial modal view too? Debugger is not very helpful here, I get a msg: Program received signal: “EXC_BAD_ACCESS”.

  21. Ken
    Posted January 30, 2010 at 6:47 am | Permalink

    Great tut
    I am just wondering how to alter the image attach code so that in a scroll view app the user would have the option to e-mail that specific picture. Would I create *pic *pic1 etc and than how would I hook up the e-mail button in image view or is it better to create different buttons and views for each picture?

    Thanks,
    Ken

  22. imtiyaz
    Posted March 12, 2010 at 2:13 pm | Permalink

    Thanks for your good work.

    Does anyone has idea how to develop email receiving application.

  23. Mohammad Asif
    Posted March 22, 2010 at 1:10 am | Permalink

    This is very helpful coding page. I would like to have the code for sending and cancelation of the email as well if u can provide it to me…

  24. Posted March 26, 2010 at 4:41 am | Permalink

    gave an errors in my own app. What is the MFMailComposeViewControllerDelegate? Do I need to keep it like this or change it somehow? How should I understand it? TKS

  25. Posted March 26, 2010 at 5:21 am | Permalink

    Sorry, my fault, beginners mistake. Didn’t import frameworks….uppps

  26. sathya T
    Posted May 25, 2010 at 5:48 am | Permalink

    I get the following link error:

    ld: warning: in /Users/sathya/Documents/Projects/iPhone/MyMail/MessageUI.framework/MessageUI, missing required architecture i386 in file
    Undefined symbols:
    “_OBJC_CLASS_$_MFMailComposeViewController”, referenced from:
    objc-class-ref-to-MFMailComposeViewController in MyMailViewController.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

  27. Sean Henderson
    Posted June 11, 2010 at 9:45 am | Permalink

    I have entered the code in my own app and after clicking the button it crashes. Not sure were the problem is with my coding. Can someone help?

  28. Gregory Ryslik
    Posted June 19, 2010 at 3:50 pm | Permalink

    I am a beginner in iphone development though I’ve had quite a bit of experience with c, c++, etc.

    I get everything to compile and editable, but when I click send I don’t actually receive an email. Is this not built in or am I missing something! Thanks for your help!

  29. Posted July 27, 2010 at 2:37 pm | Permalink

    Awesome tutorial! Thanks for making it so clear. Is there any way to display the attached image in the body of the message?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">