How to send an email to a recipient in the background in iOS5?

In the iPhone app, I want to send an email to a person who has forgotten their password. I want to send mail in the background (do not use MFMailComposeViewController for this), and the application should not be moved to the background. Is there any way to achieve this?

+8
email iphone background ios5
source share
7 answers

The best way to do this is to use SKPSMTPMessage. You can download it here: https://github.com/jetseven/skpsmtpmessage This is a very simple solution that I used earlier to use the "Forgot Password" solutions in iOS applications. To simply drag and drop the downloaded files into your application, #import "SKPSMTPMessage.h" into your class and implement the following code:

.h

 #import "SKPSMTPMessage.h" @interface SomeView : UIViewController <SKPSMTPMessageDelegate> { } - (IBAction)forgotPassword; 

.m

 - (IBAction)forgotPassword { SKPSMTPMessage *forgotPassword = [[SKPSMTPMessage alloc] init]; [forgotPassword setFromEmail:@"some-email@gmail.com"]; // Change to your email address [forgotPassword setToEmail:@"user-email@gmail.com"]; // Load this, or have user enter this [forgotPassword setRelayHost:@"smtp.gmail.com"]; [theMessage setRequiresAuth:YES]; // GMail requires this [forgotPassword setLogin:@"some-email@gmail.com"]; // Same as the "setFromEmail:" email [forgotPassword setPass:@"password"]; // Password for the Gmail account that you are sending from [forgotPassword setSubject:@"Forgot Password: My App"]; // Change this to change the subject of the email [forgotPassword setWantsSecure:YES]; // Gmail Requires this [forgotPassword setDelegate:self]; // Required NSString *newpassword = @"helloworld"; NSString *message = [NSString stringWithFormat:@"Your password has been successfully reset. Your new password: %@", newpassword]; NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain", kSKPSMTPPartContentTypeKey, message, kSKPSMTPPartMessageKey, @"8bit" , kSKPSMTPPartContentTransferEncodingKey, nil]; [forgotPassword setParts:[NSArray arrayWithObjects:plainPart, nil]]; [forgotPassword send]; } 

Also be sure to include the following methods in .m. You can change the contents of UIAlertViews depending on what you want to display to the user.

 - (void)messageSent:(SKPSMTPMessage *)message { NSLog(@"Message Sent"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Password Reset" message:@"Check your email for your new password." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } - (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error { NSLog(@"Message Failed With Error(s): %@", [error description]); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was an error reseting your password. Please try again later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } 

You also need to do the following before this works. Your goal → Get information → Build → All configurations → Other link flags: “-ObjC” If you need help with this, see http://developer.apple.com/qa/qa2006/qa1490.html

EDIT: * CFNetwork.framework also needs to be added for this! *

Let me know if you have more questions.

Thanks Jacob

+10
source share

You cannot use MFMailComposeViewController for this. No API will allow you to send emails or any messages on behalf of a user without seeing him.

The only thing I see is to call your server and the server will send an email, for example:

 NSURLRequest requestWithURL:[NSURL urlWithString:@"http://server.com/send_passcode?to=email@lala.com"]]; 
+5
source share

You cannot send SMS / email without user acceptance. But there are many web services on the Internet that can send SMS / email. I assume that some application uses these services or uses its own.

+2
source share

You CAN send emails in the background (without using MFMail Controller by default). BUT you still need the user to fill out any form (or the content you want to send by email), and ask them to click "Submit".

Here is my post on how to do this. It includes code and images.

Field Lock in MFMailComposeViewController

PS this works, and Apple has approved more than 10 of my apps that use this code / method.

+2
source share

Regarding the PostageApp comment below, if you want to send emails without any problems setting up the SMTP client, you can check the PostageKit shell to use the PostageApp service. Let you securely send emails with line passwords.

https://github.com/twg/PostageKit

0
source share

Maybe you should implement a PHP script that will send an email to the user. In ios, you can use the POST method in NSURLConnection to call a PHP script. You can find many scripts on Google to send email to the user.

0
source share

Download the SKPSMTP library and import

 #import "SKPSMTPMessage.h" #import "NSData+Base64Additions.h" -(IBAction)btnRecoverClicked:(id)Sender; 

Then we implement the method of sending mail in the background.

 -(IBAction) btnRecoverClicked:(id)sender { NSString *str=@"Your password is:"; NSString *strUserPassword=[NSString stringWithFormat:@"%@ %@",str,struserPassword]; NSLog(@"Start Sending"); SKPSMTPMessage *emailMessage = [[SKPSMTPMessage alloc] init]; emailMessage.fromEmail = @"XXXXX"; //sender email address emailMessage.toEmail = struserEmail; //receiver email address emailMessage.relayHost = @"smtp.gmail.com"; //emailMessage.ccEmail =@"your cc address"; //emailMessage.bccEmail =@"your bcc address"; emailMessage.requiresAuth = YES; emailMessage.login = @"xxxxxxxx"; //sender email address emailMessage.pass = @"XXXXXXX"; //sender email password emailMessage.subject =@"Password Recovery"; emailMessage.wantsSecure = YES; emailMessage.delegate = self; // you must include <SKPSMTPMessageDelegate> to your class NSString *messageBody = [NSString stringWithFormat:@"Your password is: %@",struserPassword] ; //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... } 

To cope with success and failed to use

 -(void)messageSent:(SKPSMTPMessage *)message{ NSLog(@"delegate - message sent"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message sent to your mail." message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; } 

and

 -(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]); } 
0
source share

All Articles