我正在尝试在有人发布问题后向我发送电子邮件。虽然我收到了一封电子邮件,但这不是我所需要的:来自mailaddress的是我自己的而不是提出问题的人。
是否可以向您自己发送邮件(我不知道提问者的smtp主机,用户名或密码)。
如果需要,我会添加一些代码。
QuestionsController.cs
MailMessage msg = new MailMessage();
msg.From = new MailAddress(question.Author + "<" + question.MailAuthor + ">");
msg.To.Add("myGmailAddress");
msg.Subject = question.Title;
msg.Body = question.Message;
msg.Sender = new MailAddress(question.MailAuthor);
SmtpClient client = new SmtpClient();
client.Send(msg);
我的代码首先没有包含msg.sender,但我通过添加发件人来尝试它,但这也没有帮助。
Web.config(在视图文件夹之外)
<system.net>
<mailSettings>
<smtp from="myGmailAddress">
<network
host="smtp.gmail.com"
password="myPassword"
userName="myGmailAddress"
port="587"
enableSsl="true"
defaultCredentials="false" />
</smtp>
</mailSettings>
</system.net>
还尝试了没有from部分和defaultCredentials =“false”。
当我调试代码时,我可以看到msg中的from以我的gmail地址开头,当他越过msg.From部分时会发生变化。但是当我收到我的邮件时,我得到了这个:
答案 0 :(得分:0)
感谢sylon的提交,我决定创建一个非正式的地址,以告知新问题的人。
答案 1 :(得分:0)
我遇到了同样的问题并在没有noreply的情况下修复它,我认为你的主机服务器没有问题,就像你可以尝试执行此操作的Gmail一样:
1.通过主机服务器发送电子邮件(我没有使用Web.config):
[HttpPost]
public ActionResult SendMail(Question question)
{
string retValue = "There was an error submitting the form, please try again later.";
if (!ModelState.IsValid)
{
return Content(retValue);
}
if (ModelState.IsValid)
{
using (var client = new SmtpClient
{
host="mail.yourDomain.com" //mail.yourDomain.be,if your using belgium,
Port = 587,
EnableSsl = true,
UseDefaultCredentials=false,
Credentials = new NetworkCredential("yourDomainEmailAddress","PasswordOfDomainMail"),
DeliveryMethod = SmtpDeliveryMethod.Network})
{
var mail = new MailMessage();
mail.To.Add("yourDomainEmailAddress");
mail.From = new MailAddress(question.MailAuthor, question.Author);
mail.Subject = String.Format(question.Title);
msg.Body = question.Message;
mail.ReplyToList.Add(yourDomainEmailAddress);
try
{
client.Send(mail);
retValue = "Your Request for Contact was submitted successfully. We will contact you shortly.";
}
catch (Exception)
{
throw;
}
}
}
return Content(retValue);
}