0

I am using the EASendMail SMTP component which I had to install and reference in my windows 8 desktop app. I am using this to send an email with some html content.

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

The above should send an email to the email address entered in a TextBox on the XAML page when a button is clicked

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

The code should send the email to whatever email address is enetered in email_txt when the button email_btn is clicked.

For this I put the textbox value in a varible var usermail = email_txt.ToString(); and call the variable usermail in oMail.To.Add(new MailAddress(usermail));. But with this I get the error below:

enter image description here

It doesn't recognize the email address in usermail.

However if I were to enter an email address directly into the code such as oMail.To.Add(new MailAddress("support@emailarchitect.net"));, it work fine and the email is sent to support@emailarchitect.net.

How do I fix this so that the email is sent to address specified in the TextBlock?

M4N
  • 94,805
  • 45
  • 217
  • 260
Tester
  • 2,887
  • 10
  • 30
  • 60

1 Answers1

1

I sorted it out. It was actually a pretty simple and careless error. The email value should be retrieved from the TextBox using var usermail = email_txt.Text.ToString();

I needed the Text value of the email TextBox.

Tester
  • 2,887
  • 10
  • 30
  • 60