美文网首页
利用SMTPLibrary后台发送邮件

利用SMTPLibrary后台发送邮件

作者: im小码哥 | 来源:发表于2018-08-06 11:45 被阅读29次

    话不多少,直接代码,先添加头文件和代理 SKPSMTPMessageDelegate

    #import "SKPSMTPMessage.h"
    #import "NSData+Base64Additions.h"
    /**
      email  接收方邮箱
      shareImage 附件图片
      title 标题
      content 内容
    */
    -(void)sendEmailMsg:(NSString *)email shareImage:(UIImage *)shareImage title:(NSString *)title content:(NSString *)content{
    
    NSMutableDictionary *defaultsDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"fromEmail@fromEmail.com.cn", @"fromEmail",
                                               email, @"toEmail",
                                               @"smtp.exmail.qq.com", @"relayHost",
                                               @"fromEmail@fromEmail.com.cn", @"login",
                                               @"fromEmailPwd", @"pass",
                                               [NSNumber numberWithBool:YES], @"requiresAuth",
                                               [NSNumber numberWithBool:YES], @"wantsSecure", nil];
    
    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = [defaultsDictionary objectForKey:@"fromEmail"];
    testMsg.toEmail = [defaultsDictionary objectForKey:@"toEmail"];;
    testMsg.relayHost = [defaultsDictionary objectForKey:@"relayHost"];
    testMsg.requiresAuth = [[defaultsDictionary objectForKey:@"requiresAuth"] boolValue];
    if (testMsg.requiresAuth) {
        testMsg.login = [defaultsDictionary objectForKey:@"login"];
        testMsg.pass = [defaultsDictionary objectForKey:@"pass"];  
    }
    testMsg.wantsSecure = [[defaultsDictionary objectForKey:@"wantsSecure"] boolValue]; // smtp.gmail.com doesn't work without TLS!
    NSString *txt = content;
    testMsg.subject = title;
    testMsg.delegate = self;
    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,txt,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    if (!shareImage) {
        testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
    }else{
        NSData *vcfData = UIImageJPEGRepresentation(shareImage, 0.5);
        NSString *str1 = [NSString stringWithFormat:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"report.png\""];
        NSString *str2 = [NSString stringWithFormat:@"attachment;\r\n\tfilename=\"report.png\""];
        NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:str1,kSKPSMTPPartContentTypeKey,
                                 str2,kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
        testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];}
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [testMsg send];
    });}
    

    回调如下:

      - (void)messageSent:(SKPSMTPMessage *)message{
     NSLog(@"邮件已发送,请注意查收");
      }
    
    - (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{
    NSLog(@"发送失败,请重试");
    }
    

    注意事项
    SMTPLibrary是运用的MRC,所以在ARC需要在targets下 选择BuildPhases下找到SKPSMTPMessage 和 NSStream+SKPSMTPExtensions 文件添加 -fno-objc-arc 即可。

    相关文章

      网友评论

          本文标题:利用SMTPLibrary后台发送邮件

          本文链接:https://www.haomeiwen.com/subject/lmmwvftx.html