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

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

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

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

这是我的按钮点击事件的代码:

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

这是我的 web 服务发送电子邮件的代码:

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

您的方法 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);