MFMailComposeViewController does not send mail - iphone

I am trying to send mail using MFMailComposeViewController. Everything works, except that mails do not get sent, and I always get MFMailComposeResultFailed.
Any pointers? I am NOT using the simulator, and sending mail does work from my device. I do have a connection (testing via Reachability), and [MFMailComposeViewController canSendMail] returns YES.
No compiler warnings in the project, no crashes...

It was a bug in IOS4.
I had both an Exchange mail account and an old, inactive IMAP account on my phone. Apparently, that leads to problems with iOS4. The mails actually were stuck in the outbox. Once I removed the inactive IMAP account, everything worked as expected.

Some readers might be facing this problem:
Make sure you implement the <MFMailComposeViewControllerDelegate> protocol
Here's the code:
// in TestViewController.h
#interface TestViewController : UIViewController<MFMailComposeViewControllerDelegate>
#end
// in TestViewController.m
#interface TestViewController ()
#end
#implementation
- (void) compose {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello there"];
[picker setToRecipients:#[]];
// Fill out the email body text
NSString *emailBody = #"Hello, sending a message from my app";
[picker setMessageBody:emailBody isHTML:NO];
// use this function. presentModalViewController:... is deprecated
[self presentViewController:picker animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end

It's difficult to tell without seeing a code snippet, however you should check the following:
1) you have correctly set the MFMailComposeViewController's delegate and implemented its delegate methods;
2) you have set the mail subject using setSubject:
3) you have set the message body using setMessageBody:isHTML:
and optionally set an attach using addAttachmentData:mimeType:fileName:
4) you have presented to the user your mail compose view controller using something like
[self presentModalViewController:mcvc animated:YES];
Hope this helps.

Related

iPhone In App Email Issue

when I press the sendMail button, it will go to the mail button, but when I hit send or cancel it will not take me back to my application. Any suggestions?
-(IBAction)sendMail {
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease] ;
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:nil];
[mailComposer setSubject:nil];
[mailComposer setMessageBody:#"Default text" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
}
}
You need to set a delegate (generally the same view controller that presented the MFMailComposeViewController). Then when the user taps the Save or Cancel button the MFMailComposeViewController will call -mailComposeController:didFinishWithResult:error on the delegate. So set yourself as the delegate and define the following method:
#pragma mark -
#pragma mark MessageUI Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error {
[controller dismissModalViewControllerAnimated:YES];
}
Add the following line below your mail composer initialization
mailComposer.mailComposeDelegate = self;//very important if you want feedbacks on what the user did with your email sheet.
Then implement the delegate method as Kenny suggested. You can use this method to take custom actions.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
{
//Do something, If you need to
}
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Remember to conform to the delegate by adding
#interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate> { }
If you are still having trouble, You may visit the following tutorial where everything nicely explained:
http://blog.mugunthkumar.com/coding/iphone-tutorial-in-app-email/
This wonderful line:
mailComposer.mailComposeDelegate = self;
is what made ​​it happen for days without knowing that was what went wrong.
And do not forget them:
# import <UIKit/UIKit.h>
# import <MessageUI/MessageUI.h>
# import <MessageUI/MFMailComposeViewController.h>
In addition to importing the MessageUI.framework in the project.
Verified in IOS5

how can i send mail with out composer splash screen

i need to send mail in my application.
for that my code is,
MFMailComposeViewController * mailView = [[MFMailComposeViewController alloc] init];
[mailView setMailComposeDelegate:self];
[mailView setSubject:#"Some Subject"];
[mailView setToRecipients:self.selectedArray];
[self presentModalViewController:mailView animated:YES];
[mailView release];
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[controller dismissModalViewControllerAnimated:YES];
}
for that i am getting the splash screen like this.
is it possible to send mail without this splash screen.
i need to send mail in background.
can any one please suggest me.
Thank u in advance.
if you don't want to use the composer you have to write your own smtp client.
I've never tried it but from what I've heard skpsmtpmessage should do the trick.

Mail comes Without Message content using MFMailComposeViewController in iPhone

