Xcode 4 / iOS - Send an email using SMTP from inside my app - ios

I've been looking around for a framework to simply allow me to send an email from inside my app. I have tried MailCore, Pantomime and SKPSMTP all with no luck. I can't get them to compile in Xcode, so I presumed they were outdated. Is there any way I can do this? If so, how? Thanks.

You can easily send emails from your iOS device. No need to implement SMTP and all. Best thing about using inbuilt emailing facilities in iOS is it gives you access to the address book! So it auto-completes names, email addresses. Yaaiiii!!
Include, AddressBook,AddressBookUI and MessageUI frameworks and code something like this. Note you can even choose to send content as HTML too!
#import <MessageUI/MessageUI.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
MFMailComposeViewController *mailComposer;
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
[mailComposer setSubject:#"your custom subject"];
[mailComposer setMessageBody:#"your custom body content" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
For the sake of completeness, I have to write this selector to dismiss the email window if the user presses cancel or send -
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{
if(error) NSLog(#"ERROR - mailComposeController: %#", [error localizedDescription]);
[self dismissModalViewControllerAnimated:YES];
return;
}
Happy coding...

It should be noted that MFMailComposeViewController has a method called canSendMail. If you don't check this before presenting a MFMailComposeViewController on a device that doesn't have an email account, you'll get a SIGABRT.
It's easy to miss this when testing on the device or the simulator since you'll probably have an email account on your Mac and your iPad.

SKPSMTPMessage still works fine for sending emails without the need for a UI
.
Make sure you add a reference to the CFNetwork.framework in your project. Otherwise you will get build errors.

I would imagine the Apple Approved way of doing this would be to send the data to a server via HTTP Post, and have the server generate the mail for you. I've seen others asking similar questions to this, and the answer is that if you send it from the device, you really need to prompt the user.
I can even tell you why this is: Imagine an application that could send itself to everyone in your address book without the your confirmation, telling them that you just installed application X, and they should too. Even if well intentioned, this could quickly create a huge SMTP storm, and in essence this would be the "I love you" virus.
That was enough of a strain on the public internet, but on wireless carriers, could quickly cause enough overload to block cel service.
Conclusion: Either use the ComposeViewController as #Srikar suggests, or else POST the data to your server, and send it from there.

Related

iOS Obj-C - Send email to app user in button action without opening mail client? [duplicate]

This question already has answers here:
How to send mail from iphone app without showing MFMailComposeViewController?
(2 answers)
Closed 5 years ago.
I want to make it so that when my app's user types their email address into my UITextField, tapping the "complete purchase" button will automatically send an email populated with certain details to the address they input. This seems like it should be simple enough, however I can't seem to get it to work without popping open the Apple Mail client? Right now, I have the following code in place:
ViewController.m
- (void)sendProgram {
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self; // Required to invoke mailComposeController when send
[mailCont setSubject:#"Your Purchase!"];
[mailCont setToRecipients:[NSArray arrayWithObject:#"brittany#test.ca"]];
[mailCont setMessageBody:#"Here is the program you ordered! Please click the following link to download." isHTML:NO];
[self presentViewController:mailCont animated:YES completion:nil];
}
}
And technically, once the mail client pops open with all of these details filled in, all I have to do is hit the send button. However I want this information to be sent without the mail client ever opening. Is this possible?
You cant send mail in iOS app without opening MFMailComposeViewController. Apple does not provide any Framework for such thing. If you want to do this then you will have to use API. Server (API) will send mail to the recipients.

Send SMS without presenting MessageComposeViewController in iOS

I am working on sending SMS demo. I want to send how to send the SMS and how to set the delegate to MessageComposeViewController. and in order to send the message we have below line
[self presentViewController:messageController animated:NO completion:nil];
This line will present the MessageComposeView on screen with SEND button. And Once we click on send button it sends the message. What I want is to send the message directly without presenting this MessageController on screen. Please help how can I do this.
In this related question, Apple has restrictions in place on being able to send a SMS message without the user clicking the SEND button.
Apple really wants the user to be in control of the SMS functionality of their phone. Otherwise all sorts of data could be flying off some random app (e.g. spamming your contacts with "try this app out!", which would not be very friendly nor very nice).
One of the answers in this question does have a potential non-MFMessageComposeViewController solution, however I have a feeling that if Apple catches you doing this they might deny your app from being approved for the app store.
You could send the message using some webservice on the internet. http://client.suresms.com/ProjectInfo.aspx?Info=3 or www.clickatell.com. They have bunches of API for sending messages.
In SureSMS simply create an account and make a http request to
http://suresms.com/Script/GlobalSendSMS.aspx?login=[youraccountnumber]&password=[yourpassword]&to=[phonenumber]&Text=Hallo.
Remember to URL encode the message text and use countrycodes. Thats it.
You have to present MessageComposeViewController.It's not possible to send without presenting it.
MFMessageComposeViewController has delegate method while delete/send/save. which only perform while we present it.
(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
You can't do it without MFMessageComposeViewController. Apple won't allow to send SMS without user interaction.
As per document
You must not modify the view hierarchy presented by this view
controller. You can, however, customize the appearance of the
interface using the UIAppearance protocol.
I've alternate solution of this, Alternative way can be Using web service API. Create a web service at server side that send a message to specific number(s) that accept numbers as parameters with request.(according to your requirement)
As using Web server or external sms provider can do it.
It is NOT possible . Apple willn't accept your App. Apple will reject your App if you do like that. Human interface guidelines should be followed up.

MFMailComposeViewController can cancel but nothing else is editable [duplicate]

I've been looking around for a framework to simply allow me to send an email from inside my app. I have tried MailCore, Pantomime and SKPSMTP all with no luck. I can't get them to compile in Xcode, so I presumed they were outdated. Is there any way I can do this? If so, how? Thanks.
You can easily send emails from your iOS device. No need to implement SMTP and all. Best thing about using inbuilt emailing facilities in iOS is it gives you access to the address book! So it auto-completes names, email addresses. Yaaiiii!!
Include, AddressBook,AddressBookUI and MessageUI frameworks and code something like this. Note you can even choose to send content as HTML too!
#import <MessageUI/MessageUI.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
MFMailComposeViewController *mailComposer;
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
[mailComposer setSubject:#"your custom subject"];
[mailComposer setMessageBody:#"your custom body content" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
For the sake of completeness, I have to write this selector to dismiss the email window if the user presses cancel or send -
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{
if(error) NSLog(#"ERROR - mailComposeController: %#", [error localizedDescription]);
[self dismissModalViewControllerAnimated:YES];
return;
}
Happy coding...
It should be noted that MFMailComposeViewController has a method called canSendMail. If you don't check this before presenting a MFMailComposeViewController on a device that doesn't have an email account, you'll get a SIGABRT.
It's easy to miss this when testing on the device or the simulator since you'll probably have an email account on your Mac and your iPad.
SKPSMTPMessage still works fine for sending emails without the need for a UI
.
Make sure you add a reference to the CFNetwork.framework in your project. Otherwise you will get build errors.
I would imagine the Apple Approved way of doing this would be to send the data to a server via HTTP Post, and have the server generate the mail for you. I've seen others asking similar questions to this, and the answer is that if you send it from the device, you really need to prompt the user.
I can even tell you why this is: Imagine an application that could send itself to everyone in your address book without the your confirmation, telling them that you just installed application X, and they should too. Even if well intentioned, this could quickly create a huge SMTP storm, and in essence this would be the "I love you" virus.
That was enough of a strain on the public internet, but on wireless carriers, could quickly cause enough overload to block cel service.
Conclusion: Either use the ComposeViewController as #Srikar suggests, or else POST the data to your server, and send it from there.

How do I send an e-mail with attachment from iPad

I am developing an experiment in Unity engine (3.4) , it is supposed to run on iPad (iOS5). I do most of my scripting in Monodevelop (2.4.2). After the experiment is over, the results are saved in a text file and stored within the program. I can access them via synchronizing with iTunes, but i want to implement an extra feature - i want to be able to send the file via e-mail. For the starters, the e-mail address can be hardwired into the program.
What i need to implement is as follows:
If the participant in finished:
close the file
compose the message using the hardwired address and the file
check if the ipad has access to the internet
if yes - send the message and place it in the 'sent' of my mail app.
if no - place the message into the outbox of my mail app.
I have experience with GUI and IO scripting, but i have hardly dealt with networking in any programming language, i have no idea where to start. Unity API and Unity Answers were not very helpful.
If you have any useful links or bits of code I could learn from, i would greatly appreciate it.
you can use the MFMailComposeViewController and attach you data as NSData like this
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *controller=[[MFMailComposeViewController alloc]init];
controller.mailComposeDelegate=self;
[controller setToRecipients#"..."];
[controller setSubject:#"Your Subject"];
NSData* attachmentData = ...
[controller addAttachmentData:attachmentData mimeType:#"..." fileName:#"..."];
[self presentModalViewController:controller animated:YES];
[controller release];
}
and dont forget to implement the delegate to dismiss the modal mail view controller
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
}
the user needs to have the mail app to be configured and press manually the send button

iphone 4.0 sending sms programmatically

I am working on a simple application in which I need send sms programmatically to my friends.
so write below code for sending sms .
MFMessageComposeViewController *picker = [[[MFMessageComposeViewController alloc] init]autorelease];
if([MFMessageComposeViewController canSendText])
{
picker.messageComposeDelegate = self;
picker.recipients =[NSArray arrayWithObject:#"123"];
picker.body=#"hello";
[self presentModalViewController:picker animated:YES];
}
but I do not want to load message picker and send sms to friends.
// [self presentModalViewController:picker animated:YES];
is it possible to send sms without click in send button.
The two options available in the iOS API are:
MFMessageComposeViewController - requires user confirmation
sms:// URLs - requires user confirmation
If you want to do something else, you'll need to set up a network-based service with an SMS gateway provider and send messages via that. I used to work with such a provider that had an HTTP POST interface, which would be simple enough to use. That comes with a couple of important differences:
the SMS is actually sent by the gateway server, not the handset (though you can usually rewrite the sender ID and get the message billed to the handset owner)
you'll need to pay for access to the service, which might include paying per message (or more likely per 1,000 messages)
Also note that sending SMS on your users' behalf without confirmation might be frowned upon when your app is reviewed, especially if they're billed for.

Resources