如何在iOS5中向后台发送电子邮件?

Ato*_*tom 8 email iphone background ios5

在iPhone应用程序中,我想向忘记密码的人发送电子邮件.我想在后台发送邮件(不能使用MFMailComposeViewController),也不能将应用程序推送到后台.有没有办法实现这个目标?

RMK*_*cob 10

这样做的最佳方法是使用SKPSMTPMessage.你可以从这里下载:https://github.com/jetseven/skpsmtpmessage 这是我以前在iOS应用程序中使用"忘记密码"解决方案时使用的一个非常简单的解决方案.要实现,只需将下载的文件拖到您的应用程序中,#import将"SKPSMTPMessage.h"拖入您的类,并实现以下代码:

.H

#import "SKPSMTPMessage.h"

@interface SomeView : UIViewController <SKPSMTPMessageDelegate> {

}

- (IBAction)forgotPassword;
Run Code Online (Sandbox Code Playgroud)

.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];
}
Run Code Online (Sandbox Code Playgroud)

还要确保在.m中包含以下方法.您可以根据要向用户显示的内容更改UIAlertView的内容.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

在此工作之前,您还需要执行以下操作.您的目标 - >获取信息 - >构建 - >所有配置 - >其他链接标志:" - ObjC"如果您需要帮助,请参阅http://developer.apple.com/qa/qa2006/qa1490.html

编辑: *还必须添加CFNetwork.framework才能工作!*

如果您还有其他问题,请与我们联系.

谢谢你,雅各布


Fel*_*ano 5

你不能MFMailComposeViewController这样做.没有API将允许您代表用户发送电子邮件或任何类型的消息而无法看到它.

我唯一看到的是打电话给你的服务器,服务器发送电子邮件,如下所示:

NSURLRequest requestWithURL:[NSURL urlWithString:@"http://server.com/send_passcode?to=email@lala.com"]];
Run Code Online (Sandbox Code Playgroud)


ber*_*ium 2

未经用户同意,您无法发送短信/电子邮件。但是互联网上有很多网络服务可以发送短信/电子邮件。我猜有些应用程序使用这些服务或使用自己的服务。