Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

iphone - How to send an email to a receipent in background in iOS5?

In an iPhone app,I want to send an email to a person who has forgotten about their passcode . I want to send the mail in background (cant use MFMailComposeViewController for this) and also the app must not be pushed to background . Is there a way to achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The best way of doing this is using SKPSMTPMessage. You can download it from here: https://github.com/jetseven/skpsmtpmessage This is a very easy solution that I have used before for using "Forgot Password" solutions in iOS apps. To implement simply drag the downloaded files into your application, #import the the "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:@"[email protected]"];  // Change to your email address
[forgotPassword setToEmail:@"[email protected]"]; // Load this, or have user enter this
[forgotPassword setRelayHost:@"smtp.gmail.com"];
[theMessage setRequiresAuth:YES]; // GMail requires this
[forgotPassword setLogin:@"[email protected]"]; // 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 the .m. You can change the contents of the 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 will work. Your Target -> Get Info -> 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 must also be added for this to work! *

Let me know if you have any more questions.

Thanks, Jacob


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...