Use the iOS open source library SKPSMTPMessage to send emails

Source: Internet
Author: User

There are currently two ways to send emails in iOS. You can use openURL to open the iOS email app and use MFMailComposeViewController to bring up the email interface in the app to send emails. There are a lot of introductions to the two search methods. The third method is described below. The Open Source library SKPSMTPMessage is used to send emails. In fact, this method has also been introduced in many articles, but I have read some articles and write almost all of them. They all paste the code in the demo, and there is no function I need to send images and video attachments. I have studied and consulted some materials, integrated the code, and pasted it to make it easier for myself and anyone who needs it. [Cpp] NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; SKPSMTPMessage * testMsg = [[SKPSMTPMessage alloc] init]; testMsg. fromEmail = [defaults objectForKey: @ "fromEmail"]; testMsg. toEmail = [defaults objectForKey: @ "toEmail"]; testMsg. bccEmail = [defaults objectForKey: @ "bccEmal"]; testMsg. relayHost = [defaults objectForKey: @ "relayHost"]; testMsg. requiresAuth = [[defaults objectForKey: @ "RequiresAuth"] boolValue]; if (testMsg. requiresAuth) {testMsg. login = [defaults objectForKey: @ "login"]; testMsg. pass = [defaults objectForKey: @ "pass"];} testMsg. wantsSecure = [[defaults objectForKey: @ "wantsSecure"] boolValue]; // smtp.gmail.com doesn't work without TLS! TestMsg. subject = @ "SMTPMessage Test Message"; // testMsg. bccEmail = @ "testbcc@test.com"; // Only do this for self-signed certs! // TestMsg. validateSSLChain = NO; testMsg. delegate = self; // text information NSDictionary * plainPart = [NSDictionary dictionaryWithObjectsAndKeys: @ "text/plain", kSKPSMTPPartContentTypeKey, @ "This is a t é st mess limit ge. ", kSKPSMTPPartMessageKey, @" 8bit ", kSKPSMTPPartContentTransferEncodingKey, nil]; // contact information NSString * vcfPath = [[NSBundle mainBundle] pathForResource: @" test "ofType: @ "vcf"]; NSData * vcfData = [NSData dat AWithContentsOfFile: vcfPath]; NSDictionary * vcfPart = [NSDictionary dictionaryWithObjectsAndKeys: @ "text/directory; \ r \ n \ tx-unix-mode = 0644; \ r \ n \ tname = \ "test. vcf \ "", kSKPSMTPPartContentTypeKey, @ "attachment; \ r \ n \ tfilename = \" test. vcf \ "", kSKPSMTPPartContentDispositionKey, [vcfData encodeBase64ForData], kSKPSMTPPartMessageKey, @ "base64", signature, nil]; // image and video attachment // attach image N SString * imgPath = [[NSBundle mainBundle] pathForResource: @ "test" ofType: @ "jpg"]; NSData * imgData = [NSData dataWithContentsOfFile: imgPath]; NSDictionary * imagePart = [NSDictionary dictionaryWithObjectsAndKeys: @ "image/jpg; \ r \ n \ tx-unix-mode = 0644; \ r \ n \ tname = \ "test.jpg \" ", kSKPSMTPPartContentTypeKey, @" attachment; \ r \ n \ tfilename = \ "test.jpg \" ", region, [imgData encodeBase64ForDa Ta], kSKPSMTPPartMessageKey, @ "base64", delimiter, nil]; // attach video NSString * videoPath = [[NSBundle mainBundle] pathForResource: @ "video" ofType: @ "mov"]; NSData * videoData = [NSData dataWithContentsOfFile: videoPath]; NSDictionary * videoPart = [NSDictionary dictionaryWithObjectsAndKeys: @ "video/quicktime; \ r \ n \ tx-unix-mode = 0644; \ r \ n \ tname = \ "video. mov \ "", kSKPSMTPPartConte NtTypeKey, @ "attachment; \ r \ n \ tfilename = \" video. mov \ "", kSKPSMTPPartContentDispositionKey, [videoData encodeBase64ForData], kSKPSMTPPartMessageKey, @ "base64", kSKPSMTPPartContentTransferEncodingKey, nil]; testMsg. parts = [NSArray arrayWithObjects: plainPart, vcfPart, imagePart, videoPart, nil]; [testMsg send]; the Code is modified based on the Demo. After testing, an email with attachments can be sent normally.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.