URL Scheme Attachment Microsoft Outlook 应用程序

3
我正在尝试制作一个应用程序,生成一个文件并填充所有电子邮件字段,以便用户只需输入正文即可。我还为用户提供了选择使用本机iOS电子邮件应用程序或Microsoft Outlook应用程序(如果已安装)的可能性。
当我将此实现为在本机电子邮件应用程序中准备要发送的电子邮件时,我使用了MessageUI框架,这使得附加文件变得容易,但是对于Outlook应用程序,我必须使用URL方案(ms-outlook://),并且似乎没有简单的方法(或根本没有方法)来附加文件。
是否有人成功地通过Outlook应用程序从另一个应用程序发送附件?

你能找到任何解决方案吗? - Mohammed Akhtar Zuberi
还没有。我已经向Outlook团队提出了这个问题,但不幸的是他们目前不支持此功能。 - tx2
我自己成功解决了。我应该在这里发布解决方案吗? - Mohammed Akhtar Zuberi
1个回答

3
我基于“有总比没有好”的理念回答这个问题。我知道在 iOS 应用上无法预先附加文件发送邮件,因此我设法找到了一种方法,至少可以在电子邮件中发送图像文件。
// Create an array of recipients for the email.
NSArray* emailRecipients = @[@"example@email.com", @"example2@email.com"];

// Create a mutable string to hold all of the recipient email addresses and add the first one.
NSMutableString* emailTo = [[NSMutableString alloc] initWithString:emailRecipients[0]];

// Loop through all of the email recipients except for the first one.
for (int index = 1; index < emailRecipients.count; index++)
{
    // Add a semicolon and then the email address at the current index.
    [emailTo appendFormat:@";%@", emailRecipients[index]];
}

// Get the email subject from the subject text field.
NSString *emailSubject = @"Your Email Subject";

// Encode the string for URL.
NSString *encodedSubject = [emailSubject stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

// Define your image's size
NSString *htmlBody = (@"<div style=\"width:450px;height:797px;\"><img src=\"http://your_website.com/your_image.jpg\" style=\"width:100%;height:100%;\"></div>");

// Encode the string for URL.
NSString* encodedBody = [htmlBody stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

// See if the subject or body are empty.
if (![emailSubject length] || ![emailBody length])
{
    // Exit.
    return;
}

// Create a string with the URL scheme and email properties.
NSString *stringURL = [NSString stringWithFormat:@"ms-outlook://compose?to=%@&subject=%@&body=%@", emailTo, encodedSubject, encodedBody];
// Convert the string to a URL.
NSURL *url = [NSURL URLWithString:stringURL];
// Open the app that responds to the URL scheme (should be Outlook).
[[UIApplication sharedApplication] openURL:url];

这将轻松地在电子邮件正文中嵌入图像文件。您可能需要根据您的图片调整大小。


多个收件人需要用逗号分隔,而不是分号。我刚刚修复了我的应用程序中的一个错误,这个错误是由于在 iOS 上使用分号而不是逗号导致的 Outlook。 - Crake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接