在C#中使用TextBox值发送电子邮件

时间:2013-12-04 14:44:07

标签: c# email visual-studio-2012 windows-8 easendmail

我正在使用我必须在我的Windows 8桌面应用中安装和引用的EASendMail SMTP组件。我使用它发送一封包含一些HTML内容的电子邮件。

private async void btnSend_Click(object sender, RoutedEventArgs e)
        {
            btnSend.IsEnabled = false;
            await Send_Email();
            btnSend.IsEnabled = true;
        }

        private async Task Send_Email()
        {
            var usermail = email_txt.ToString();
            String Result = "";
            try
            {
                SmtpMail oMail = new SmtpMail("TryIt");
                SmtpClient oSmtp = new SmtpClient();

                // Set sender email address, please change it to yours
                oMail.From = new MailAddress("test@emailarchitect.net");

                // Add recipient email address, please change it to yours
               // oMail.To.Add(new MailAddress("support@emailarchitect.net"));
                oMail.To.Add(new MailAddress(usermail));

                // Set email subject
                oMail.Subject = "test email from C# XAML project with file attachment";

                // Set Html body
                oMail.HtmlBody = "<font size=5>This is</font> <font color=red><b>a test</b></font>";

                // get a file path from PicturesLibrary, 
                // to access files in PicturesLibrary, you MUST have "Pictures Library" checked in
                // your project -> Package.appxmanifest -> Capabilities
                Windows.Storage.StorageFile file = 
                    await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("test.jpg");

                string attfile = file.Path;
                Attachment oAttachment = await oMail.AddAttachmentAsync(attfile);

                // if you want to add attachment from remote URL instead of local file.
                // string attfile = "http://www.emailarchitect.net/test.jpg";
                // Attachment oAttachment = await oMail.AddAttachmentAsync(attfile);

                // you can change the Attachment name by
                // oAttachment.Name = "mytest.jpg";

                // Your SMTP server address
                SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

                // User and password for ESMTP authentication            
                oServer.User = "test@emailarchitect.net";
                oServer.Password = "testpassword";

                // If your SMTP server requires TLS connection on 25 port, please add this line
                // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                // If your SMTP server requires SSL connection on 465 port, please add this line
                // oServer.Port = 465;
                // 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();
        }

上面的内容应该会在点击按钮时向XAML页面上的TextBox中输入的电子邮件地址发送电子邮件

<TextBox x:Name="email_txt"></TextBox>
<Button x:Name="email_btn" Content="Emial Me"  Click="email_btn_Click"/>

当点击按钮email_txt时,代码应将电子邮件发送到email_btn中的任何电子邮件地址。

为此,我将文本框值放在变量var usermail = email_txt.ToString();中,并在usermail中调用变量oMail.To.Add(new MailAddress(usermail));。但是有了这个,我得到以下错误:

enter image description here

它无法识别usermail中的电子邮件地址。

但是,如果我直接在oMail.To.Add(new MailAddress("support@emailarchitect.net"));等代码中输入电子邮件地址,则可以正常工作,并将电子邮件发送至support@emailarchitect.net。

如何解决此问题,以便将电子邮件发送到TextBlock中指定的地址?

1 个答案:

答案 0 :(得分:1)

我整理出来了。这实际上是一个非常简单和粗心的错误。应使用var usermail = email_txt.Text.ToString();

从TextBox中检索电子邮件值

我需要电子邮件TextBox的Text值。