I have faced some problems, when the user sent the mail. Some of the time the mail comes without message content(Email body), even the user typed the message content.The message content doesn't displayed only some times.(For 10 mail comes into my inbox, 2 or 3 messages comes without content in the email body) So please guide me why its happening?
Here my code is,
- (void)viewDidLoad {
[self displayComposerSheet];
}
-(void) displayComposerSheet
{
picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[picker setToRecipients:[NSArray arrayWithObjects:#"aaa#bbb.com",nil]];
[picker setSubject:#"ShoutOuts"];
}
[self presentModalViewController:picker animated:YES];
}
Please Help me Out.
Thanks!
I don't have a straight answer, but two hints:
a) Why are you calling displayComposerSheet in viewDidLoad? I'd rather put it into viewWillAppear, since the view controller might already be loaded and in memory if used once again.
b) Did you try pre-filling the mail body with a placeholder text to see if that is being sent?

iPhone: How to Close MFMailComposeViewController?

I'm having difficulties closing an email message that I have raised.
The email opens nicely, but once it is opened it will not close as the mailComposeController:mailer didFinishWithResult:result error:error handler never gets invoked.
As far as I can tell I have all the bits in place to be able to do this.
Anyone any ideas of what I can look at?
Here is how I raise the email:
-(IBAction)emailButtonPressed
{
NSString *text = #"My Email Text";
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.delegate = self;
[mailer setSubject:#"Note"];
[mailer setMessageBody:text isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
and later in the class I have this code to handle the close (but it never gets called):
-(void)mailComposeController:(MFMailComposeViewController *)mailer didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self becomeFirstResponder];
[mailer dismissModalViewControllerAnimated:YES];
}
My header file is defined as:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface myViewController : UIViewController <UIActionSheetDelegate, UIAlertViewDelegate, MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>
Thanks
Iphaaw
You are setting the delegate wrong, the delegate property in MFMailComposeViewController is called mailComposeDelegate, so it should be:
mailer.mailComposeDelegate = self;
Another possible error I can see is calling dismissModalViewControllerAnimated: on mailer - you should send this message to the view controller who presented the mail interface - self in this case:
[self dismissModalViewControllerAnimated:YES];
I wrote "possible error" because it might actually work if iOS routes the message through responder chain, anyway - the documentation says it should be send to presenter.

Mail send framework for iphone

I want to send emails with attachments from my iphone application with custom UI. What can i use for this?
UPD: maybe it's possible to use some smtp library for this task? What can you advice?
you need to do the following
first add a framework by right clicking on project.
Add -> Existing Framework ->
library/frameworks/MessageUI.framework
then
in ViewController.h file
#import <MessageUI/MessageUI.h>
#interface ViewController : UIViewController <UITextFieldDelegate, MFMailComposeViewControllerDelegate>{
//....yor variables
}
ViewController.m file
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Sample Email Application"; // title of navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(composeMail:)] autorelease]; // for adding a compose button
//in navigation bar.
//...your code
}
-(void) composeMail: (id) sender{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate = self;
[[picker navigationBar] setTintColor:[UIColor blackColor]];
[picker setSubject:#"Sample Email Application"];
[picker setMessageBody:[NSString stringWithFormat:#"Visit for more help %#. ",#"http://google.com"] isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[controller dismissModalViewControllerAnimated:YES];
}
You will need to compile in your own SMTP server, there are a few online that work. This is a pile of hurt. Just use the iPhones Message Composer which is standard. Unless you are building a email spam client this wont really work.
If you want to send email without using the native mail composer UI and without setting up your own SMTP server, you can check out PostageApp (http://postageapp.com/). There's an iOS / Mac API wrapper that lets you send email through the API. https://github.com/postageapp/postageapp-objc
(Disclosure: I work for PostageApp and developed the plugin.)
The open source three20 framework has designed it's own Mail capabilities via TTMessageController which mimics the original Mail app..You can use that as a starting point and then simply modify the UI of it to your needs.
E-mailing attachments is another story though...
More information: http://www.three20.info/overview