Cannot send email in app using MFMailComposeViewController - ios

I have an app which uses MFMailComposeViewController to send documents through email. I read somewhere that I need to enable at least one email so that the method [MFMailComposeViewController canSendEmail] will return YES and email the document. However, whenever I tap the email button, all it does is return to the previous view.
I checked the code and [MFMailComposeViewController canSendEmail] returns NO. Can anyone tell me why is this happening?
Here is the code:
- (void)sendEmail
{
if ([MFMailComposeViewController canSendMail] == NO) return;
NSURL *fileURL = document.fileURL; NSString *fileName = document.fileName;
NSData *attachment = [NSData dataWithContentsOfURL:fileURL options:(NSDataReadingMapped|NSDataReadingUncached) error:nil];
if (attachment != nil)
{
MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];
[mailComposer addAttachmentData:attachment mimeType:#"application/pdf" fileName:fileName];
[mailComposer setSubject:fileName];
mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;
mailComposer.mailComposeDelegate = self;
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
}
}

First add and Import the MessageUI Framework
#import <MessageUI/MessageUI.h>
and Declare the MFMaileComposeViewControllerDelegate
#interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>
Write this code for sending the mail
- (IBAction)openMail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"xyz"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"fisrtMail#example.com", #"secondMail#example.com", nil];
[mailer setToRecipients:toRecipients];
NSData *pdfdata = [NSData dataWithContentsOfURL:"Your URL"]
[mailController addAttachmentData:pdfData
mimeType:#"application/pdf"
fileName:#"file.pdf"];
NSString *emailBody = #"xyz";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
and also write the delegate method of MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(#"Mail not sent.");
break;
}
// Remove the mail view
[self dismissModalViewControllerAnimated:YES];
}

because your mail app in iphone not auth. Go to preferences -> Mail (or simply open Mail app) and auth from google or another service and you can sent emails through MFMailComposeViewController. (this in on real iPhone - I don't try it on simulator)

Related

MFMailComposeViewController delegate not called

I'm not able to call Mail composer delegate method even if I'm writing below code:
-(void)openMailComposerInViewController:(UIViewController *)hostController{
if ([MFMailComposeViewController canSendMail])
{
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"Test Email";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"support#test.com"];
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:emailTitle];
[mailComposer setMessageBody:messageBody isHTML:NO];
[mailComposer setToRecipients:toRecipents];
// Present mail view controller on screen
[hostController presentViewController:mailComposer animated:YES completion:NULL];
mailController = hostController;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
pragma mark - Mail
- (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
[mailController dismissViewControllerAnimated:YES completion:NULL];
}
I do not want to call mail composer on self
However, my delegate is not getting called. Please help. The application crashed on clicking on Cancel or Send mail button
I experienced this problem, and my problem was I had the following code:
self.mailComposer.mailComposeDelegate = self
self.mailComposer = MFMailComposeViewController()
I was setting the delegate before I initialized the mailComposer object. I just switched those 2 lines to this and it worked:
self.mailComposer = MFMailComposeViewController()
self.mailComposer.mailComposeDelegate = self
Because of ARC, I was losing the viewcontroller in which the delegate was written so it was not gettting called. Below code helped me fix the issue:
+(MailComposer *)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMailComposer = [[MailComposer alloc]init];
});
return sharedMailComposer;
}
Then I used the above instance while calling the MailComposerViewController
[[MailComposer sharedInstance] openMailComposerInViewController:[(UINavigationController *)moduleRouter.container topViewController]];
It Worked :)
your class instance must be same for setMailComposeDelegate and presentViewController.
mailComposer.mailComposeDelegate = hostController;
[hostController presentViewController:mailComposer animated:YES completion:nil];

How to set MFMailComposeViewController From field rather than default mail app

How to set MFMailComposeViewController From field rather than default mail from field. I am constructing one app which has different email id sign up I want to sync those ids and send mail from those accounts.
I don't think you can change From, since MFMailComposeViewController is used to send mail with mail accounts registered in iOS's settings, not the mail address you used to login your app.
try this code
- (IBAction)openMail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"A Message from MobileTuts+"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"fisrtMail#example.com", #"secondMail#example.com", nil];
[mailer setToRecipients:toRecipients];
UIImage *myImage = [UIImage imageNamed:#"mobiletuts-logo.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"mobiletutsImage"];
NSString *emailBody = #"Have you seen the MobileTuts+ web site?";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
for check result
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(#"Mail not sent.");
break;
}
// Remove the mail view
[self dismissModalViewControllerAnimated:YES];
}

