使用SKPSMTPMessage库发送 邮件地址:https://github.com/jetseven/skpsmtpmessage

这个库比较老旧,使用MRC手动内存管理(如下),当然也可以用pod直接导入,而且省事方便ios -静默方式发送邮件

引入头文件

#import "SKPSMTPMessage.h"

#import "NSData+Base64Additions.h"

遵循代理  SKPSMTPMessageDelegate

//代码如下

   

-(void)sendEmail{

 

    SKPSMTPMessage *testMsg = [[SKPSMTPMessagealloc] init];

    //发送邮箱

    testMsg.fromEmail =@"[email protected]";

    //收件邮箱

    testMsg.toEmail =@"[email protected]";

    testMsg.relayHost =@"mail.hi-service.cn";

    testMsg.requiresAuth =YES;

    //发送邮箱

    testMsg.login =@"[email protected]";

    //发送邮箱密码

    testMsg.pass =@"********";

    testMsg.wantsSecure =YES;

    testMsg.delegate =self;

    //主题

    testMsg.subject = [NSStringstringWithFormat:@"%@_%@",info.companyName,info.userNames];

    //内容

    NSDictionary *plainPart = [NSDictionary                                dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,[NSStringstringWithCString:"测试正文"encoding:NSUTF8StringEncoding], kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

   

    NSDictionary *vcfPart = [NSDictionarydictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,[NSStringstringWithFormat:@"%@",contentStr],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

    

     //添加附件图片

     NSData *imgData =UIImagePNGRepresentation(_selectIcon);

     NSDictionary *imagePart = [NSDictionarydictionaryWithObjectsAndKeys:@"image/png;\r\n\tx-unix-mode=0644;\r\n\tname=\"backIcon.png\"",kSKPSMTPPartContentTypeKey,                             @"attachment;\r\n\tfilename=\"backIcon.png\"",kSKPSMTPPartContentDispositionKey,[imgDataencodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

    testMsg.parts = [NSArrayarrayWithObjects:plainPart,vcfPart,imagePart,nil];

    [testMsg send];

}


//代理方法

- (void)messageSent:(SKPSMTPMessage *)message

{

    [AlertshowWithTitle:@"提交成功"];

    [self.navigationControllerpopViewControllerAnimated:YES];

    NSLog(@"%@", message);

}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error

{

    [AlertshowWithTitle:@"提交失败"];

    NSLog(@"message - %@\nerror - %@", message, error);

}

  到此为止写完了,先不用激动,后边还有坑,当发送成功收到邮件时,一脸蒙蔽了,标题乱码了,经过一系列的编码尝试都不行,这时要修改库里的编码方式,先找到SKPSMTPMessage.m这个类,869行编码格式改为NSUTF8StringEncoding


     

相关文章:

  • 2021-12-20
  • 2021-11-30
  • 2021-04-11
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2021-10-09
  • 2022-01-30
  • 2022-02-15
  • 2022-02-12
  • 2022-01-03
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案