我和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;
}
}
发布于 2015-05-18 15:51:28
发件人地址仅设置电子邮件标题中的发件人地址。大多数SMTP服务器将不允许您从与凭据匹配的帐户以外的电子邮件地址发送电子邮件。
听起来像Hotmail正在重写您的from头,以匹配您以前登录的帐户。
https://stackoverflow.com/questions/30314525
复制