How to attach a PDF file to mail and send mail in iOS

Currently I am developing an expense management app and I want to attach a PDF file to the mail and send it to the specified email Address. So guide me from where should I start this and how to develop it.
Thanks.
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
[vc setSubject:#"Monthly Expense Report"];
[vc addAttachmentData:pdfData mimeType:#"application/pdf" fileName:[NSString stringWithFormat:#"%#.pdf",[arrOfExpenseDate objectAtIndex:1]]];
if ([MFMailComposeViewController canSendMail])
{
[self presentViewController:vc animated:YES completion:nil];
}
I have created a example for you on how to send an pdf attachment using MFMailComposeViewController. Following is the code :
#interface ViewController () <MFMailComposeViewControllerDelegate>
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)compseEmail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
[vc setSubject:#"Monthly Expense Report"];
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"ApacheInstallationSteps" ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[vc addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"ApacheInstallationSteps.pdf"];
[self presentViewController:vc animated:YES completion:nil];
}
else
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert!!!" message:#"Your device can't send emails." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
- (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 dismissViewControllerAnimated:YES completion:nil];
}
#end
Make sure that you add MessageUI.framework. Following is the output.
To send a pdf or any other attahcment in mail using MFMailComposeViewController, you have to use:
[mc addAttachmentData:fileData mimeType:mimeType fileName:filename];
For more details, follow this tutorial.
You can also download the sample code given in this tutorial for better understanding.
Hope that helps.

how to send an email from within the app

I'm trying to send an email with data from text field and image and it doesn't work, please advise. Here is my code:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)
{
Cocktails*c=[[Cocktails alloc]init];
_arrTextField=[NSArray arrayWithObjects:_txtName,_txtIngredients,_txtPreper,_txtServe,_txtFrom ,nil];
NSLog(#"send email");
if ([MFMailComposeViewController canSendMail])
{
NSMutableArray*recipients=[[NSMutableArray alloc]init];
[recipients addObject:#"maya1580#gmail.com"];
MFMailComposeViewController *controller= [[MFMailComposeViewController alloc]init];
controller.mailComposeDelegate= self;
[controller addAttachmentData:_imgDrink mimeType:#"image/png" fileName:#"Myimage"];
[controller setSubject:#"my cocktail"];
[controller setMessageBody: _arrTextField isHTML:NO];
[controller setToRecipients:recipients];
}
else
{
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:#"Your devise is not set up for Email" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:Nil, nil];
[alert show];
// [alert release];
}
You never present the mail controller. It needs to be presented like any other modal view controller.
Here that should help:
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(#"Mail not sent.");
break;
}
// Remove the mail view
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction)sendCode:(id)sender {
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Your subject"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"maya1580#gmail.com", nil];
[mailer setToRecipients:toRecipients];
UIImage *myImage = [UIImage imageNamed:#"MyImage.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"myimag
e"];
NSString *emailBody =
#"your body";
[mailer setMessageBody:emailBody isHTML:NO];
[mailer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:mailer animated:YES];
}
else {
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Your device doesnt support that action."
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert2 show];
}
}
}
and in the .h file add:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface Controller : UIViewController <MFMailComposeViewControllerDelegate> {
}
-(IBAction)send:(id)sender;
#end
You cant send automaticaly email because apple wont let you do this kind of things without user knowledge. You must present presentModalView!

Sending eMail to self using iOS.

I have worked with MFMailComposeViewController via messageUIFramework. I have a case where I have to send email to self. How do i add the "from" address MFMailComposeViewController delegate? any suggestions?
How to send an email in-APP ?
- (IBAction)sendMail:(id)sender {
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Message Pro"];
//Destination adress
NSArray *toRecipients = [NSArray arrayWithObjects:#"your adress", nil];
[mailer setToRecipients:toRecipients];
//Attachement Object
UIImage *myImage = [UIImage imageNamed:#"image.jpeg"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"mobiletutsImage"];
//Message Body
NSString *emailBody = #"message body";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(#"Mail not sent.");
break;
}
// Remove the mail view
[self dismissModalViewControllerAnimated:YES];
}
Don't forgot
to add a button with IBAction = sendMail
to import MessageUI.framework
add delegate in your .h file MFMailComposeViewControllerDelegate
The "from" address is specified for you by the MFMailComposeViewController (and the user can override to be any of their configured mail accounts when they get the compose mail view). You, the programmer, don't have to worry about getting/setting the "from" address.

Resources