71

What I already found is

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];

But I just want to open the Mail app not only a composer view. Just the mail app in its normal or last state.

Any ideas?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
cschuff
  • 5,502
  • 7
  • 36
  • 52

15 Answers15

96

Apparently Mail application supports 2nd url scheme - message:// which ( I suppose) allows to open specific message if it was fetched by the application. If you do not provide message url it will just open mail application:

NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • 6
    Wondering if the Slack app uses this technique. They are launching without a mail compose view – Daniel Mar 23 '15 at 16:26
  • 3
    this should be the accepted answer. The current accepted answer says that this is not possible but using "message//" does open the email app without the compose screen. – John Stricker Aug 26 '15 at 14:26
  • 1
    Great to find this! Do you know if we can use that URL scheme so that Mail app opens an e-mail file (_.msg_ from Outlook or similar) giving a URL where the file is hosted? So something like `message://http://domain/file.msg`. – Alex MM Jun 10 '16 at 14:30
  • @AlexMM, I have not tried that, but if you have message url + attachment url that might work – Vladimir Jun 10 '16 at 15:30
  • According to [this post](https://www.macstories.net/tutorials/ios-7-and-mail-message-urls/) Mail app will only open mails that are already downloaded in Mail app. But the thing is if there is any other way to open a mail hosted in some domain. Thanks! – Alex MM Jun 13 '16 at 08:03
  • It may be worth a separate question, I am not sure. – Vladimir Jun 13 '16 at 16:03
  • This is the accepted answer, but still it's undocumented by Apple in https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MailLinks/MailLinks.html#//apple_ref/doc/uid/TP40007899-CH4-SW1 so I'm not really sure it should be the best answer. – Axy Nov 05 '19 at 05:09
48
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";

NSString *body = @"&body=It is raining in sunny California!";

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];

email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
DShah
  • 9,768
  • 11
  • 71
  • 127
Amit
  • 1,795
  • 1
  • 17
  • 20
  • 18
    This opens in compose view. – crizCraig Jun 05 '13 at 00:07
  • 1
    Not working in iOS 11. Can any one help me with this? – Hardik Amal Nov 04 '17 at 08:25
  • 1
    @HardikAmal iOS 11: The problem is that `stringByAddingPercentEscapesUsingEncoding` is encoding all special characters (including the colon after `mailto`). The solution is either to find a better encoding function or get rid of the encoding line and manually encode the message by substituting %20 for the spaces in the `subject` and `body`. – DavidH Mar 30 '18 at 15:45
21

Swift version of the original Amit's answer:

Swift 5

func openEmailApp(toEmail: String, subject: String, body: String) {
    guard
        let subject = subject.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
        let body = "Just testing ...".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    else {
        print("Error: Can't encode subject or body.")
        return
    }
    
    let urlString = "mailto:\(toEmail)?subject=\(subject)&body=\(body)"
    let url = URL(string:urlString)!
    
    UIApplication.shared.open(url)
}

Swift 3.0:

func openMailApp() {
    
    let toEmail = "email@outlook.com"
    let subject = "Test email".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    let body = "Just testing ...".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    
    if 
      let urlString = "mailto:\(toEmail)?subject=\(subject)&body=\(body)",
      let url = URL(string:urlString) 
    {
        UIApplication.shared().openURL(url)
    }
}

Swift 2:

func openMailApp() {
    
    let toEmail = "email@outlook.com"
    let subject = "Test email".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()
    let body = "Just testing ...".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()
    
    if let
        urlString = ("mailto:\(toEmail)?subject=\(subject)&body=\(body)")),
        url = NSURL(string:urlString) {
        UIApplication.sharedApplication().openURL(url)
    }
}
VojtaStavik
  • 2,312
  • 1
  • 18
  • 30
  • 4
    This version actually does not work for me. It seems only the subject and the body should be encoded based on Apple's doc here: https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MailLinks/MailLinks.html So the right `urlString` in the answer should be `let urlString = "mailto:\(toEmail)?subject=\(subject.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)&body=\(body.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)"`. This code can be refactored to better format and safer code though. – Zhao May 19 '16 at 17:20
