我正在尝试创建一个简单的联系表格,用户可以在其中填写他们的详细信息并上传文件。提交表单后,该文件将附加到联系人电子邮件中,并上传到我服务器上的目录。我一直在关注 .NET Web Forms 文档,我可以发送电子邮件并上传到我的服务器,但是电子邮件中的附件失败了,我不明白为什么!
这是我的代码,提交后我收到消息“错误 1”:
@{
var fileName = "";
<form method="POST" enctype="multipart/form-data" id="form">
<input type="text" id="name" name="name" />
<input type="email" id="email" name="email" />
<input type="file" name="fileAttachment" />
<input type="submit" value="submit" />
</form>
if(IsPost) {
var fileSavePath = "";
var msg = "";
try{
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/MyDirectory/" + fileName);
if (uploadedFile.ContentLength > 1120000) {
msg = "The maximum file size is 5 MB";
<p>@msg</p>
}else{
uploadedFile.SaveAs(fileSavePath);
var name = Request.Form["name"];
var email = Request.Form["email"];
var fileAttachment = Request["fileAttachment"];
if( !name.IsEmpty() || !email.IsEmpty())
{
string message = string.Format("Name: {0}<br/><br/>Email: {1}"
, name
, email
);
try{
WebMail.SmtpServer = "127.0.0.1";
var filesList = new string [] { fileAttachment };
WebMail.Send(
from: "no-reply@mydomain.com",
to: "me@mydomain.com",
subject: "File attached",
body: message,
filesToAttach: filesList
);
}catch (Exception ex ){
<p>Error 1</p>
}
}
}
}catch(HttpException ex){
msg = "Error 2";
<p>@msg</p>
}
}
}
谁能明白为什么这不起作用?