iOS - Send an email automatically (NOT from user&#

2020-07-17 05:10发布

问题:

How can I send an email automatically from an app, to myself, NOT from the user's account(that is not possible without user interaction), but from another email id of mine(with username and password given in the code)?

回答1:

Yes, you can send email without user interation. But you have to make use of SMTP services on your server side.

Pls Refere: http://iosameer.blogspot.in/2013/01/sending-e-mail-in-background-from-ios_25.html



回答2:

You need to import:

#import "SKPSMTPMessage.h"

And then use this funtion:

-(void)sendEmail
{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg = [[SKPSMTPMessage alloc] init];

    testMsg.fromEmail = [defaults objectForKey:@"fromEmail"];
    testMsg.toEmail = [defaults objectForKey:@"toEmail"];
    testMsg.bccEmail = [defaults objectForKey:@"bccEmal"];
    testMsg.relayHost = [defaults objectForKey:@"relayHost"];
    testMsg.requiresAuth = [[defaults objectForKey:@"requiresAuth"] boolValue];

    if (testMsg.requiresAuth) {
        testMsg.login = [defaults objectForKey:@"login"];

        testMsg.pass = [defaults objectForKey:@"pass"];
    }

    testMsg.wantsSecure = [[defaults objectForKey:@"wantsSecure"] boolValue]; //
    testMsg.subject = @"Your Email subject";
    testMsg.delegate = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [testMsg send];
    });

}

And then call this function from where you need to send the Email:

[self sendEmail];

Try it, it is working... All the best!!!