I used MFMailComposeViewController for sending mail to others. When click a button the compose sheet is opening, and i can able to type To address,subject,message body. But after clicking send button the mail page is not closing.
code:
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"My image"];
UIImage *myImage = [UIImage imageNamed:#"mobiletuts-logo.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"Image"];
NSString *emailBody = #"Hi, my image";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:nil];
}
- (void)mailComposeController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Sorry. Forget to add these lines. After this lines mail working
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:nil];
}
Try this
[self presentModalViewController:mailer animated:YES];
for dismiss
[self dismissModalViewControllerAnimated:YES];
Related
i have to send html text to mail as an attachment with a pdf extension.
Code :
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
NSArray *toReceipents =[NSArray arrayWithObjects:#"", nil];
[mailViewController setToRecipients:toReceipents];
[mailViewController setSubject:strMailMessage];
NSLog(#"Mail Message:%# %#",strMailMessage,appDelegate.strShareText);
NSData* data = [appDelegate.strShareText dataUsingEncoding:NSUTF8StringEncoding];
[mailViewController setMessageBody:appDelegate.strShareText isHTML:YES];
[mailViewController addAttachmentData:data mimeType:#"application/pdf" fileName:#"Medication file.pdf"];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
Note:when i download the pdf file i am getting the same text.but i want to show the text in table format in pdf document
Good code but for HTML try this ;) :
- (IBAction)mailCompose:(id)sender {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Hello World!"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"e-mail here or leve empty", nil];
[mail setToRecipients:toRecipients];
NSString *emailBody = #"Body message App</b><br /><a href='https://itunes.apple.com/it/app/yourApp/id'>Download Free on AppStore!</a>";
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myVoice.caf"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
[mail addAttachmentData:myData mimeType:documentsDirectory fileName:#"myVoice.caf"];
[mail setMessageBody:emailBody isHTML:YES];
mail.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:mail animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Saved");
break;
case MFMailComposeResultFailed:
NSLog(#"Faild");
break;
case MFMailComposeResultSent:
NSLog(#"Sent");
break;
default:
NSLog(#"Default");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Enjy hope this can help you
I am sending email from iphone app and i want that email sent to should be the value or address the var emailTO has should be in TO automatically.
NSString*emailTO=#"ali#yahoo.com";
[picker setRecipients:[NSArray arrayWithObject:emailTO]];
but it does not work any idea how to implement in this way thanks
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:[NSArray arrayWithObject:[NSString stringWithFormat:#"%#,#"ur Receipent data"]]]];
Try to use this one...
NSArray *array = [NSArray arrayWithObject:#"ali#yahoo.com"];
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setToRecipients:array];
[self presentViewController:mailViewController animated:YES completion:NULL];
}
else
{
NSLog(#"Device is unable to send email in its current state.");
}
Try This. Hope will Help You.Note that you will need to add the MessageUI framework to your app.
#import <MessageUI/MessageUI.h>
and
MFMailComposeViewController *mail = [[[MFMailComposeViewController alloc] init] autorelease];
mail.mailComposeDelegate = self;
[mail setToRecipients:[NSArray arrayWithObject:#"email#gmail.com"]];
[mail setSubject:#"Set Your Subject Here"];
[self presentModalViewController:mail animated:YES];
Use MFMailComposeViewControllerDelegate protocol: Delegate Function for conform the result
- (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;
}
}
Description
setToRecipients:
Sets the initial recipients to include in the email’s “To” field.
- (void)setToRecipients:(NSArray*)toRecipients
Parameters
toRecipients
An array of NSString objects, each of which contains the email address of a single recipient.
try this
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[picker setRecipients:[NSArray arrayWithObject:#"your email address"]];
[self presentViewController:mailViewController animated:YES completion:nil];
If you want to open iPhone Email application, then use below code.So, you have not any need to add MFMailComposeViewController.
NSString *subject = #"";
NSString *body = #"";
NSString *address = #"test#gmail.com";
NSString *cc = #"";
NSString *path = [NSString stringWithFormat:#"mailto:%#?cc=%#&subject=%#&body=%#", address, cc, subject, body];
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url];
You should use
[picker setRecipients:[NSArray arrayWithObjects:emailTO,nil]];
I'm testing on an iPod Touch running OS 3.1.3
Trying to allow users to send an email from within the app - but when the following code is executed, the entire screen just turns completely blank / white.
Any ideas on why this is happening?
I've got the MessageUI framework in the project.
I'm importing and delegating in the header file:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
<MFMailComposeViewControllerDelegate>
And here's the code, pretty standard:
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"App Feedback"];
[picker setToRecipients:[NSArray arrayWithObject:#"xyz#gmail.com"]];
[self presentModalViewController:picker animated:YES];
[picker release];
}
And then I have the didFinishWithResult function that would dismiss the ModalViewController when the email has been sent.
But again, all I get is a blank white screen on my iPod Touch. =/
Thanks!
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setSubject:#"App Feedback"];
[mail setMessageBody:#"*your message content*" isHTML:NO];
[self presentModalViewController:mail animated:YES];
[mail release];
}
- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}
You can take look at sample code from apple:
http://developer.apple.com/library/ios/#samplecode/MessageComposer/Listings/Classes_MessageComposerViewController_m.html
-(IBAction)showMailPicker:(id)sender {
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil) {
[self displayMailComposerSheet];
if ([mailClass canSendMail]) {
[self displayMailComposerSheet];
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send mail.";
}
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send mail.";
}
}
-(void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from California!"];
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
NSString *path = [[NSBundle mainBundle] pathForResource:#"rainy" ofType:#"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"image/jpeg" fileName:#"rainy"];
NSString *emailBody = #"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
feedbackMsg.text = #"Result: Mail sending canceled";
break;
case MFMailComposeResultSaved:
feedbackMsg.text = #"Result: Mail saved";
break;
case MFMailComposeResultSent:
feedbackMsg.text = #"Result: Mail sent";
break;
case MFMailComposeResultFailed:
feedbackMsg.text = #"Result: Mail sending failed";
break;
default:
feedbackMsg.text = #"Result: Mail not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
I am try to send mail from my app. I want to type(dynamically) recipient id,ccid,sub and message.
Thanks
Senthilkumar
on iOS 3+
Import #import in your view controller header.
Then:
-(void)showMailPanel {
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
// only on iOS < 3
//if ([MFMailComposeViewController canSendMail] == NO)
// [self launchMailApp]; // you need to
mailComposeViewController.mailComposeDelegate = self;
[mailComposeViewController setToRecipients:[NSArray arrayWithObjects:#"email address1",#"email address 2",nil]];
[mailComposeViewController setSubject:#"your subject"];
[mailComposeViewController setMessageBody:#"your body" isHTML:YES];
mailComposeViewController.delegate = self;
[self.navigationController presentModalViewController:mailComposeViewController animated:YES];
[mailComposeViewController release];
}
- (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;
}
[controller dismissModalViewControllerAnimated:YES];
}
-(void)launchMailApp {
{
NSString *recipients = #"dest_email";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, subject];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
You need things like this:
//Import this in your .h file
#import <MessageUI/MessageUI.h>
...
MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease];
mailController.mailComposeDelegate = self;
[mailController setSubject:[NSString stringWithFormat:#"Report a problem - #%#", yourUserID]];
[mailController setToRecipients:[NSArray arrayWithObject:#"support#yourWebsite.com"]];
[self presentModalViewController:mailController animated:YES];
Besides, the parent view controller (the delegate) needs to conform to the MFMailComposeViewControllerDelegate protocol:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
//Extra implementation here...
[self dismissModalViewControllerAnimated:YES];
}
i have the following code presenting the user with email from within the app :
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Subject"];
NSString* link = [NSString stringWithFormat:#"<a href='%#'>Report</a>",link];
[picker setMessageBody:link isHTML:YES];
picker.navigationBar.barStyle = UIBarStyleDefault;
[self presentModalViewController:picker animated:YES];
[picker release];
and allso :
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
[self dismissModalViewControllerAnimated:YES];
}
everything works just fine , expect that i'd like the cancel to skip the delete draft/save draft/cancel stage and basicly chose the delete draft (not present it to the user)
can this be done ?
thanks
- (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;
}
//Dismiss the mailViewController.
[self dismissModalViewControllerAnimated:YES];
}
Is this what you are looking for? You can then handle each error or completion individually!