【发布时间】:2015-07-30 15:01:25
【问题描述】:
我正在与 Umbraco 合作。我的页面上有一个表单,用户应该在其中输入他们的电子邮件地址和应该发送到我的电子邮件的消息。
问题是我现在基本上只是作为发件人和收件人向自己发送电子邮件。 MailMessage 构造函数中的 from 没有设置 from 地址,它做了什么?
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Mail;
public class BlogPostSurfaceController : Umbraco.Web.Mvc.SurfaceController
{
public BlogPostSurfaceController() {
}
[HttpPost]
public ActionResult CreateComment(CommentViewModel model)
{
//model not valid, do not save, but return current Umbraco page
if (!ModelState.IsValid)
{
//redirecting)
return CurrentUmbracoPage();
}
SendMail(model);
//redirect to current page to clear the form
return RedirectToCurrentUmbracoPage();
}
[HttpPost]
public int SendMail(CommentViewModel model) {
try{
MailAddress from = new MailAddress(model.Email);
MailAddress to = new MailAddress("me@hotmail.com");
MailMessage mail = new MailMessage(from, to);
mail.Body = model.Message;
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("me@hotmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch(Exception ex) {
return -1;
}
return 1;
}
}
【问题讨论】:
标签: c# asp.net-mvc umbraco