Send Email with Attachment in iphone
1) In Xcode 4 goto the build phases tab for your target. Make sure you see MessageUI.framework. If it's not there click + to add a new framework.
2)In your interface file
#import <MessageUI/MFMailComposeViewController.h>
@interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate>
3)Add this Method
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Check My Image!"];
// Set up recipients
// NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
// NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];
// [picker setToRecipients:toRecipients];
// [picker setCcRecipients:ccRecipients];
// [picker setBccRecipients:bccRecipients];
// Attach an image to the email
UIImage *myImage = YourImage;
NSData *myData = UIImagePNGRepresentation(myImage);
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"myImage.png"];
// Fill out the email body text
NSString *emailBody = @"My Pic";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
4) Implement the delegate method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(@"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Result: failed");
break;
default:
NSLog(@"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
1) In Xcode 4 goto the build phases tab for your target. Make sure you see MessageUI.framework. If it's not there click + to add a new framework.
2)In your interface file
#import <MessageUI/MFMailComposeViewController.h>
@interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate>
3)Add this Method
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Check My Image!"];
// Set up recipients
// NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
// NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];
// [picker setToRecipients:toRecipients];
// [picker setCcRecipients:ccRecipients];
// [picker setBccRecipients:bccRecipients];
// Attach an image to the email
UIImage *myImage = YourImage;
NSData *myData = UIImagePNGRepresentation(myImage);
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"myImage.png"];
// Fill out the email body text
NSString *emailBody = @"My Pic";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
4) Implement the delegate method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(@"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Result: failed");
break;
default:
NSLog(@"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
No comments:
Post a Comment