Sending email message from the app using MFMailComposeViewController in MessageUI on iPhone
Asked Answered
V

3

6

I'm a software developer, and I'm making an application for the email message and I have the following code:

// Header file  

// importing the MessageUI framework  
#import <MessageUI/MessageUI.h>  

// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)  
@interface TutorialProjectViewController : UIViewController <MFMailComposeViewControllerDelegate> {  

}  

- (IBAction)pressTheMailButtonDudeFunction:(id)sender

// Implementation file  

- (IBAction)pressTheMailButtonDudeFunction:(id)sender {  

    // allocatind new message composer window  
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];  

    // setting a delegate method to "self"  
    mc.mailComposeDelegate = self;  

    // pre-populating the message subject  
    [mc setSubject:@"Send me a message"];  

    // adding content of the message as a plain text  
    [mc setMessageBody:@"Send me a message is you like this tutorial :)" isHTML:NO];  

    // adding content of the message as an HTML  
    [mc setMessageBody:@"<p>Send me a message is you like this tutorial :)<p>" isHTML:YES];  

    // adding recipients  
    [mc setToRecipients:[NSArray arrayWithObjects:@"Fuerte <[email protected]>", @"[email protected]", nil]];  

    // adding recipients for a send copy to (arrayWithObject or arrayWithObjects)  
    [mc setCcRecipients:[NSArray arrayWithObject:@"[email protected]"]];  

    // adding hidden recipients  
    [mc setBccRecipients:[NSArray arrayWithObject:@"[email protected]"]];  

    // adding image attachment  
    // getting path for the image we have in the tutorial project  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Extra_Xcode_100x100" ofType:@"png"];  

    // loading content of the image into NSData  
    NSData *imageData = [NSData dataWithContentsOfFile:path];  

    // adding the attachment to he message  
    [mc addAttachmentData:imageData mimeType:@"image/png" fileName:@"Collection"];  

    // setting different than the default transition for the modal view controller  
    [mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];  

    /* 
     Modal view controllers transitions: 

     UIModalTransitionStyleCoverVertical => pops up from the bottom, default transition 
     UIModalTransitionStyleCrossDissolve => fade on the screen 
     UIModalTransitionStyleFlipHorizontal => page flip 
     */  

    // displaying our modal view controller on the screen (of course animated has to be set on YES if you want to see any transition)  
    [self presentModalViewController:mc animated:YES];  

    // releasing the controller  
    [mc release];  
}  

// delegate function callback  
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {  
    // switchng the result  
    switch (result) {  
        case MFMailComposeResultCancelled:  
            NSLog(@"Mail send canceled.");  
            /* 
             Execute your code for canceled event here ... 
             */  
            break;  
        case MFMailComposeResultSaved:  
            NSLog(@"Mail saved.");  
            /* 
             Execute your code for email saved event here ... 
             */  
            break;  
        case MFMailComposeResultSent:  
            NSLog(@"Mail sent.");  
            /* 
             Execute your code for email sent event here ... 
             */  
            break;  
        case MFMailComposeResultFailed:  
            NSLog(@"Mail send error: %@.", [error localizedDescription]);  
            /* 
             Execute your code for email send failed event here ... 
             */  
            break;  
        default:  
            break;  
    }  
    // hide the modal view controller  
    [self dismissModalViewControllerAnimated:YES];  
}  

And I'm not getting the proper answer... Is it a right code?