14

You can open the mail app without using opening the compose view by using the url scheme message://

Jesse S.
  • 783
  • 7
  • 11
11

Since the only way to launch other applications is by using their URL schemes, the only way to open mail is by using the mailto: scheme. Which, unfortunately for your case, will always open the compose view.

Briggs
  • 639
  • 5
  • 13
  • @cschuff even now that appears to be true. I just took another tour through the URL launching mechanism and any related info I could find. Still limited to launching with the compose view opened. – Thompsonian Mar 03 '14 at 23:24
  • 1
    unless no Mail accounts are configured, in which case it will open Mail with the default "Welcome to Mail" screen showing iCloud, Exchange, Google, Yahoo!, Aol., Outlook.com, and Other – Max MacLeod May 05 '15 at 08:27
  • 2
    Please see alternative answer which does successfully answer the question: http://stackoverflow.com/a/29212029/6426003 – Simon Pickup Dec 12 '16 at 08:58
9

Run your app on a real device and call

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"your@email.com"]];

Note, that this line takes no effect on simulator.

Display Name
  • 4,502
  • 2
  • 47
  • 63
8

You can launch any app on iOS if you know its URL scheme. Don't know that the Mail app scheme is public, but you can be sneaky and try this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"message:message-id"]];

Props to Farhad Noorzay for clueing me into this. It's some bit of reverse engineering the Mail app API. More info here: https://medium.com/@vijayssundaram/how-to-deep-link-to-ios-7-mail-6c212bc79bd9

Crazy Developer
  • 3,464
  • 3
  • 28
  • 62
Diogenes
  • 2,697
  • 3
  • 26
  • 27
  • Does this still work in iOS 11? It's only opening Mail for me, not the specific message. – T'Pol Jul 25 '18 at 23:58
5

Expanding on Amit's answer: This will launch the mail app, with a new email started. Just edit the strings to change how the new email begins.

//put email info here:
NSString *toEmail=@"supp0rt.fl0ppyw0rm@gmail.com";
NSString *subject=@"The subject!";
NSString *body = @"It is raining in sunny California!";

//opens mail app with new email started
NSString *email = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@", toEmail,subject,body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
Stan Tatarnykov
  • 691
  • 9
  • 17
3

If you are using Xamarin to developer an iOS application, here is the C# equivalent to open the mail application composer view:

string email = "yourname@companyname.com";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
3

Swift 4 / 5 to open default Mail App without compose view. If Mail app is removed, it automatically shows UIAlert with options to redownload app :)

UIApplication.shared.open(URL(string: "message:")!, options: [:], completionHandler: nil)
Pacyjent
  • 409
  • 5
  • 6
3

Swift 5 version:

if let mailURL = URL(string: "message:") {
    if UIApplication.shared.canOpenURL(mailURL) {
        UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
    }
}
Nadzeya
  • 641
  • 6
  • 16
2

On swift 2.3: open mailbox

UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)
Tim
  • 320
  • 1
  • 10
1

It will open Default Mail App with composer view:

NSURL* mailURL = [NSURL URLWithString:@"mailto://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}

It will open Default Mail App:

NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Sandip Patel - SM
  • 3,346
  • 29
  • 27
0

You might want to use a scripting bridge. I used this method in my App to directly give the user the option to send e-mail notifications using the built-in Mail.app. I also constructed an option to do this directly over SMTP as an alternate.

But since you want to use Mail.app method, you can find more information about how to do that solution by following this:

https://github.com/HelmutJ/CocoaSampleCode/tree/master/SBSendEmail

  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – Sabito stands with Ukraine Jan 14 '21 at 05:19
-5

In Swift:

let recipients = "someone@gmail.com"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)
Segev
  • 1,267
  • 16
  • 21