Launch Apple Mail App from within my own App?
Asked Answered
H

15

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?

Handily answered 11/1, 2012 at 15:31 Comment(1)
did you try imap or pop instead of mailto?Jody
D
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];
}
Divebomb answered 23/3, 2015 at 13:53 Comment(7)
Wondering if the Slack app uses this technique. They are launching without a mail compose viewKristelkristen
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.Impending
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.Kemeny
@AlexMM, I have not tried that, but if you have message url + attachment url that might workDivebomb
According to this post 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!Kemeny
It may be worth a separate question, I am not sure.Divebomb
This is the accepted answer, but still it's undocumented by Apple in developer.apple.com/library/archive/featuredarticles/… so I'm not really sure it should be the best answer.Deadbeat
A
48
NSString *recipients = @"mailto:[email protected][email protected],[email protected]&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]];
Affined answered 11/1, 2012 at 15:54 Comment(3)
This opens in compose view.Emilyemina
Not working in iOS 11. Can any one help me with this?Withrow
@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.Southland
J
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 protected]"
    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 protected]"
    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)
    }
}
Jumbuck answered 3/8, 2015 at 12:35 Comment(1)
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: developer.apple.com/library/ios/featuredarticles/… 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.Veronicaveronika
C
14

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

Causalgia answered 17/3, 2017 at 15:5 Comment(0)
E
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.

Estefanaestel answered 11/1, 2012 at 15:38 Comment(3)
@Handily 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.Classify
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 OtherDrin
Please see alternative answer which does successfully answer the question: https://mcmap.net/q/121816/-launch-apple-mail-app-from-within-my-own-appGooey
A
9

Run your app on a real device and call

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"[email protected]"]];

Note, that this line takes no effect on simulator.

Attune answered 7/10, 2014 at 7:24 Comment(1)
+1 for noting that this doesn't work on the simulatorBookplate
R
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

Roney answered 3/4, 2015 at 22:45 Comment(1)
Does this still work in iOS 11? It's only opening Mail for me, not the specific message.Idle
W
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=@"[email protected]";
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]];
Wards answered 8/3, 2015 at 14:56 Comment(0)
K
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 = "[email protected]";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);
Kirt answered 17/9, 2014 at 19:51 Comment(0)
C
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)
Cockade answered 7/3, 2019 at 12:38 Comment(0)
P
3

Swift 5 version:

if let mailURL = URL(string: "message:") {
    if UIApplication.shared.canOpenURL(mailURL) {
        UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
    }
}
Pythagoras answered 4/10, 2019 at 15:31 Comment(0)
O
2

On swift 2.3: open mailbox

UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)
Oink answered 17/1, 2017 at 11:46 Comment(0)
P
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];
}
Pentagon answered 7/2, 2019 at 12:5 Comment(0)
M
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

Most answered 29/12, 2018 at 3:38 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link 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.Monia
M
-5

In Swift:

let recipients = "[email protected]"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)
Milkwhite answered 23/11, 2014 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.