Vetchling answered 25/4, 2011 at 9:3 Comment(11)
it displays too much errors which i can't able to understand.And it displays "Mail send canceled.", "Mail saved.","Mail sent." at the same timeVetchling
Please be a bit more elaborate about your problem.Ironworks
@Nick Weaver: It displays too much errors which i can't able to understand.And it displays "Mail send canceled.", "Mail saved.","Mail sent." at the same time...Vetchling
I have also "import the MessageUI framework" into my project still i m not able to get the answerVetchling
plz elaborate what is the exact problem you are facing.Claude
@Jenifer: It displays too much errors which i can't able to understand.And it displays "Mail send canceled.", "Mail saved.","Mail sent." at the same time... I have also "import the MessageUI framework" into my project still i m not able to get the answerVetchling
If you cant understand the errors give it to us we will see if we can understand....Sweetmeat
import the following in .h file #import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h>Prudenceprudent
@Swapna:I have do that....Now the errors are removed but it displays the three messages"Mail send canceled.", "Mail saved.","Mail sent." at the same time...Vetchling
@Sweeta: are you trying that on device or simulator? if device and without an email account be careful check that by [MFMailComposeViewController canSendMail], and can you please tell me which line of code you're receiving the error?Ashlar
+1 pressTheMailButtonDudeFunctionBerky
E
5
  1. Be sure that you included the MessageUI framework into your iOS project. Within Xcode 4, you can include the framework by selecting your project in the left column. Then selecting the Tab "Build Phases". Here you can click on the arrow left of "Link Binary With Libraries" and you see the list of frameworks which are already included to your app. If MessageUI.framework is missing - just add it there.
  2. The code you posted looks like a complete tutorial code snipped ... so only use the code you need ... and add more features to it step by step. This way you'll see where you add a buggy line of code. Maybe there is no image "Extra_Xcode_100x100.png" in your app bundle.

So, here's a "minimal" MFMailComposeViewController:


- (IBAction)showMinimalModalMailView:(id)sender {
    // get a new new MailComposeViewController object 
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];  

    // his class should be the delegate of the mc  
    mc.mailComposeDelegate = self;  

    // set a mail subject ... but you do not need to do this :)
    [mc setSubject:@"This is an optional mail subject!"];  

    // set some basic plain text as the message body ... but you do not need to do this :)
    [mc setMessageBody:@"This is an optional message body plain text!" isHTML:NO];  

    // set some recipients ... but you do not need to do this :) 
    [mc setToRecipients:[NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]];  

    // displaying our modal view controller on the screen with standard transition  
    [self presentModalViewController:mc animated:YES];  

    // be a good memory manager and release mc, as you are responsible for it because your alloc/init
    [mc release];  

}
Eblis answered 2/5, 2011 at 20:47 Comment(0)
D
2

I had the same issue, every time ran the app on sending the message it crashed; I found that if I removed

[mc setToRecipients:[NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]];

it works fine and just asks me for the email address.

Denton answered 4/1, 2012 at 13:54 Comment(1)
Try that array end as [NSNull null], since nil can't be the end when objects are expected. This was a pre-arc thingFaldstool
D
1

For sending email message from the app your device should be configure for email service.

// importing the MessageUI framework  
#import <MessageUI/MessageUI.h>  

// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)  
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate> {  

}

- (IBAction)sendEMailClick:(id)sender {
    
    //check mail service is configure to your device or not.
    if ([MFMailComposeViewController canSendMail]) {
        
        // get a new new MailComposeViewController object
        MFMailComposeViewController * composeVC = [MFMailComposeViewController new];
        
        // his class should be the delegate of the composeVC
        [composeVC setMailComposeDelegate:self];
        
        // set a mail subject ... but you do not need to do this :)
        [composeVC setSubject:@"This is an optional mail subject!"];
        
        // set some basic plain text as the message body ... but you do not need to do this :)
        [composeVC setMessageBody:@"This is an optional message body plain text!" isHTML:NO];
        
        // set some recipients ... but you do not need to do this :)
        [composeVC setToRecipients:[NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]];
        
        // Present the view controller modally.
        
        [self presentViewController:composeVC animated:true completion:nil];
    } else {
        
        NSLog(@"Mail services are not available or configure to your device");
    }
}

after email is send or cancel click in MFMailComposeViewController delegate methode of MFMailComposeViewControllerDelegate is call, so there you can check email send status.

#pragma mark - MFMailComposeViewControllerDelegate Methode.
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
    
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            
            break;
            
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            
            break;
            
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            
            break;
            
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@",error.description);
            
            break;
    }
    
    // Dismiss the mail compose view controller.
    [controller dismissViewControllerAnimated:true completion:nil];
    
}
Dupery answered 30/6, 2017 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.