简体   繁体   English

UWP电子邮件通知

[英]UWP email notifications

Im trying to build a uwp app that will notify people by email when they have successfully created an account. 我正在尝试构建一个uwp应用,当人们成功创建帐户后,该应用将通过电子邮件通知人们。 This will all be connected to a database etc. but for the meantime, how do I send an email ? 这些都将连接到数据库等,但是与此同时,我如何发送电子邮件?

Can someone show me a very basic method for sending an email. 有人可以告诉我一种发送电子邮件的非常基本的方法。 I can hardcode in the address and message for now, I just need to know how this works. 我现在可以在地址和消息中进行硬编码,我只需要知道它是如何工作的。 The only things I can find online are how to launch the mail app in windows 10. This is not what I want. 我只能在网上找到的唯一方法是如何在Windows 10中启动邮件应用程序。这不是我想要的。 I want to be able to email people automatically through code. 我希望能够通过代码自动向人们发送电子邮件。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks 谢谢

With the built-in UWP relevant Email APIs, you cannot send email through code directly. 使用内置的UWP相关电子邮件API,您无法直接通过代码发送电子邮件。 But you could use StreamSocket to emulate a SmtpClient and send email by programming. 但是您可以使用StreamSocket模拟SmtpClient并通过编程发送电子邮件。

I saw that Sebastien Pertus has achieved this in windows 8.1 app, but I've tried his code in UWP, it worked well. 我看到Sebastien Pertus在Windows 8.1应用程序中实现了此目的,但是我已经在UWP中尝试了他的代码,效果很好。 You could check his blog for reference: Sending an email within a Windows 8.1 application : using StreamSocket to emulate a SmtpClient 您可以查看他的博客以供参考: 在Windows 8.1应用程序中发送电子邮件:使用StreamSocket模拟SmtpClient

You can open the compose window of the mail app ( https://docs.microsoft.com/en-us/windows/uwp/contacts-and-calendar/sending-email ) 您可以打开邮件应用程序的撰写窗口( https://docs.microsoft.com/zh-cn/windows/uwp/contacts-and-calendar/sending-email

Example taken from that Link: 该链接中的示例:

private async Task ComposeEmail(Windows.ApplicationModel.Contacts.Contact recipient,
string subject, string messageBody)
{
    var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
    emailMessage.Body = messageBody;
    var email = recipient.Emails.FirstOrDefault<Windows.ApplicationModel.Contacts.ContactEmail>();
    if (email != null)
    {
        var emailRecipient = new Windows.ApplicationModel.Email.EmailRecipient(email.Address);
        emailMessage.To.Add(emailRecipient);
        emailMessage.Subject = subject;
    }

    await Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(emailMessage);
}

If you want the app to send the email directly, you need to connect to an smtp server. 如果您希望该应用程序直接发送电子邮件,则需要连接到smtp服务器。 See here https://stackoverflow.com/a/44184416/6409018 for how to do that. 有关如何执行此操作的信息,请参见https://stackoverflow.com/a/44184416/6409018

In short: You can use the nuget package from https://github.com/LightBuzz/SMTP-WinRT 简而言之:您可以从https://github.com/LightBuzz/SMTP-WinRT使用nuget包

using (SmtpClient client = new SmtpClient("smtp.example.com", 465, false, "SenderEmail@example.com", "YourPassword"))
{
     EmailMessage emailMessage = new EmailMessage();

     emailMessage.To.Add(new EmailRecipient("someone1@anotherdomain.com"));
     emailMessage.CC.Add(new EmailRecipient("someone2@anotherdomain.com"));
     emailMessage.Bcc.Add(new EmailRecipient("someone3@anotherdomain.com"));
     emailMessage.Subject = "Subject line of your message";
     emailMessage.Body = "This is an email sent from a UWP app!";

     await client.SendMailAsync(emailMessage);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM