Sending email through iPhone application - iphone

I'd like to setup a button in the iPhone app I'm making to send an email with the results of some operations performed earlier in the app. I've read other people comment on here that you can use other frameworks but, frankly, I can't find one that actually works. I've heard MailCore is good. Has anyone been able to send an email seamlessly through an iPhone app?
Note: I do not want to exit the app to send the email.

Answer From this Question: How can I send mail from an iPhone application
On iPhone OS 3.0 and later you should use the MFMailComposeViewController class, and the MFMailComposeViewControllerDelegate protocol, that that tucked away in the MessageUI framework. Note you must link the Message UI Framework and import its headers.
#import <MessageUI/MFMailComposeViewController.h>
First to send a message:
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"My Subject"];
[controller setMessageBody:#"Hello there." isHTML:NO];
if (controller) [self presentModalViewController:controller animated:YES];
[controller release];
Then the user does the work and you get the delegate callback in time:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;{
if (result == MFMailComposeResultSent) {
NSLog(#"It's away!");
}
[self dismissModalViewControllerAnimated:YES];
}

In this case, your best bet is to use a service like Amazon Simple Email Service (SES). It will enable you to send the email without ever leaving the application (unlike the other solutions listed). Amazon AWS has an iOS SDK which can utilize the SES service. This service is super simple to use, and the cost is extremely low.
http://aws.amazon.com/sdkforios/

MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
NSString *emailBody = #"Write email body text here........";
[mailComposer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];

Related

MFMailComposeViewController in iphone app

hii every one
can we use " MFMailComposeViewController " with out using " presentModalViewController" , i mean i need to send email with out navigating to that mail composer page
i did a test project with following code
- (IBAction)buttonPressed {
arrRecipients = [[NSMutableArray alloc]init];
[arrRecipients addObject:#"xxxxxxx#gmail.com"];
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
[mailController setSubject:#"Ravikiran test mail"];
[mailController setToRecipients:arrRecipients];
[mailController setMessageBody:#"this is my test app" isHTML:YES];
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
it is sending mail but it is navigating to mail composer page & then its sending but i need to send mail just on click of the button
No, you cannot use MFMailComposeViewController to send an email without user interaction. There are probably 3rd parties libraries that let you do this, but the iOS SDK does not allow you to.
Also, you're leaking the arrRecipients array.
Best way for your needs is to have a web service which sends the data to an email id.
its always to good to inform the user in some way before sending the mail.
Thanks!

Email body not displayed after sending from iPhone via code

I have couple of iPhone apps that sends an email via code.
I've noticed quite a number of emails coming in from customers where everything is populated except for the body message (i always see their signature though).
I sent a test message, using the same app from my iPhone device, and i get the message (a simple "this is a test" message).
Wondering if there are any coding that needs to occur to handle different version of iOS, or perhaps different iPhone device hardwares?
Anybody noticed this before?
I would consider the possibility that the users are accidentally pressing send on a blank email. Perhaps to eliminate this as a possibility, guard against blank messages in code, then if it still happens you'll know it's a real issue.
set all delegates properly
and use this
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSString *sub= [NSString stringWithFormat:#"The Black Sheep App: %# Specials",[[BarSplArr objectAtIndex:0]objectForKey:#"barname"]];
NSString *message=[NSString stringWithFormat:#"Check out these specials for %# on %#, %# \n\n %#",[[BarSplArr objectAtIndex:0]objectForKey:#"barname"],day,date,[[BarSplArr objectAtIndex:0]objectForKey:#"drinkspecials"] ];
[controller setSubject:sub];
[controller setMessageBody:message isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];

iPhone MFMAilComposeViewControllerDelegate problem

I wrote a code that utilizes the MFMAilComposibleViewController to harness in-app emails. I specified the delegate as so under, but my delegate method isn't getting called. This is the first time I'm getting this type of error; I normally develop under the iPhone but for a client I need to develop under an iPad.
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setModalPresentationStyle: UIModalPresentationFormSheet];
[mailComposer setSubject: #"Hello World"];
[mailComposer setMailComposeDelegate: self];
[mailComposer setMessageBody: #"<font color='red'>Hello</font> <font color='blue'>World</font>" isHTML:YES];
[self presentModalViewController: mailComposer animated: YES];
This snippet is an exact copy-paste of the method that calls the mailview. Please note, I did specify in my header the MFMailViewControllerDelegate protocol.
Thank You!
Have you tested this in simulator? As i've experienced before, the mail composer doesn't work in the simulator but in the device. Try it.

Easy way to open the Mail application with an pre-defined message subject and body?

In my app the user generates text content. I want to enable the user to launch the Mail application, which then should contain a specified subject and message body. Like: You write a poem in my app and then want to send it to your new girlfriend. So you tap a mail icon and the Mail app opens, containing already an subject and message body with your poem inside.
Someone said there is a kind of URL mechanism for that?
You can use MFMailComposeViewController for sending the mail.
-(void)OpenMail{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"my new poem"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
emailBody is string which contains the poem.
All the best.
Apple URL Scheme Reference: Mail Links lists all your options.
Or you use MFMailComposeViewController.

Class to manage e-mail from iPhone

I'm working on an iPhone app that offers the user the opportunity to send an e-mail in 3 different places in the app, and for 3 different purposes.
Rather than put the same code for showing the e-mail composer in 3 different view controllers, shouldn't I develop a separate E-mail class, create an instance, and then set properties such as To, CC, BCC, Body, HTML_Or_Not, and so on?
Also, if I create an instance of such a class, and it brings up the e-mail composer, is it OK to release the class even before the e-mail composer has left the screen?
My advice, It's so easy to use the built in mail picker class, just stick with it, you can create a function to setup and show the picker, and use that when you need to like so:
- (void)showMailPicker {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
picker.navigationBar.barStyle = UIBarStyleBlack;
[picker setToRecipients: ...];
[picker setSubject:#"Title"];
// Fill out the email body text
NSString *emailBody = #"email Body...";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
Yes, it's safe to release the picker once you have presented it, and remember that once you present the picker, you can't change the email addresses, subject, body etc...
My aproach would me to encapsulate the whole process of creating, initialising, displaying, dismissing and handling all of the aspects of the mail composer in a class, like a "mail manager" or something.
Then you can create and instance and set the properties you need, and then call "show mail composer" or something to that effect.
I wouldn't recommend releasing this manager class if it will be dealing with the dismissal and handling of the result of the mail composer (ie, handling an error when sending) until after it has finished what it's doing. If you release it too early, you may not have any logical way to dismiss the mail composer or gracefully handle it's results etc.