当前位置: 首页>>代码示例>>C#>>正文


C# SmtpClient.SendMailAsync方法代码示例

本文整理汇总了C#中SmtpClient.SendMailAsync方法的典型用法代码示例。如果您正苦于以下问题:C# SmtpClient.SendMailAsync方法的具体用法?C# SmtpClient.SendMailAsync怎么用?C# SmtpClient.SendMailAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SmtpClient的用法示例。


在下文中一共展示了SmtpClient.SendMailAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Send_Email

        public async Task Send_Email(string toMail, string toSubject, string toMessage)
        {
            String Result = "";
            try
            {
                SmtpMail oMail = new SmtpMail("TryIt");
                SmtpClient oSmtp = new SmtpClient();

                // Set your gmail email address
                oMail.From = new MailAddress("[email protected]");

                // Add recipient email address, please change it to yours
                oMail.To.Add(new MailAddress(toMail));

                // Set email subject and email body text
                oMail.Subject = toSubject;
                oMail.TextBody = toMessage;

                // Gmail SMTP server
                SmtpServer oServer = new SmtpServer("smtp.gmail.com");

                // User and password for ESMTP authentication           
                oServer.User = "[email protected]";
                oServer.Password = "123123fv";

                // Enable TLS connection on 25 port, please add this line
                oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                await oSmtp.SendMailAsync(oServer, oMail);
                Result = "Email was sent successfully!";
            }
            catch (Exception ep)
            {
                Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
            }

            // Display Result by Diaglog box
            Windows.UI.Popups.MessageDialog dlg = new
                Windows.UI.Popups.MessageDialog(Result);

            await dlg.ShowAsync();



        }
开发者ID:CasperGuldbechNielsen,项目名称:FranceVacancesProject,代码行数:45,代码来源:email.cs

示例2: TestMailDeliveryAsync

        public void TestMailDeliveryAsync()
        {
            SmtpServer server = new SmtpServer();
            SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port);
            MailMessage msg = new MailMessage("[email protected]", "[email protected]", "hello", "howdydoo");

            Thread t = new Thread(server.Run);
            t.Start();
            Task task = client.SendMailAsync(msg);
            t.Join();

            server.Stop();

            Assert.Equal("<[email protected]>", server.MailFrom);
            Assert.Equal("<[email protected]>", server.MailTo);
            Assert.Equal("hello", server.Subject);
            Assert.Equal("howdydoo", server.Body);

            Assert.True(task.Wait(1000));
        }
开发者ID:AtsushiKan,项目名称:corefx,代码行数:20,代码来源:SmtpClientTest.cs

示例3: btnSend_Tapped


//.........这里部分代码省略.........
                {
                    MessageDialog dlg = new MessageDialog("Please input password!");
                    await dlg.ShowAsync();
                    textPassword.Password = "";
                    textPassword.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                    return;
                }
            }

            btnSend.IsEnabled = false;
            pgBar.Value = 0;

            try
            {
                SmtpClient oSmtp = new SmtpClient();

                oSmtp.Authorized +=  OnAuthorized;
                oSmtp.Connected += OnConnected;
                oSmtp.Securing +=  OnSecuring;
                oSmtp.SendingDataStream += OnSendingDataStream;

                SmtpServer oServer = new SmtpServer(textServer.Text);
                bool bSSL = (chkSSL.IsChecked.HasValue) ? (bool)chkSSL.IsChecked : false;
                if (bSSL)
                {
                    oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
                }

                oServer.Protocol = (ServerProtocol)lstProtocols.SelectedIndex;

                if (bAuth)
                {
                    oServer.User = textUser.Text;
                    oServer.Password = textPassword.Password;
                }

                // For evaluation usage, please use "TryIt" as the license code, otherwise the 
                // "Invalid License Code" exception will be thrown. However, the trial object only can be used 
                // with developer license

                // For licensed usage, please use your license code instead of "TryIt", then the object
                // can used with published windows store application.
                SmtpMail oMail = new SmtpMail("TryIt");

                oMail.From = new MailAddress(textFrom.Text);

                // If your Exchange Server is 2007 and used Exchange Web Service protocol, please add the following line;
                // oMail.Headers.RemoveKey("From");
                oMail.To = new AddressCollection(textTo.Text);
                oMail.Cc = new AddressCollection(textCc.Text);
                oMail.Subject = textSubject.Text;

                if (lstFormat.SelectedIndex == 0)
                {
                    oMail.TextBody = textBody.Text;
                }
                else
                {
                    if (chkHtml.IsChecked.HasValue)
                    {
                        if ((bool)chkHtml.IsChecked)
                        {
                            editorMenu.Visibility = Windows.UI.Xaml.Visibility.Visible;
                            await htmlEditor.InvokeScriptAsync("OnViewHtmlSource", new string[] { "false" });
                            chkHtml.IsChecked = true;
                        }
                    }
                    string html = await htmlEditor.InvokeScriptAsync("getHtml", null);
                    html = "<html><head><meta charset=\"utf-8\" /></head><body style=\"font-family:Calibri;font-size: 15px;\">" + html + "<body></html>";
                    await oMail.ImportHtmlAsync(html,
                        Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
                        ImportHtmlBodyOptions.ErrorThrowException | ImportHtmlBodyOptions.ImportLocalPictures
                        | ImportHtmlBodyOptions.ImportHttpPictures | ImportHtmlBodyOptions.ImportCss);
                }

                int count = m_atts.Count;
                for (int i = 0; i < count; i++)
                {
                    await oMail.AddAttachmentAsync(m_atts[i]);
                }
                btnCancel.IsEnabled = true;

                textStatus.Text = String.Format("Connecting {0} ...", oServer.Server);
                // You can genereate a log file by the following code.
                // oSmtp.LogFileName = "ms-appdata:///local/smtp.txt";
                asyncCancel = oSmtp.SendMailAsync(oServer, oMail);
                await asyncCancel;

                textStatus.Text = "Completed";

            }
            catch (Exception ep)
            {
                textStatus.Text = "Error:  " + ep.Message;
            }

            asyncCancel = null;
            btnSend.IsEnabled = true;
            btnCancel.IsEnabled = false;
        }
