问题
I want to send mail from my custom iPhone app. I have used MFMailComposeViewController
to send mail from my iphone in my previous app. Now, i don't want to show the MFMailComposeViewController
to the user, if they click Send Mail button
the mail automatically send to the recipient mail address
. How can i do this? Can you please help me on this? Thanks in advance.
I have used below code to show the MFMailComposeViewController,
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"Details"];
[controller setMessageBody:@"Hi" isHTML:NO];
[controller setToRecipients:[NSArray arrayWithObjects:@"abcd.m@gmail.com", nil]];
[self presentModalViewController:controller animated:YES];
[controller release];
回答1:
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).
回答2:
The iOS SDK has made it really easy to send email using the built-in APIs. With a few line of codes, you can launch the same email interface as the stock Mail app that lets you compose an email. You can pop up mail composer form , write message and can send plain mail or file attached mail using MFMailComposeViewController class. For more info : Sending e-mail from your iOS App
But, in this section what i am going to explain is about sending emails without showing the mail composer sheet ie. sending emails in background. For this feature, we can not use iOS native MFMailComposer class because it does not allow us to send emails in background instead it pop ups the mail composer view from where user have to tap "send" button , so for this section i am going to use SKPSMTPMessage Library to send emails in background, however email account has to be hardcoded on this method.
Limitations :
sender/receiver email address has to be hardcoded or you have to grab it using some pop up form in your app where user inputs sender/receiver email address. In addition, sender account credentials has to be also hardcoded since there is no way we can grab it from device settings.
Method :
- Import CFNetwork.framework to your project.
- Include #import "SKPSMTPMessage.h" #import "NSData+Base64Additions.h" // for Base64 encoding
- Include to your ViewController
- Download SKPSMTPMessage library from
https://github.com/jetseven/skpsmtpmessage Drag and Drop "SMTPLibrary" folder you have downloaded to your project.
Before proceeding, let you know that i am using sender/receiver email address and sender password hardcoded in the code for this example.But, you may grab this credentials from user, allowing them to input in some sort of forms(using UIViews).
-(void) sendEmailInBackground { NSLog(@"Start Sending"); SKPSMTPMessage *emailMessage = [[SKPSMTPMessage alloc] init]; emailMessage.fromEmail = @"sender@gmail.com"; //sender email address emailMessage.toEmail = @"receiver@gmail.com"; //receiver email address emailMessage.relayHost = @"smtp.gmail.com"; //emailMessage.ccEmail =@"your cc address"; //emailMessage.bccEmail =@"your bcc address"; emailMessage.requiresAuth = YES; emailMessage.login = @"sender@gmail.com"; //sender email address emailMessage.pass = @"Passwxxxx"; //sender email password emailMessage.subject =@"@"email subject header message"; emailMessage.wantsSecure = YES; emailMessage.delegate = self; // you must include <SKPSMTPMessageDelegate> to your class NSString *messageBody = @"your email body message"; //for example : NSString *messageBody = [NSString stringWithFormat:@"Tour Name: %@\nName: %@\nEmail: %@\nContact No: %@\nAddress: %@\nNote: %@",selectedTour,nameField.text,emailField.text,foneField.text,addField.text,txtView.text]; // Now creating plain text email message NSDictionary *plainMsg = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, messageBody,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; emailMessage.parts = [NSArray arrayWithObjects:plainMsg,nil]; //in addition : Logic for attaching file with email message. /* NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"JPG"]; NSData *fileData = [NSData dataWithContentsOfFile:filePath]; NSDictionary *fileMsg = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx- unix-mode=0644;\r\n\tname=\"filename.JPG\"",kSKPSMTPPartContentTypeKey,@"attachment;\r\n\tfilename=\"filename.JPG\"",kSKPSMTPPartContentDispositionKey,[fileData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; emailMessage.parts = [NSArray arrayWithObjects:plainMsg,fileMsg,nil]; //including plain msg and attached file msg */ [emailMessage send]; // sending email- will take little time to send so its better to use indicator with message showing sending... }
Now, handling delegate methods :
// On success
-(void)messageSent:(SKPSMTPMessage *)message{
NSLog(@"delegate - message sent");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message sent." message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
// On Failure
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{
// open an alert with just an OK button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]);
}
Ok, thats all from the coding side. hope this tutorial may find useful for you guyz
来源:https://stackoverflow.com/questions/9427056/how-to-send-mail-from-iphone-app-without-showing-mfmailcomposeviewcontroller