简体   繁体   中英

Umbraco email form - missing sender

I'm working with Umbraco. I have an form on my page where the user should input their email address and the message which should then be sent to my email.

The problem is I'm basically just sending emails to my self now as both sender and receiver. What does the from in the MailMessage constructor do since it's not setting the from address in?

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;
    }
}

The from address only sets the from address in the email header. Most SMTP servers will not let you send email from an email address other than the account that matches your credentials.

It sounds like Hotmail is rewriting your from header to match the account you used to log in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM