使用附件Asp-Net MVC发送电子邮件

时间:2018-01-15 22:04:24

标签: c# asp.net-mvc email smtp email-attachments

我正在制作一个发送带附件的电子邮件的表单,我的应用程序在本地运行没有问题,但是当我从服务器运行它时,电子邮件不会被发送

我不确定是否应该提供一个临时文件夹将文件上传到服务器,我只是想发送附件,而不是保存在服务器上,我做错了什么?

我的模特:

public class EmailViewModel
{
    [Display(Name = "Destino")]
    [Required(ErrorMessage = "Debe ingresar un {0}")]
    public string Destino { get; set; }

    [Display(Name = "Asunto")]
    [Required(ErrorMessage = "Debe ingresar un {0}")]
    public string Asunto { get; set; }

    [DataType(DataType.MultilineText)]
    public string Mensaje { get; set; }

    public HttpPostedFileBase Upload { get; set; }
}

我的观点:

<h2>Enviar E-Mail</h2>

@using (Html.BeginForm("EnviarEMail", "OrdenCompras", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <hr />
    <div class="form-horizontal">

        <div class="form-group">
            @Html.LabelFor(model => model.Destino, "Destinatario", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Destino, new { htmlAttributes = new { @class = "form-control" , placeholder="gdiaz@mueblespangal.cl" } })
                @Html.ValidationMessageFor(model => model.Destino, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Asunto, "Asunto", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Asunto, new { htmlAttributes = new { @class = "form-control" , placeholder="Nueva Orden de Compra Muebles Pangal"} })
                @Html.ValidationMessageFor(model => model.Asunto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Mensaje, "Mensaje", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Mensaje, new { htmlAttributes = new { @class = "form-control" , placeholder ="Comentarios"} })
                @Html.ValidationMessageFor(model => model.Mensaje, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.Upload,"Adjuntar O. de Compra" ,new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                <input type="file" name="upload" />
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button class="btn btn-success" type="submit">
                    Enviar E-Mail
                    <i class="glyphicon glyphicon-envelope"></i>
                </button>
            </div>
        </div>
    </div>
}

我的控制器:

 [HttpPost]
 public ActionResult EnviarEMail(EmailViewModel emailviewmodel)
 {
     var mensaje = new MailMessage();
     mensaje.Subject = emailviewmodel.Asunto;
     mensaje.Body = emailviewmodel.Mensaje;
     mensaje.To.Add(emailviewmodel.Destino);

     mensaje.IsBodyHtml = true;

     if (emailviewmodel.Upload != null && emailviewmodel.Upload.ContentLength > 0)
     {
         mensaje.Attachments.Add(new Attachment(emailviewmodel.Upload.InputStream, Path.GetFileName(emailviewmodel.Upload.FileName)));
     }        

     var smtp = new SmtpClient();

     smtp.Send(mensaje);

     return RedirectToAction("VerOrdenCompra");
 }

我的WebConfig:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="coambiado@gmail.com">
        <network enableSsl="true" defaultCredentials="false" host="smtp.gmail.com" port="25"
                 userName="coambiado@gmail.com" password="******" />
      </smtp>
    </mailSettings>
  </system.net> 

0 个答案:

没有答案