I want to send the email from my iOS device programtically using iOS.I am using the below code to send the email but i don't know where to enter from field.From which email id the mail will be sent?
Code for Sending email in iOS
// Email Subject
if(![MFMailComposeViewController canSendMail])
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device doesn't support Email!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"support#appcoda.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
Just to add, one other thing you can't do using the built-in MFMailComposeViewController class is to send emails silently, without the full-screen "Compose email" view appearing.
In my app, I resorted to sending the email details (To address, Body text, etc) to my app's WCF web service, and getting that to send the email instead.
And, of course, doing it that way, you can choose which From address you wish to use.
You cannot actually set the From field when using the MFMailComposeViewController. What actually happens is the From field will default to whatever email account the user has specified to be used as the default email account on the device in the settings.
Related
I am new in iOS programming, I know this is too old question but I am confused here about message controller. I want to make a application in which I want to send simple messages.
if I set multiple recipients can any of one can you view all recipients?
if it so,then how can I make a private message for all so that one recipient can't view other recipients?
Here is my code for composing a message for more recipients
- (void)showSMS:(NSString*)file {
if(![MFMessageComposeViewController canSendText]) {
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
return;
}
NSArray *recipents = #[#"12345678", #"72345524"];
NSString *message = [NSString stringWithFormat:#"Just sent the %# file to your email. Please check!", file];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipents];
[messageController setBody:message];
[self presentViewController:messageController animated:YES completion:nil];
}
There's a setting in Settings -> Messages which disables group messaging, and all recipients will receive the messages separately. Other than that, you can't do it with MFMessageComposeViewController because there's no bcc option. Your could send the messages one at a time though
After spending some time looking at similar questions on stack overflow, unfortunately none seemed to resolve my issue.
I'm using a UIActionSheet, when the user clicks send email on the action sheet, the following code is called:
if ([MFMailComposeViewController canSendMail]){
NSString *emailTitle = #"bla bla bla";
// Email Content
NSString *messageBody = #"bla bla bla";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"test#test.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[self.view.window.rootViewController presentViewController:mc animated:YES completion:NULL];
NSLog(#"email view opened");
}
else{
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert addButtonWithTitle:#"Cancel"];
[anAlert show];
}
MFMailComposeViewController opens on my iPhone in portrait mode, but when opening the app in landscape mode on an iPad, nothing happens. No view pops up, nothing at all.
It shows "email view opened" in the NSLog but nothing else.
Thoughts?
Edit The app is portrait ONLY on iPhone, and any orientation for iPad
EDIT AGAIN
After playing around a bit more, if I assign the code to a button, it works fine, only when selecting email from the action sheet is this problem present
Found the answer, this is it :
dispatch_async(dispatch_get_main_queue(), ^ {
// INSERT EMAIL CODE HERE
});
Hello friends I have used MFMessageComposeViewController to send message for specific mobile number. For that i have used following code:
if(phoneNumber)
{
if([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.body = #"Hello";
controller.recipients = [NSArray arrayWithObjects:#"+9876543210", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
Here in recipients array i had set one mobile number. Can i set their name in place of the number? Due to privacy reason i don't want to show the mobile number there.
Is it possible? If yes then how can we implement that.
Thanks in advance.
I'm building an app that allows the user to export en import data files and send them by e-mail.
So I've created a data file type with extension ".myAppExtension".
At first time everythings goes well. I can't export and send an e-mail. And when I open the e-mail, the method does work.
-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if (url != nil && [url isFileURL]) {
NSLog(#"%#",url);
NSLog(#"%#",[url pathExtension]);
if([[[url pathExtension] lowercaseString] isEqualToString:[#"myAppExtension" lowercaseString]]){
//Deal with received file
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Export ok" message:[NSString stringWithFormat:#"This file has been added : %#",[url lastPathComponent]] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil,nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Export failed" message:[NSString stringWithFormat:#"This extention is not supported : %#",[url pathExtension]] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil,nil];
[alert show];
}
}
return YES;
}
My issue is that when I want to export an other type of file with extension "otherExtension". I did not create data type for this extension in my app.
So I export and send an e-mail with this second type of file. The file name showed in e-mail "file.otherExtension". But, this is the issue, when I tap this mail attachement the e-mail app offers me to open it in my application. That's not what I want and, as I said, I did not create the data type for "otherExtension".
Edit : This is how I created the file type in myApp-info.plist :
If someone is interested, the issue were from the send e-mail fonction.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init] ;
[picker setSubject:mailSubject];
[picker addAttachmentData:codedData mimeType:#"application/myApp" fileName:[filePath lastPathComponent]];
[picker setToRecipients:[NSArray array]];
[picker setMessageBody:mailBody isHTML:NO];
[picker setMailComposeDelegate:self];
[currentMainViewController presentViewController:picker animated:YES completion:nil];
By adding the mime type to mail attachment the receiving application was using it to read the file. Replacing the mime type by "nil" solved my issue.
[picker addAttachmentData:codedData mimeType:nil fileName:[filePath lastPathComponent]];
I am sending email via following method:
-(void) sendEmailOpenControllerWithSubject:(NSString *)subject messsageBody:(NSString *) message
{
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:subject];
[controller setMessageBody:message isHTML:NO];
[controller setToRecipients:[[NSArray alloc] initWithObjects:currentProspect.email, nil]];
if (controller) [self presentModalViewController:controller animated:YES];
}
I am setting message body subject and recipient but it is likely that user changes these attribute in MailComposer.
What I need:
So I want to get the contents like message body,subject and recipients after the email is sent. As it is possible that user has changed these via mail composer.
Starting in iOS 5, you can register to be notified of changes to the availability of text message sending.
A userInfo dictionary key for the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification
NSString *const MFMessageComposeViewControllerTextMessageAvailabilityKey;
Refer more on Message UI Framework here