Search code examples
uwpsmtpattachmentstoragefile

Sending email with attachment error : Cannot convert from 'Windows.Storage.StorageFile' to 'string'.


I am trying to send an email with attachment using smtp through webservice, however I encountered this error:

'Cannot convert from 'Windows.Storage.StorageFile' to 'string'. '

error

Here is the code for my button click event:

 private async void btn_send_Click(object sender, RoutedEventArgs e)
    {
        string name = "test.jpg";
        var folder = KnownFolders.PicturesLibrary;
        var file = await folder.GetFileAsync(name);


        CitiKioskService.Service1Client kioskclient = new CitiKioskService.Service1Client();
        kioskclient.Endpoint.Address = new EndpointAddress(new Uri(new SettingsViewModel().KioskWebServiceURL));
        string email_address = tb_email.Text;
        string message = "Dear visitor, the following attachment is the photo taken during your visit. Thank You. This is a system generated email. Please do not reply to this email";
        await kioskclient.sendEmailWithAttachAsync(email_address, "Visit Photo", message, file);


    }

The line that is having problem is this line:

await kioskclient.sendEmailWithAttachAsync(email_address, "Visit Photo", message, file);

Here is the code for my webservice for sending the email:

public List<string> sendEmailWithAttach(string toAddress, string subject, string body, string attachment)
    {
        List<string> message = new List<string>();
        String msg = "";
        try
        {
            SmtpMail oMail = new SmtpMail("TryIt");
            EASendMail.SmtpClient oSmtp = new EASendMail.SmtpClient();

            // Set your hotmail/live/outlook.com email address
            oMail.From = new EASendMail.MailAddress("live@hotmail.com");

            // Add recipient email address
            oMail.To.Add(new EASendMail.MailAddress(toAddress));

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


            // Send attachment 
            string attfile = attachment;
            EASendMail.Attachment oAttachment = oMail.AddAttachment(attfile);

            // Hotmail SMTP server
            SmtpServer oServer = new SmtpServer("smtp.live.com");

            // User and password for ESMTP authentication
            oServer.User = "live@hotmail.com";
            oServer.Password = "password";


            oServer.Port = 25;

            // detect SSL/TLS type automatically
            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

            oSmtp.SendMail(oServer, oMail);
            msg = "Email was sent successfully!";

        }
        catch (SmtpFailedRecipientException ex)
        {
            msg = ex.GetBaseException().ToString();
            message.Add(ex.GetBaseException().ToString());
        }

        return message;
    }

Solution

  • Your method sendEmailWithAttach(string toAddress, string subject, string body, string attachment) needs a string type for the argument attachment. Whereas you're passing an object of type StorageFile, which you get by calling await folder.GetFileAsync(name);

    If you want to pass the file path of the attachment you can do:

    await kioskclient.sendEmailWithAttachAsync(email_address, "Visit Photo", message, file.Path);