Sending Email with Attachments

This tutorial gives you an idea on how to send emails in objective c with attachments.

First we are aware that iPhone has this capablities already. But the question is how could we be able to use this feature in our own applications?

Here is the answer: read.

Anyway, there are two ways to implement emails sending in Objective C.

You could either use the UIApplication or the MFMailComposeViewController in MessageUI Framework.

Here is the procedure on sending an email using UIApplication.

Attach the code from this tip to a button, you can give your users a way to promote your app to their friends via the iPhone’s built in Mail app.

- (void)sendEmailTo:(NSString*)to withCC:(NSString*)cc withBCC:(NSString*)bcc withSubject:(NSString*)subject withBody:(NSString*)body {
	NSString * url = [NSString stringWithFormat:@"mailto:?to=%@&cc=%@&bcc=%@&subject=%@&body=%@",
					  [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [cc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [bcc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
 
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

NOTE: Running this function in the iPhone Simulator will have no effect because the simulator does not include the iPhone’s Mail app. To see the effects of this function, it must be run on a device.

Usage

Once you have that message defined in one of your classes, you can open the Mail app with a pre-composed email with just one message call. The following example opens an email and specifies a to, cc and bcc address.

[self sendEmailTo:@"me@me.com"
           withCC:@"cc@cc.com"
          withBCC:@"bcc@bcc.com"
      withSubject:@"Subject"
         withBody:@"<p>Hello, <a href='http://zaldzbugz.wordpress.com'>Hello</a>, I am here!</p><p><a href='http://google.com'><img src='image source here...'/></a></p>"];

Next is using the MessageUI Framework.

//First we need to reference the Message UI framework.Then import
<MessageUI/MessageUI.h> and <MessageUI/MFMailComposeViewController.h>
//Here is the framework link incase you forgot. Macintosh
HD>Developer>Platforms>iPhoneOS.platform>developer>SDKs>iPhoneOS3.0.sdk>System>Library>Frameworks

//Add this protocol to any interface you were using
<MFMailComposeViewControllerDelegate>

Then add this code in you implementation file.

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
 [self dismissModalViewControllerAnimated:YES];
}

-(IBAction)mailIt {

 MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 picker.mailComposeDelegate = self;

 [picker setSubject:@"This is a sample Subject"];

 //image...
 UIImage *roboPic = [UIImage imageNamed:@"podd.jpg"];
 NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
 [picker addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"podd.jpg"];

 //audio

 NSString *emailBody = @"This is a sample Body content.!";
 [picker setMessageBody:emailBody isHTML:YES];

 [self presentModalViewController:picker animated:YES];
 [picker release];
}

Happy Coding…:)

Advertisement
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.