How to setToRecipients MFMailComposerViewController in Xcode6 - objective-c

I am Using Xcode 6.
I am using MFMailComposerViewController to send emails within the app.
I am able to call MFMailComposerViewController,which provides the standard Interface to send emails.
I am able to set the subject as well.
But I am not able to set the Recipients.
Here is my code.
if ([MFMailComposeViewController canSendMail]) {
mailComposer = [[MFMailComposeViewController alloc]init];
[mailComposer setSubject:#"PSA Mobile CRM"];
NSArray *toRecipients = [NSArray arrayWithObject:#"abc#mass.uk.net"];
[mailComposer setToRecipients:toRecipients];
mailComposer.mailComposeDelegate = self;
[self presentViewController:mailComposer animated:YES completion:nil];
}
else {
NSLog(#"This device cannot send email");
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult (MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultSent:
NSLog(#"You sent the email.");
break;
case MFMailComposeResultSaved:
NSLog(#"You saved a draft of this email");
break;
case MFMailComposeResultCancelled:
NSLog(#"You cancelled sending this email.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: An error occurred when trying to compose this email");
break;
default:
NSLog(#"An error occurred when trying to compose this email");
break;
}
[self dismissViewControllerAnimated:YES completion:NULL]; }
I am able to send the email.
But I am not able to set the setToRecipients method.
Can anyone please help me out with this?

Related

unable to send the mail using objective c?

i am very new to objective c.i am trying to send the email whenever the user clicks the send button.whenever i clicks the button the if condition is not satisfied and coming out of the loop also the email is not send.
This is my .h file
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface complaintsviewcontroller : UITableViewController<MFMailComposeViewControllerDelegate>
This is my .m file
- (IBAction)sendbutton:(id)sender {
if ([MFMailComposeViewController canSendMail])
{
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"testing ";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"noreply#etowns.in"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(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 localizedDescription]);
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
can anyone suggest me what is the mistake i done in the above coding ?
As per the standards you need to check MFMailComposeViewController is able to send your mail just before sending using below condition.
if ([MFMailComposeViewController canSendMail]){
// configure your email part here
// if not checking, this misleads the standards or leads uncertainty further
}
After that if you have not configured mail Mail Accounts under the settings -> mail, an alert appears like below.
Final Solution: If you got something like above popup just configure the mail account with gmail, yahoo or anyone, so the mailcomposer will be able to send email on behalf.

Send mail to an array list recipients from a database in ObjectiveC

I am using ObjectiveC and I want to send an email to a list of email addresses that I have in a sqlite database. The email array hold the addresses that I want to send the mail to.
I show you my code
- (void)sendEmailButtonClicked: (id)sender {
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:emailArray];
NSLog(#"What are the emais %#",toRecipents);
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setToRecipients:toRecipents];
[mc setMessageBody:messageBody isHTML:NO];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(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 localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
I obtain this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM mf_isLegalCommentedEmailAddress]: unrecognized selector sent to instance 0x78e5e180'
Anyone can help me please? Thank you so much!
First of all it should be:
NSArray *toRecipents = [NSArray arrayWithArray:emailArray];
But why not just do:
[mc setToRecipients:emailArray];
Assuming email array is a valid array object as we can't see from your code where/how you create it.
EDIT
You likely can't send an email from the simulator if there isn't an email account setup. Check for this with the following:
if ([MFMailComposeViewController canSendMail]) {
//Do your email stuff
}
else {
//Present an error etc ...
}
There's an issue with your array. This code works fine when sending to multiple recipients:
//Email
-(void)mailButton {
NSArray *emailArray = #[#"me#gmail.com", #"you#gmail.com", #"him#gmail.com", #"her#gmail.com", #"everyone#gmail.com"];
if ([MFMailComposeViewController canSendMail]) {
NSString *subject = #"Subject";
NSString *messageBody = [NSString stringWithFormat:#"Message Body"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:subject];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:emailArray];
[self presentViewController:mc animated:YES completion:NULL];
}
else {
UIAlertView *emailError = [[UIAlertView alloc] initWithTitle:#"Email Unavailable"
message:#"Sorry, were unable to find an email account on your device.\nPlease setup an account in your devices settings and try again."
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:nil];
[emailError show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
toRecipents should be an array, which contains only one email address.
As below,
toRecipents = #[#"foo#example.com"];
If, you want to send email some people, you should use setCcRecipients method.

Trouble sending email in iOS 7 using MFMailComposeViewController

I have a very simple app to test sending email, but the email never arrives. I have included the MessageUI framework in the app, and implemented the MFMailComposeViewControllerDelegate as well. The two methods in the app are as follows:
- (IBAction)showEmail:(id)sender
{
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"email#address.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
and the delegate method:
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(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 localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
When I press the email button in my app the first method works perfectly and when I click the send button that is presented, the "Mail Sent" message appears in the log. The email simply never arrives.
Everything seems to work as advertised, with the exception of the email never arriving at its destination.
I am running this on the iPad not in the simulator, and I have a good network connection.
What am I missing?
Well apparently this was an email configuration problem with the iPad itself. After a reboot of the device the above code works perfectly. I absolutely hate this kind of problem.
Use the canSendMail method.
Use the following code to check if a device mail configuration is setup or not. Otherwise your app will crash.
if ([MFMailComposeViewController canSendMail])
{
// Create and show the MailComposeViewController
}
else
{
// Show No mail account set up on device.
}

Changing views after sending an email

I'm having trouble thinking of ways to change views after hitting "send" when in email mode. I have a main view that is a form that the user fills out, that info then populates the email. Now when I hit "send" I don't want the user to go back to the form page but a new one.
Thoughts? Thanks!
Add MFMailComposeViewControllerDelegate to your view controller interface.
then make the current view controller the delegate when initializing the ``
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
you will be notified of the status of the mail in the method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
// message.text = #"Result: canceled";
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
{
UIAlertView *FailedAlert= [[UIAlertView alloc]initWithTitle:#"Mail could not be sent" message:nil delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[FailedAlert show];
[FailedAlert release];
break;
}
default:
NSLog(#"Hit default case in the switch");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Here you should be able to load a new view based on your choice and result.

ipad mail composer application to send a image to another address

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.