我有一个 [WebMethod] Sendemail 这工作正常,但现在我要升级它以发送附件。我正在使用文件上传。这是我的方法调用。
lblEmailSent.Text = Send.Sendemail(txtTo.Text, txtSubject.Text, txtbody.Text, FileUpload1.PostedFile.FileName, FileUpload1.FileContent);
我的呼叫声明以蓝色下划线显示,给出的两个错误如下所示:
* 1) * 'WebTestServiceApp.localhost.Service1.Sendemail(string, string, string, string, WebTestServiceApp.localhost.Stream)' 的最佳重载方法匹配有一些无效参数
* 2) *参数 5:无法从 'System.IO.Stream' 转换为 'WebTestServiceApp.localhost.Stream'
FileUpload1.PostedFile.FileName 作为字符串传递 FileUpload1.FileContent 作为流传递
这是我的 [WebMethod],现在你们都可以看到我能看到的一切,我看不出有什么问题,但我不确定 FileUpload1.FileContent 是否应该作为流传递。
[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" ;
}
}