我在我的mvc網站上有兩種形式,FeedbackForm和CareerForm。我需要將這兩個表單發送到同一封電子郵件。我創建了兩個模型,我的形式和兩種觀點,然後我在我的第一控制器附件未附加到電子郵件asp.net mvc
/*Feedback*/
[HttpGet]
public ActionResult Feedback(string ErrorMessage)
{
if (ErrorMessage != null)
{
}
return View();
}
[HttpPost]
public ActionResult Feedback(FeedbackForm Model)
{
string ErrorMessage;
//email
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = MailPriority.High;
msg.From = new MailAddress(Model.Email, Model.Name);
msg.To.Add("[email protected]");
msg.Subject = @Resources.Global.Feedback_Email_Title + " " + Model.Company;
string message = @Resources.Global.Feedback_Email_From + " " + Model.Name + "\n"
+ @Resources.Global.Feedback_Email + " " + Model.Email + "\n"
+ @Resources.Global.Feedback_Phone + " " + Model.Phone + "\n"
+ @Resources.Global.Feedback_Company + " " + Model.Company + "\n\n"
+ Model.AdditionalInformation;
msg.Body = message;
msg.IsBodyHtml = false;
//Attachment
if (Model.ProjectInformation != null && !(String.IsNullOrEmpty(Model.ProjectInformation.FileName)))
{
HttpPostedFileBase attFile = Model.ProjectInformation;
if (attFile.ContentLength > 0)
{
var attach = new Attachment(attFile.InputStream, attFile.FileName);
msg.Attachments.Add(attach);
}
}
SmtpClient client = new SmtpClient("denver.corepartners.local", 55);
client.UseDefaultCredentials = false;
client.EnableSsl = false;
try
{
client.Send(msg);
}
catch (Exception ex)
{
return RedirectToAction("Feedback", "Home", ErrorMessage = "Ошибка при отправке письма, попробуйте позже");
}
return RedirectToAction("Feedback", "Home");
}
加入和加入到第二控制器
/*CareerForm*/
[HttpGet]
public ActionResult CareerForm()
{
CareerForm model = new CareerForm();
model.StartNow = true;
model.EmploymentType = new List<CheckBoxes>
{
new CheckBoxes { Text = "полная занятость" },
new CheckBoxes { Text = "частичная занятость" },
new CheckBoxes { Text = "контракт" }
};
return View(model);
}
[HttpPost]
public ActionResult CareerForm(CareerForm Model)
{
string ErrorMessage;
//curricula vitae to email
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = MailPriority.Normal;
msg.From = new MailAddress(Model.Email, Model.Name + " " + Model.Surname);
msg.To.Add("[email protected]");
msg.Subject = "Анкета с сайта";
string message = "Имя: " + Model.Name + " " + Model.Surname + "\n"
+ "Контактный телефон: " + Model.Phone + "\n";
if (Model.Adress != null)
{
message += "Адрес: " + Model.Adress + "\n";
}
message += "Email: " + Model.Email + "\n"
+ "Желаемая должность: " + Model.Position;
bool check = false;
foreach (var item in Model.EmploymentType)
{
if (item.Checked) check = true;
};
if (check == true)
{
message += "\nТип занятости: ";
foreach (var item in Model.EmploymentType)
{
if (item.Checked) message += item.Text + " ";
};
}
else
{
message += "\nТип занятости: не выбран";
}
if (Model.StartNow)
{
message += "\nМогу ли немедленно приступить к работе: да";
}
else
{
message += "\nГотов приступить к работе с: " + Model.StartFrom;
}
msg.Body = message;
msg.IsBodyHtml = false;
//Attachment
if (Model.Resume != null && !(String.IsNullOrEmpty(Model.Resume.FileName)))
{
HttpPostedFileBase attFile = Model.Resume;
if (attFile.ContentLength > 0)
{
var attach = new Attachment(attFile.InputStream, attFile.FileName);
msg.Attachments.Add(attach);
}
}
SmtpClient client = new SmtpClient("denver.corepartners.local", 55);
client.UseDefaultCredentials = false;
client.EnableSsl = false;
try
{
client.Send(msg);
}
catch (Exception ex)
{
return RedirectToAction("CareerForm", "Career", ErrorMessage = "Ошибка при отправке письма, попробуйте позже");
}
return RedirectToAction("CareerForm", "Career");
}
但我只在第一種情況下獲得的附加文件,當我發送FeedbackForm到電子郵件。
對於CareerForm,我收到了電子郵件,但每次都沒有附件。 我檢查了debagger,每次看到Model.Resume = null
,但我不明白爲什麼。
我的代碼有什麼問題?
也許這是因爲我創建CareerForm model = new CareerForm();
在[HttpGet]
?
我該如何解決這個問題?
UPD 瀏覽:
FeedbackForm http://jsfiddle.net/fcnk9/
CareerForm http://jsfiddle.net/9Gz9u/
您可以將vi ew代碼也是? – davmos
FeedbackForm [http://jsfiddle.net/fcnk9/](http://jsfiddle.net/fcnk9/)CareerForm [http://jsfiddle.net/9Gz9u/](http://jsfiddle.net/9Gz9u/) ) – Heidel