发送带附件错误的电子邮件:无法从“Windows.Storage.StorageFile”转换为“字符串”。

时间:2017-11-10 05:39:57

标签: uwp smtp attachment storagefile

我正在尝试使用smtp通过webservice发送带附件的电子邮件,但是我遇到了这个错误:

'无法从'Windows.Storage.StorageFile'转换为'string'。 “

error

以下是按钮点击事件的代码

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


    }

遇到问题的一行就是这一行:

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

以下是我的网络服务发送电子邮件的代码:

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

1 个答案:

答案 0 :(得分:2)

您的方法sendEmailWithAttach(string toAddress, string subject, string body, string attachment)需要参数attachment的字符串类型。而您传递的是StorageFile类型的对象,您可以通过调用await folder.GetFileAsync(name);来获取

如果您想传递附件的文件路径,您可以这样做:

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