开发者ID:DirkViljoen,项目名称:eSalon,代码行数:101,代码来源:MainPage.xaml.cs

示例4: SubmitMail

        private async Task SubmitMail(
             SmtpServer oServer, SmtpMail oMail, string[] atts,
             string bodyText, bool htmlBody, int index )
        {
           
            SmtpClient oSmtp = null;
            try
            {
                oSmtp = new SmtpClient();
               // oSmtp.TaskCancellationToken = m_cts.Token;
               // oSmtp.Authorized += new SmtpClient.OnAuthorizedEventHandler(OnAuthorized);
                oSmtp.Connected += OnConnected;
              //  oSmtp.Securing += new SmtpClient.OnSecuringEventHandler(OnSecuring);
                //oSmtp.SendingDataStream +=
                  //  new SmtpClient.OnSendingDataStreamEventHandler(OnSendingDataStream);

                UpdateRecipientItem(index, "Preparing ...");
               
                if ( !htmlBody )
                {
                    oMail.TextBody = bodyText;
                }
                else
                {
                    string html = bodyText;
                    html = "<html><head><meta charset=\"utf-8\" /></head><body style=\"font-family:Calibri;font-size: 15px;\">" + html + "<body></html>";
                    await oMail.ImportHtmlAsync(html,
                        Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
                        ImportHtmlBodyOptions.ErrorThrowException | ImportHtmlBodyOptions.ImportLocalPictures
                        | ImportHtmlBodyOptions.ImportHttpPictures | ImportHtmlBodyOptions.ImportCss);
                }

                int count = atts.Length;
                for (int i = 0; i < count; i++)
                {
                    await oMail.AddAttachmentAsync(atts[i]);
                }


                UpdateRecipientItem(index, String.Format("Connecting {0} ...", oServer.Server));
                oSmtp.Tag = index;

                // You can genereate a log file by the following code.
                // oSmtp.LogFileName = "ms-appdata:///local/smtp.txt";

                IAsyncAction asynCancelSend = oSmtp.SendMailAsync(oServer, oMail);
                m_cts.Token.Register(() => asynCancelSend.Cancel());
                await asynCancelSend;

                Interlocked.Increment(ref m_success);
                UpdateRecipientItem(index, "Completed");

            }
           catch (Exception ep)
            {
                oSmtp.Close();
                string errDescription = ep.Message;
                UpdateRecipientItem(index, errDescription);
                Interlocked.Increment(ref m_failed);
            }

        }
开发者ID:DirkViljoen,项目名称:eSalon,代码行数:62,代码来源:MainPage.xaml.cs

示例5: TestCredentialsCopyInAsyncContext

        public async void TestCredentialsCopyInAsyncContext()
        {
            SmtpServer server = new SmtpServer();
            SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port);
            MailMessage msg = new MailMessage("[email protected]", "[email protected]", "hello", "howdydoo");

            CredentialCache cache = new CredentialCache();
            cache.Add("localhost", server.EndPoint.Port, "NTLM", CredentialCache.DefaultNetworkCredentials);

            client.Credentials = cache;

            try
            {
                Thread t = new Thread(server.Run);
                t.Start();
                await client.SendMailAsync(msg);
                t.Join();

                Assert.Equal("<[email protected]>", server.MailFrom);
                Assert.Equal("<[email protected]>", server.MailTo);
                Assert.Equal("hello", server.Subject);
                Assert.Equal("howdydoo", server.Body);
            }
            finally
            {
                server.Stop();
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:28,代码来源:SmtpClientTest.cs


注:本文中的SmtpClient.SendMailAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。