在Windows Universal App中发送电子邮件

时间:2014-07-09 06:33:14

标签: c# windows windows-phone-8 win-universal-app

是否可以在适用于Windows 8.1和Windows Phone 8.1的Windows Universal App中发送电子邮件?

await Launcher.LaunchUriAsync(new Uri("mailto:abc@abc.com?subject=MySubject&body=MyContent"));

使用此代码行,我可以发送电子邮件,但我想发送带附件的邮件。

4 个答案:

答案 0 :(得分:5)

由于Microsoft错过了将EmailMessage和EmailManager添加到Windows应用商店库,似乎只有两个不满意的解决方案:您可以使用共享或通过mailto协议发起电子邮件发送。我是这样做的:

  /// <summary>
  /// Initiates sending an e-mail over the default e-mail application by 
  /// opening a mailto URL with the given data.
  /// </summary>
  public static async void SendEmailOverMailTo(string recipient, string cc, 
     string bcc, string subject, string body)
  {
     if (String.IsNullOrEmpty(recipient))
     {
        throw new ArgumentException("recipient must not be null or emtpy");
     }
     if (String.IsNullOrEmpty(subject))
     {
        throw new ArgumentException("subject must not be null or emtpy");
     }
     if (String.IsNullOrEmpty(body))
     {
        throw new ArgumentException("body must not be null or emtpy");
     }

     // Encode subject and body of the email so that it at least largely 
     // corresponds to the mailto protocol (that expects a percent encoding 
     // for certain special characters)
     string encodedSubject = WebUtility.UrlEncode(subject).Replace("+", " ");
     string encodedBody = WebUtility.UrlEncode(body).Replace("+", " ");

     // Create a mailto URI
     Uri mailtoUri = new Uri("mailto:" + recipient + "?subject=" +
        encodedSubject +
        (String.IsNullOrEmpty(cc) == false ? "&cc=" + cc : null) +
        (String.IsNullOrEmpty(bcc) == false ? "&bcc=" + bcc : null) +
        "&body=" + encodedBody);

     // Execute the default application for the mailto protocol
     await Launcher.LaunchUriAsync(mailtoUri);
  }

答案 1 :(得分:1)

Windows Phone 8.1

您可以使用以下内容发送带附件的电子邮件:

var email = new EmailMessage(); 
email.To = ...;
email.Body = ...;
email.Attachments.Add( ... );
var ignore = EmailManager.ShowComposeNewEmailAsync(email);

Windows 8.1

在Windows 8.1上,遗憾的是,无法发送带附件的电子邮件。 mailto 协议就是您所拥有的,并且它不会正式支持附件。但是,您可以按以下方式添加附件:

  

mailto:xxx@xxx.com?subject = xxx&amp; body = xxx&amp; attach = C:\ path \ to \ file

  

mailto:xxx@xxx.com?subject = xxx&amp; body = xxx&amp; Attachment = C:\ path \ to \ file

但是由客户决定是否会处理附件。有关详细信息https://msdn.microsoft.com/en-us/library/aa767737(v=vs.85).aspx

,请参阅此主题

答案 2 :(得分:0)

发送带附件的电子邮件您需要使用EmailMessageEmailManager类。

<强> 1。 EmailMessage:

EmailMessage类定义将要发送的实际电子邮件。您可以指定收件人(收件人,抄送,不列颠哥伦比亚省),主题和电子邮件正文。

<强> 2。 EmailManager:

EmailManager类在Windows.ApplicationModel.Email命名空间中定义。 EmailManager类提供了一个静态方法ShowComposeNewEmailAsync,它接受EmailMessage作为参数。 ShowComposeNewEmailAsync将使用EmailMessage启动撰写电子邮件屏幕,该屏幕允许用户发送电子邮件。

您可以在windows-phone-8-1-and-windows-runtime-apps-how-to-2-send-emails-with-attachment-in-wp-8-1

找到更多参考资料

答案 3 :(得分:0)

在此页面上:https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871373.aspx

Microsoft提供了一个如何与您的应用程序共享的示例。下载源示例应用:http://go.microsoft.com/fwlink/p/?linkid=231511

打开解决方案并将文件:test.txt添加到项目根目录。

然后打开ShareFiles.xaml.cs并将该类替换为:

public sealed partial class ShareText : SDKTemplate.Common.SharePage
{
    public ShareText()
    {
        this.InitializeComponent();
        LoadList();
    }

    List<Windows.Storage.IStorageItem> list { get; set; }

    public async void LoadList()
    {
        var uri = new Uri("ms-appx:///test.txt");
        var item = await StorageFile.GetFileFromApplicationUriAsync(uri);
        list = new List<IStorageItem>();
        list.Add(item);
    }


    protected override bool GetShareContent(DataRequest request)
    {
        bool succeeded = false;

        string dataPackageText = TextToShare.Text;
        if (!String.IsNullOrEmpty(dataPackageText))
        {
            DataPackage requestData = request.Data;
            requestData.Properties.Title = TitleInputBox.Text;
            requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
            requestData.Properties.ContentSourceApplicationLink = ApplicationLink;
            requestData.SetText(dataPackageText);
            requestData.SetStorageItems(list);
            succeeded = true;
        }
        else
        {
            request.FailWithDisplayText("Enter the text you would like to share and try again.");
        }
        return succeeded;
    }

}

可能不是最好的代码,但它帮助了我:)。