How to send email directly to user using C# in Windows 10 Apps?

流过昼夜 提交于 2019-12-07 07:23:34

问题


I have following code

 EmailMessage email = new EmailMessage();
 email.To.Add(new EmailRecipient("xyz@live.com"));
 email.Subject = "Msg Subject ";
 email.Body = "My Msg";
 await EmailManager.ShowComposeNewEmailAsync(email);

showComposeNewEmailAsync() launches the email application with above message displayed, but I want to sent the email directly to the user without launching the email application. How can I do it?


As I am new to coding please explain me in details.


回答1:


You have to work with Sockets and implement the SMTP behavior by yourself.


Otherwise you could also use a SMTP client for WinRT. According to the Microsoft Forum this should also work with UWP.




回答2:


Use SMTP client for Windows Store as follows:

First install this Nuget Package:

Install-Package lightbuzz-smtp

Second include namespace

using LightBuzz.SMTP;

Then you can use this code snippet to send email directly from UWP app:

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);
}


来源:https://stackoverflow.com/questions/34290921/how-to-send-email-directly-to-user-using-c-sharp-in-windows-10-apps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!