iOS send mail, SMS

Source: Internet
Author: User

The first way: OpenURL
    #pragmaMark-Send mail using System mail client-(void) Launchmailapp {nsmutablestring*mailurl =[[[ nsmutablestring alloc]init]autorelease]; //Add RecipientsNsarray *torecipients = [Nsarray arraywithobject:@"[email protected]"]; [Mailurl AppendFormat:@"mailto:%@", [torecipients componentsjoinedbystring:@","]]; //Add ccNsarray *ccrecipients = [Nsarray arraywithobjects:@"[email protected]",@"[email protected]", nil]; [Mailurl AppendFormat:@"? cc=%@", [ccrecipients componentsjoinedbystring:@","]]; //add encryption to sendNsarray *bccrecipients = [Nsarray arraywithobjects:@"[email protected]", nil]; [Mailurl AppendFormat:@"&bcc=%@", [bccrecipients componentsjoinedbystring:@","]]; //Add a Theme[Mailurl appendString:@"&subject=my Email"]; //Add message Content[Mailurl appendString:@"&body=<b>email</b> body!"]; NSString* email =[Mailurl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];         [[UIApplication Sharedapplication] OpenURL: [Nsurl Urlwithstring:email]]; }  
The second way: Mfmailcomposeviewcontroller

Let the controller comply with the <MFMailComposeViewControllerDelegate> protocol

#pragmaMark-Send messages within the app//Activate mail feature- (void) sendmailinapp{Class mailclass= (Nsclassfromstring (@"Mfmailcomposeviewcontroller")); if(!Mailclass) {[Wsyfunction alertviewwithtitle:@"Tips"Message@"the current system version does not support in-app send mail functionality, and you can use the Mailto method instead"Buttontitle:@"Determine"]; return; }    if(![Mailclass Cansendmail]) {[Wsyfunction alertviewwithtitle:@"Tips"Message@"user does not have a mail account set up"Buttontitle:@"Determine"]; return; } [self displaymailpicker];}//bring up the mail send window- (void) displaymailpicker{Mfmailcomposeviewcontroller*mailpicker =[[Mfmailcomposeviewcontroller alloc] init]; Mailpicker.mailcomposedelegate=Self ; //Set Theme[Mailpicker setsubject: [NSString stringWithFormat:@"%@", Self.booknamearray[choosebook]]; //Add RecipientsNsarray *torecipients = [Nsarray arraywithobject:@""];    [Mailpicker settorecipients:torecipients]; //Add cc//Nsarray *ccrecipients = [Nsarray arraywithobjects:@ "[email protected]", @ "[email protected]", nil];//[Mailpicker setccrecipients:ccrecipients]; //add encryption to send//Nsarray *bccrecipients = [Nsarray arraywithobjects:@ "[email protected]", nil];//[Mailpicker setbccrecipients:bccrecipients]; //    //Add a pictureUIImage *addpic = [UIImage imagenamed:@"Lanch.jpeg"]; NSData*imagedata = Uiimagepngrepresentation (addpic);//PNG//about MimeType:http://www.iana.org/assignments/media-types/index.html[Mailpicker addattachmentdata:imagedata MimeType:@""FileName:@"Lanch.jpeg"]; //to construct a storage path for a string file//get the document directoryNSString *docpath =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*strpath = [DocPath stringbyappendingpathcomponent:[nsstring stringWithFormat:@"%@.txt", Self.booknamearray[choosebook]]; //constructing a String objectNSString *book_str =@"";  for(inti =0; i < Self.array.count; i++) {Book_str= [Book_str stringbyappendingstring:[nsstring stringWithFormat:@"\n\n%@", Self.array[i]]; }    //the ability to store a string in a file by sending the WriteToFile:atomically:encoding:error: method to a String object[book_str writetofile:strpath atomically:yes encoding:nsutf8stringencoding Error:nil]; NSData*bookdata =[NSData Datawithcontentsoffile:strpath]; //Send txt text attachment[Mailpicker addattachmentdata:bookdata MimeType:@"Text/txt"Filename:[nsstring stringWithFormat:@"%@.txt", Self.booknamearray[choosebook]]; NSString*emailbody =@"<font color= ' red ' >eMail</font> text";    [Mailpicker setmessagebody:emailbody Ishtml:yes]; [Self Presentmodalviewcontroller:mailpicker animated:yes];}#pragmaMark-Implements Mfmailcomposeviewcontrollerdelegate-(void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller didfinishwithresult: (mfmailcomposeresult) result Error: (Nserror *) error{//Close mail Send window[self dismissmodalviewcontrolleranimated:yes]; NSString*msg; Switch(Result) { Casemfmailcomposeresultcancelled:msg=@"you have canceled editing a message";  Break;  Casemfmailcomposeresultsaved:msg=@"you have successfully saved the message";  Break;  Casemfmailcomposeresultsent:msg=@"you click Send, put the message in the queue, not yet sent";  Break;  Casemfmailcomposeresultfailed:msg=@"you tried to save or send a message failed";  Break; default: Msg=@"";  Break; } [wsyfunction Alertviewwithtitle:@""Message:msg Buttontitle:@"Determine"];}

Finally, the direct call method is used:

[Self Sendmailinapp];

iOS send mail, SMS

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.