我有一個[WebMethod] Sendemail這工作正常,但現在我要升級它來發送附件。我正在使用文件上傳。這是我的方法呼叫。將FileUpload內容傳遞給[WebMethod]的正確方法?
lblEmailSent.Text = Send.Sendemail(txtTo.Text, txtSubject.Text, txtbody.Text, FileUpload1.PostedFile.FileName, FileUpload1.FileContent);
我的CALL語句中帶下劃線的藍色和兩個錯誤給出的樣子:
* 1) *爲「WebTestServiceApp.localhost.Service1.Sendemail的最佳重載的方法匹配(串,字符串, 字符串,字符串,WebTestServiceApp.localhost.Stream) '有一些無效 參數
* 2) *參數5:不能從轉換' System.IO.Stream」到 'WebTestServiceApp.localhost.Stream'
FileUpload1.PostedFile.FileName作爲字符串FileUpload1.FileContent通過作爲流過
這是我的[的WebMethod],現在所有人都可以看到我能看到的每一件事,我無法發現任何錯誤,但我不確定FileUpload1.FileContent是否應該作爲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("[email protected]");
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" ;
}
}