The Correct way to pass FileUpload content into [W

2019-03-03 07:09发布

I have a [WebMethod] Sendemail This works fine but now i what to upgrade it to send attachments. I am using a fileupload. This is my method Call.

lblEmailSent.Text = Send.Sendemail(txtTo.Text, txtSubject.Text, txtbody.Text, FileUpload1.PostedFile.FileName, FileUpload1.FileContent);

My Call Statement is underlined in blue and the two error given look like:

*1)*The best overloaded method match for 'WebTestServiceApp.localhost.Service1.Sendemail(string, string, string, string, WebTestServiceApp.localhost.Stream)' has some invalid arguments

*2)*Argument 5: cannot convert from 'System.IO.Stream' to 'WebTestServiceApp.localhost.Stream'

FileUpload1.PostedFile.FileName is passed as a String FileUpload1.FileContent is passed as a stream

This is my [WebMethod], Now u all can see every thing i can see, i can't spot anything wrong, But i am unsure if FileUpload1.FileContent should be passed as a Stream.

[WebMethod]
public string Sendemail(String inValueTo, String inValueSub, String inValueBody, String inValueAttachmentPostedfile, Stream inValueAttachemtnFileContent) //, String inValueAttachmentPostedfile, Stream inValueAttachemtnFileContent
{
    try
    {
        String valueTo = inValueTo;
        String valueSub = inValueSub;
        String valueBody = inValueBody;
        String valueAttachmentPostedfile = inValueAttachmentPostedfile; //FileUpload1.PostedFile.FileName
        Stream valueAttachmentFileContent = inValueAttachemtnFileContent;  //FileUpload1.FileContent.fileName

        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); // Creating new message.

        message.To.Add(valueTo);
        message.Subject = valueSub;
        message.From = new System.Net.Mail.MailAddress("shaunmossop@mweb.co.za");
        message.Body = valueBody;
        message.IsBodyHtml = true;

          string fileName = Path.GetFileName(valueAttachmentPostedfile); // Get attachment file
         Attachment myAttachment =
                         new Attachment(valueAttachmentFileContent, fileName);
          if (fileName != "")
          {
              message.Attachments.Add(myAttachment); // Send attachment
          }

        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com"); //Properties.Settings.Default.MailSMTPServer

        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;

        NetworkCredential netC = new NetworkCredential(Properties.Settings.Default.username, Properties.Settings.Default.password); // Useing Projects defult settings.
        smtp.Credentials = netC;
        smtp.Send(message);

        return "Message has been sent";
    }
    catch (Exception)
    {
        return "Message faild to send" ;

    }
}

1条回答
仙女界的扛把子
2楼-- · 2019-03-03 07:50

the second error it gives you hits the nail on the head, yes youre passing string string string string but youre passing the wrong type of stream for it to interact with, it wants this kind WebTestServiceApp.localhost.Stream youre giving it this kind System.IO.Stream

when you use the stream try explicitly declaring it as a WebTestServiceApp.localhost.Stream when you first make it, that way youre then passing the right types of stream and your problem should stop!

theres two types of stream you see, one used for web apps and one used for desktop apps.

查看更多
登录 后发表回答