Email app from another app - iphone

I want to start an email app from app on button pressed. But when I pressed the button nothing is happening !!!
code :
- (IBAction) startMail
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"mailto:emailAdress?subject=testMail&body=its test mail."]];
}
any thing wrong in code ? Also button is properly set in IB.
Thanks..

If you want to compose an EMail inside your app, you should have a look at the MFMailComposeViewController reference to do so instead of calling a mailto: URL scheme.

Using the mailto URL won't work in the simulator as mail.app isn't installed on the simulator. It does work on device though.

You can use "MFMailComposeViewController" to send mail from your application.
Sample code-
MFMailComposeViewController *picker;
picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *objSubject = [[NSString alloc] init];<br/>
NSString *emailBody = [[NSString alloc] init];
[picker setSubject:objSubject];
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
You must use the following delegate to check the status of mail sent or not
**-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
}**
Enjoy

Try this:
NSString *mailString = [NSString stringWithFormat:#"mailto:?to=%#&subject=%#&body=%#",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
Here to, subject and body are NSString variables

Check this out, it's a better solution than directing your user out of the app;
How can I send mail from an iPhone application

NSString *url = [NSString stringWithString: #"mailto:foo#example.com?cc=bar#example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

Related

how to send MMS from iPhone app

In my new iOS Project I'd like the end user to be able to MMS text and/or images(from TextField) in a UIButton Action . I've seen similar apps that has this functionality (with text, haven't seen one with images yet).
I have search in google but could not find how to do this, any help much appreciated
This will work fine
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.persistent = YES;
pasteboard.image = [UIImage imageNamed:#"PDF_File.png"];
NSString *phoneToCall = #"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
if([MFMessageComposeViewController canSendText]) {
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:#"Your Email Body"];
picker.messageComposeDelegate = self;
picker.recipients = [NSArray arrayWithObject:#"123456789"];
[picker setBody:emailBody];// your recipient number or self for testing
picker.body = emailBody;
NSLog(#"Picker -- %#",picker.body);
[self presentModalViewController:picker animated:YES];
NSLog(#"SMS fired");
}

iOS 5 simplest way to send email from an app

I just tried to send email using this code and it seems to work:
NSString* urlEmail = [NSString stringWithString: #"mailto:foo#example.com?cc=bar#example.com&subject=test%20from%20me!&body=this%20is%20a%20test!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlEmail]];
The only problem is, is there a function I can use to automatically escape everything in a normal NSString so I don't have to write it out manually like this? If not, how can I use stringWithFormat without conflicting with the % signs already in the string? I basically want to be able to append to, subject, body, etc. dynamically.
Use MFMailComposeViewController:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:recipients];
[picker setSubject:subject];
[picker setMessageBody:body isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
According to https://stackoverflow.com/a/917630/535632, you can escape like this:
NSString *urlStr = [urlEmail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Mailbox programming in iphone?

I want saved email addresses of different users, and I want to open mailBox's new mail when I click the email, same as phonebook email in iphone,
what should I to do ???
You should use the MFMailComposeViewController class, and the implement MFMailComposeViewControllerDelegate protocol,
First to send a message:
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"My Subject"];
[controller setMessageBody:#"Hello there." isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
Once you sent you will get delegate callback in mailComposeController :
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(#"It's gone!");
}
[self dismissModalViewControllerAnimated:YES];
}
Create new message within Mail:
NSString *subject = #"The subject";
NSString *body = #"The message";
NSString *address = #"mail#address.com";
NSString *cc = #"mail#address.com";
NSString *path = [NSString stringWithFormat:#"mailto:%#?cc=%#&subject=%#&body=%#", address, cc, subject, body];
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];

iphone - start an application at the end of a phone call

Is it possible to launch an application at the end of phone call or after sending SMS?
NO this is not at all possible. Because you cant get the event when the call is ended or message is sent. So theres no way you can open up the application.
Happy Coding...
I got this code from Apple site and it works perfectly:
- (IBAction) dialNumber:(id)sender{
NSString *aPhoneNo = [#"tel://" stringByAppendingString:[itsPhoneNoArray objectAtIndex:[sender tag]]] ;
NSURL *url= [NSURL URLWithString:aPhoneNo];
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
if ([osVersion floatValue] >= 3.1) {
UIWebView *webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
webview.hidden = YES;
// Assume we are in a view controller and have access to self.view
[self.view addSubview:webview];
[webview release];
}
else {
// On 3.0 and below, dial as usual
[[UIApplication sharedApplication] openURL: url];
}
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:aPhoneNo]];
}
Looks like it might help ..
iPhone SDK: Launching an app after call ends

Sending email in iPhone

In my application I have to send feedback to the client's email.
Here is my code,
-(void) send:(id) sender {
[self sendEmailTo:[txtTo text] withSubject:[txtSubject text] withBody:[txtBody text]];
}
-(void) sendEmailTo:(NSString *)to withSubject:(NSString *) subject withBody:(NSString*)body {
NSString *mailString = [NSString stringWithFormat:#"mailto:?to=%#&subject=%#body=%#",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
}
But this does not work. Is it require any type of SMTP setting or something else?
I tried apple's MailComposer sample but it also does not work.
Please help me.
I have posted this here, but for the sake of not clicking again here it is:
You need to include the MessageUI.framework into your project, and inside your header file you need to set the delegate:
#import <MessageUI/MessageUI.h>
#interface RootViewController : UIViewController <MFMailComposeViewControllerDelegate> {
MFMailComposeViewController *email;
}
#property (nonatomic, retain) MFMailComposeViewController *email;
Once you do that, you have a few delegate methods inside your implementation file you need to include (You should check to see the result, but I am trying to keep as little code as needed):
#synthesize email;
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[email dismissModalViewControllerAnimated:YES];
}
Wherever you want to use this, you need to initialize and set it up like this:
email = [[MFMailComposeViewController alloc] init];
email.mailComposeDelegate = self;
// Subject
[email setSubject:#"Testing"];
// Optional Attachments
NSData *artwork = UIImagePNGRepresentation([UIImage imageNamed:#"albumart.png"]);
[email addAttachmentData:artwork mimeType:#"image/png" fileName:#"albumart.png"];
// Body
[email setMessageBody:#"This is the body"];
// Present it
[self presentModalViewController:email animated:YES];
Try to use different format:
NSString *mailString = [NSString stringWithFormat:#"mailto:%#?subject=%#&body=%#",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];