Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@laiso
Last active January 4, 2016 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laiso/8554578 to your computer and use it in GitHub Desktop.
Save laiso/8554578 to your computer and use it in GitHub Desktop.
@import CFNetwork;
@import Security;
#import <MailCore/MailCore.h>
const NSString * const DocomoMailAddress = @"xxx@docomo.ne.jp";
const NSString * const DocomoMailUser = @"user";
const NSString * const DocomoMailPassword = @"password";
const NSString * const ExampleMailingAddress = @"hello@example.jp";
@implementation DocomoMailSender
- (void)fetchMessages
{
MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
[session setHostname:@"imap2.spmode.ne.jp"];
[session setPort:993];
[session setUsername:DocomoMailUser];
[session setPassword:DocomoMailPassword];
[session setConnectionType:MCOConnectionTypeTLS];
MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindHeaders;
NSString *folder = @"INBOX";
MCOIndexSet *uids = [MCOIndexSet indexSetWithRange:MCORangeMake(1, UINT64_MAX)];
MCOIMAPFetchMessagesOperation *fetchOperation = [session fetchMessagesByUIDOperationWithFolder:folder requestKind:requestKind uids:uids];
[fetchOperation start:^(NSError * error, NSArray * fetchedMessages, MCOIndexSet * vanishedMessages) {
if(error) {
NSLog(@"Error downloading message headers:%@", error);
}
for (MCOAbstractMessage *message in fetchedMessages) {
NSLog(@"Subject: %@", message.header.subject);
}
}];
}
- (void)sendMail
{
MCOSMTPSession *session = [[MCOSMTPSession alloc] init];
[session setHostname:@"smtp.spmode.ne.jp"];
[session setPort:465];
[session setUsername:DocomoMailUser];
[session setPassword:DocomoMailPassword];
[session setConnectionType:MCOConnectionTypeTLS];
MCOMessageHeader *header = [[MCOMessageHeader alloc] init];
[header setFrom:[MCOAddress addressWithMailbox:DocomoMailAddress]];
[header setTo:@[[MCOAddress addressWithMailbox:ExampleMailingAddress]]];
[header setSubject:[NSString stringWithFormat:@"テストメール: %f", [[NSDate date] timeIntervalSince1970]]];
MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
builder.header = header;
builder.textBody = @""
"こんにちは\n"
"\n"
"テストメールです。\n";
MCOSMTPSendOperation *sendOperation = [session sendOperationWithData:[builder data]];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"Error send email connection:%@", error);
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment