objective-c第三方發送郵件,後臺發送郵件

ios發送郵件,第三方庫SKPSMTPMessage發送郵件的使用. 不須要調用系統的郵件,能夠在app後臺發送郵件.下面是整理的一個使用SKPSMTPMessage方法.ios

//
//  NISendMail.m
//
//  Created by  羅若文 on 16/7/7.
//  Copyright © 2016年 羅若文. All rights reserved.
//

#import "NISendMail.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"

@interface NISendMail ()
@property NSString * login;
@property NSString * password;
@property NSString * relayHost;
@end

@implementation NISendMail

#pragma mark - 初始化,發送者郵箱,密碼,郵箱服務器
- (instancetype)initWithEmail:(NSString *)login password:(NSString *)password relayHost:(NSString *)relayHost
{
    self = [super init];
    if (self) {
        _login=login;
        _password=password;
        _relayHost=relayHost;
    }
    return self;
}

#pragma mark - 發送郵件:標題,接收方,內容,附件
-(void)sendMail:(NSString *)title toEmail:(NSString *)toEmail content:(NSString *)content enclosureName:(NSString *)enclosureName enclosure:(NSData * )enclosure{
    [self sendMail:title toEmail:toEmail bccEmail:nil ccEmail:nil content:content enclosureName:enclosureName enclosure:enclosure];
}

#pragma mark - 發送郵件:標題,接收方,隱藏抄送,抄送,內容,附件名,附件
-(void)sendMail:(NSString *)title toEmail:(NSString *)toEmail bccEmail:(NSString *)bccEmail ccEmail:(NSString *)ccEmail content:(NSString *)content enclosureName:(NSString *)enclosureName enclosure:(NSData * )enclosure{
    if(!content){
        content=@" ";
    }
    if([NIString isEmpty:enclosureName]){
        enclosureName=@"nullName.config";//忘記傳名字的時候
    }
    //線程發送郵件
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
        testMsg.fromEmail = _login;
        testMsg.toEmail = toEmail;
        testMsg.relayHost = _relayHost;//@"smtp.qq.com";
        testMsg.requiresAuth = [[NSNumber numberWithBool:YES] boolValue];
        if (testMsg.requiresAuth) {
            testMsg.login = _login;
            testMsg.pass = _password;
        }
        testMsg.wantsSecure = [[NSNumber numberWithBool:YES] boolValue]; // smtp.gmail.com doesn't work without TLS!
        if(title){
            testMsg.subject = title; //主題
        }else{
            testMsg.subject = @"標題"; //主題
        }
        
        if(bccEmail){
            testMsg.bccEmail = bccEmail;  //隱藏抄送
        }
        if(ccEmail){
            testMsg.ccEmail = ccEmail;  //抄送
        }
        
        NSDictionary * plainPart = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
                                    content,kSKPSMTPPartMessageKey,
                                    @"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

        
        if(enclosureName && enclosure){
            NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSString stringWithFormat:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"%@\"",enclosureName],kSKPSMTPPartContentTypeKey,
                                     [NSString stringWithFormat:@"attachment;\r\n\tfilename=\"%@\"",enclosureName],kSKPSMTPPartContentDispositionKey,
                                     [enclosure encodeBase64ForData],kSKPSMTPPartMessageKey,
                                     @"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
            
            testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
        }
        else{
            testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
        }
        [testMsg send];
    });

}
@end

在須要發送郵件的地方能夠這樣寫git

NISendMail * sm=[[NISendMail alloc]initWithEmail:@"寫發送者郵箱" password:@"發送者密碼,若是是qq郵箱要用獨立密碼" relayHost:@"smtp.qq.com"];
        [sm sendMail:@"標題" toEmail:@"732649784@qq.com" content:@"內容測試:使用第三方庫SKPSMTPMessage發送郵件" enclosureName:nil enclosure:nil];

查看源碼:http://git.oschina.net/shareDemoCode/SendEmailDemo服務器

相關文章
相關標籤/搜索