i done a sample ipad mail composer application to send a image to another address.so i wrote the following code:
#import <messageUI/MFMailComposeViewController.h>
//to compose mail
-(IBAction)composeMail{
if([self validateImageView]){
[self sendSelectedImage];
}
else{
[self showAlert];
}
}
//to validate image view
-(BOOL)validateImageView{
if(selectedImageView.image){
return YES;
}
else{
return NO;
}
}
//to send selected image
-(void)sendSelectedImage{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
@try {
picker.mailComposeDelegate = self;
[picker setSubject:@"Hello from Triassic!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"shamsudheen@triassicsolutions.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"shamsudheen@triassicsolutions.com", @"shamsudheen@triassicsolutions.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"shamsudheen@triassicsolutions.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSData *myData = UIImagePNGRepresentation(selectedImageView.image);
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];
// Fill out the email body text
NSString *emailBody = @"It is raining in Trivandrum!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
@catch (NSException * ex) {
NSLog([NSString stringWithFormat:@"%@",ex]);
}
@finally {
[picker release];
}
}
//to show a alert box
-(void)showAlert{
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:@"Please select a image from PhotoAlbums!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Continue", nil];
[alertView show];
[alertView release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation==UIInterfaceOrientationLandscapeRight || interfaceOrientation==UIInterfaceOrientationLandscapeLeft);
}
#pragma mark -
#pragma mark Dismiss Mail/SMS view controller
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the
// message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
@try {
feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
feedbackMsg.text = @"Mail sending canceled";
break;
case MFMailComposeResultSaved:
feedbackMsg.text = @"Mail saved";
break;
case MFMailComposeResultSent:
feedbackMsg.text = @"Mail sent";
break;
case MFMailComposeResultFailed:
feedbackMsg.text = @"Mail sending failed";
break;
default:
feedbackMsg.text = @"Mail not sent";
break;
}
}
@catch (NSException * ex) {
NSLog([NSString stringWithFormat:@"%@",ex]);
}
@finally {
[self dismissModalViewControllerAnimated:YES];
}
}
so when compose button clicks it will show a popup with the entered mail address and with all the details.
it showing the result send process is done successfully.but i am not getting any mail to shamsudheen@triassicsolutions.com.may i know what is the mistake i done.can i send a mail through this to another email by entering the popup through the address section.i think the compose method works when popup is loading.then how can i send a mail to address that in entered in the displayed popup.it not working fine ..may i know whats the mistake i done
In Simulator you cannot send the mail because If you want to send a mail to another person first you have to set your maildetails(you have to login to your account) in the account settings in the Device. But that feature is not exists in the Simulator.That’s why you cannot send the email from Simulator.