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.
Asked
Active
Viewed 647 times
-1

cessmestreet
- 2,298
- 3
- 22
- 42
1 Answers
0
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!");
}
}];

Ryan Pendleton
- 2,566
- 19
- 29
-
1Hi Thanks for this. I tried it. I was able to import the library but whenever I try it, I always get: A stable connection to the server could not be established." i don't seem to understand why. :( – cessmestreet Oct 18 '14 at 09:06
-
Hi. I was able to fix it by changing the Authentication Type. Thank you for this. – cessmestreet Oct 18 '14 at 09:18