0

我正在尝试通过 web 服务使用 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);

这是我用于发送电子邮件的网络服务的代码:

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

1 回答 1

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);
于 2017-11-10T06:38:07.620 回答