iOS SMTP发送邮件开源项目[SKPSMTPMessage]

SKPSMTPMessage 完全不依赖于iOS系统内置的发送邮件程序,可内置于你的应用实现后台发送邮件目的。

Demo效果图:



项目地址:

github地址:https://github.com/huanghuorong/iphone-smtp

googlecode:http://code.google.com/p/skpsmtpmessage/


关键代码:

#pragma mark IBActions
- (IBAction)sendMail:(id)sender 
{
    SKPSMTPMessage *test_smtp_message = [[SKPSMTPMessage alloc] init];
    test_smtp_message.fromEmail = fromEmail.text;
    test_smtp_message.toEmail = toEmail.text;
    test_smtp_message.relayHost = relayHost.text;
    test_smtp_message.requiresAuth = useAuthSwitch.on;
    test_smtp_message.login = login.text;
    test_smtp_message.pass = password.text;
    test_smtp_message.wantsSecure = SSLSwitch.on; // smtp.gmail.com doesn't work without TLS!
    test_smtp_message.subject = subject.text;
//    test_smtp_message.bccEmail = @"testbcc@test.com";
    
    // Only do this for self-signed certs!
    // test_smtp_message.validateSSLChain = NO;
    test_smtp_message.delegate = self;
    
    NSMutableArray *parts_to_send = [NSMutableArray array];
    
    //If you are not sure how to format your message part, send an email to your self.  
    //In Mail.app, View > Message> Raw Source to see the raw text that a standard email client will generate.
    //This should give you an idea of the proper format and options you need
    NSDictionary *plain_text_part = [NSDictionary dictionaryWithObjectsAndKeys:
                                     @"text/plain\r\n\tcharset=UTF-8;\r\n\tformat=flowed", kSKPSMTPPartContentTypeKey,
                                     [messageBody.text stringByAppendingString:@"\n"], kSKPSMTPPartMessageKey,
                                     @"quoted-printable", kSKPSMTPPartContentTransferEncodingKey,
                                     nil];
    [parts_to_send addObject:plain_text_part];
        
    if (sendVCFSwitch.on)
    {
        NSString *vcard_path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];
        NSData *vcard_data = [NSData dataWithContentsOfFile:vcard_path];
        NSDictionary *vcard_part = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,
                                    @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,
                                    [vcard_data encodeBase64ForData],kSKPSMTPPartMessageKey,
                                    @"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
        [parts_to_send addObject:vcard_part];
    }

    if (sendImageSwitch.on)
    {
        NSString *image_path = [[NSBundle mainBundle] pathForResource:@"Success" ofType:@"png"];
        NSData *image_data = [NSData dataWithContentsOfFile:image_path];        
        NSDictionary *image_part = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"inline;\r\n\tfilename=\"Success.png\"",kSKPSMTPPartContentDispositionKey,
                                    @"base64",kSKPSMTPPartContentTransferEncodingKey,
                                    @"image/png;\r\n\tname=Success.png;\r\n\tx-unix-mode=0666",kSKPSMTPPartContentTypeKey,
                                    [image_data encodeWrappedBase64ForData],kSKPSMTPPartMessageKey,
                                    nil];
        [parts_to_send addObject:image_part];
    }

    NSDictionary *sig_text_part = [NSDictionary dictionaryWithObjectsAndKeys:
                                     @"text/plain\r\n\tcharset=UTF-8;\r\n\tformat=flowed", kSKPSMTPPartContentTypeKey,
                                     [@"\n" stringByAppendingString:sig.text], kSKPSMTPPartMessageKey,
                                     @"quoted-printable", kSKPSMTPPartContentTransferEncodingKey,
                                     nil];
    [parts_to_send addObject:sig_text_part];
    
    test_smtp_message.parts = parts_to_send;
    
    Spinner.hidden = NO;
    [Spinner startAnimating];
    ProgressBar.hidden = NO;
    HighestState = 0;
    
    [test_smtp_message send];
}


参考:IOS开发中发送Email的两种方法

http://blog.sina.com.cn/s/blog_94d94f1a01016rgc.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值