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.

Skpsmtpmessage open source Library:

Https://github.com/jetseven/skpsmtpmessage

The attachment code is as follows:

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: @ "requ Iresauth "] 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 datawithcontentsoffile: 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", kskpsmtppartcontenttransferencodingkey, nil]; // image and video attachment // attach image nsstring * 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 \" ", kskpsmtppartcontentdispositionkey, [imgdata encodebase64fordata], kskpsmtppartmessagekey, @ "base64", callback, 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 \ "", kskpsmtppartcontenttypekey, @ "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 on the basis of the demo. After testing, you can normally send emails with attachments.

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.