Xcode 4/iOS - Send an Email Using Smtp from Inside My App

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

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...

How to send mail from iphone app without showing MFMailComposeViewController?

Sending emails programmatically, without user intervention, from an iphone application, cannot be implemented using any of the Apple frameworks. It could be possible in a jailbroken phone but then it would never see the inside of App Store.

If you want control of email sending, then a better way would be to set up a web service (at your server end) you can post to using an HTTP request. If you are posting to only one address this can work very well, although you may want to get the user to input their return mail address.

Otherwise only the standard dialog is available (this relies on using whatever account they've setup on the device).

Send email with content without using webservice in iPhone app

I have achieved this with SMKSMTPMessage - a library that allows you to send an email with attachments if required from code, without any user interaction required



Related Topics



Leave a reply



Submit