I know how to send an email to my application by running the Mail application and then back to my application ... but I would like my application to be able to send email without opening the mail application. For example, I would press a button in my application, clicking on this button will send an email. Then I will notify the user that the letter was sent ...
Has anyone done this?
thanks.
Themselves
Here is sample code for sending email using MFMailComposeViewController.
-(IBAction)showPicker:(id)sender { // This sample can run on devices running iPhone OS 2.0 or later // The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. // So, we must verify the existence of the above class and provide a workaround for devices running // earlier versions of the iPhone OS. // We display an email composition interface if MFMailComposeViewController exists and the device can send emails. // We launch the Mail application on the device, otherwise. Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (mailClass != nil) { // We must always check whether the current device is configured for sending emails if ([mailClass canSendMail]) { [self displayComposerSheet]; } else { [self launchMailAppOnDevice]; } } else { [self launchMailAppOnDevice]; } } -(void)displayComposerSheet { // Displays an email composition interface inside the application. Populates all the Mail fields. MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Hello from California!"]; // Set up recipients 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]; // Attach an image to the email NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"]; // Fill out the email body text 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 { // Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation. message.hidden = NO; // Notifies users about errors associated with the interface switch (result) { case MFMailComposeResultCancelled: message.text = @"Result: canceled"; break; case MFMailComposeResultSaved: message.text = @"Result: saved"; break; case MFMailComposeResultSent: message.text = @"Result: sent"; break; case MFMailComposeResultFailed: message.text = @"Result: failed"; break; default: message.text = @"Result: not sent"; break; } [self dismissModalViewControllerAnimated:YES]; } -(void)launchMailAppOnDevice { // Launches the Mail application on the device. NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!"; NSString *body = @"&body=It is raining in sunny California!"; NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; }
. Apple MFMailComposeViewController (. ), iPhone Mail Mail . SMTP . - -. - . , , , , , . , SMTP, , , , , .
Apple:
MFMailComposeViewController , . , , , . , , .
The best way to do this is to have a web server for your mail sending application. You will transfer the email data and send it to your username.
Add framework to buildphases MessageUI.framework
ViewController.h File
#import <MessageUI/MessageUI.h> @interface ViewController () <MFMailComposeViewControllerDelegate>
ViewController.m File
-(IBAction)emailButtonClicked:(id)sender{ MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init]; if (mailComposer !=nil) { mailComposer.mailComposeDelegate = self; NSString *emailBody = @"Write the text here........"; [mailComposer setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:mailComposer animated:YES]; } } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [self becomeFirstResponder]; [self dismissModalViewControllerAnimated:YES]; }