I'm trying to use NSSharingService to compose a mail. I'm able to add recipients, body, subject and attachments but I can't seem to add a sender and autosend the email.
Is there a way to add a sender and automatically send the email using NSSharingService?
If there is, can you tell me how?
If none, can you suggest another approach. I tried ScriptingBridge. I got it to work but when I automatically run my application using a scheduler, it's having -[SBProxyByClass setSender:]: object has not been added to a container yet; selector not recognized
crash. This is why I'm trying a new approach which is using NSSharingService.
Thanks.
Apple is pretty strict about letting you perform actions without user consent. Since you want to be able to do it automatically, rather than using NSSharingService or ScriptingBridge, I'd recommend just using an SMTP library inside your app.
It looks like a popular library for Objective-C is libmailcore, although I haven't used it before so I can't tell you much about it. Their example for sending a message looks simple enough:
MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = @"matt@gmail.com";
smtpSession.password = @"password";
smtpSession.authType = MCOAuthTypeSASLPlain;
smtpSession.connectionType = MCOConnectionTypeTLS;
MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *from = [MCOAddress addressWithDisplayName:@"Matt R"
mailbox:@"matt@gmail.com"];
MCOAddress *to = [MCOAddress addressWithDisplayName:nil
mailbox:@"hoa@gmail.com"];
[[builder header] setFrom:from];
[[builder header] setTo:@[to]];
[[builder header] setSubject:@"My message"];
[builder setHTMLBody:@"This is a test message!"];
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation =
[smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"Error sending email: %@", error);
} else {
NSLog(@"Successfully sent email!");
}
}];