Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过邮政mvc发送电子邮件_C#_Asp.net Mvc_Postal - Fatal编程技术网

C# 通过邮政mvc发送电子邮件

C# 通过邮政mvc发送电子邮件,c#,asp.net-mvc,postal,C#,Asp.net Mvc,Postal,我试图在用户每次创建新评论时发送一封电子邮件。 我已经安装了Postal.MVC5包 评论模式 public int Id { get; set; } [Required] [MinLength(3)] [MaxLength(20)] public string Asunto { get; set; } [Required] public string Detalle { get; set; } [DataType(DataType.Da

我试图在用户每次创建新评论时发送一封电子邮件。 我已经安装了Postal.MVC5包

评论模式

public int Id { get; set; }

    [Required]
    [MinLength(3)]
    [MaxLength(20)]
    public string Asunto { get; set; }
    [Required]
    public string Detalle { get; set; }
    [DataType(DataType.Date)]
    public DateTime Fecha { get; set; }
    [DataType(DataType.Time)]
    public DateTime Hora { get; set; }
    public  virtual Local local { get; set; }
    public virtual string username { get; set; }
public class NewCommentEmail : Email
{
    public string To { get; set; }
    public string UserName { get; set; }
    public string Comment { get; set; }
}
    using System.Net.Mail;
using System.Net;

namespace AutoPlanMCV.Models
{
    public class NewCommentEmail 
    {
        public static string GmailUsername { get; set; }
        public static string GmailPassword { get; set; }
        public static string GmailHost { get; set; }
        public static int GmailPort { get; set; }
        public static bool GmailSSL { get; set; }

        public string ToEmail { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }

        static NewCommentEmail()
        {
            GmailHost = "smtp.gmail.com";
            GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment.
            GmailSSL = true;
        }

        public void Send()
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = GmailHost;
            smtp.Port = GmailPort;
            smtp.EnableSsl = GmailSSL;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword);

            using (var message = new MailMessage(GmailUsername, ToEmail))
            {
                message.Subject = Subject;
                message.Body = Body;
                message.IsBodyHtml = IsHtml;
                smtp.Send(message);
            }
        }

}
NewCommentEmail模式

public int Id { get; set; }

    [Required]
    [MinLength(3)]
    [MaxLength(20)]
    public string Asunto { get; set; }
    [Required]
    public string Detalle { get; set; }
    [DataType(DataType.Date)]
    public DateTime Fecha { get; set; }
    [DataType(DataType.Time)]
    public DateTime Hora { get; set; }
    public  virtual Local local { get; set; }
    public virtual string username { get; set; }
public class NewCommentEmail : Email
{
    public string To { get; set; }
    public string UserName { get; set; }
    public string Comment { get; set; }
}
    using System.Net.Mail;
using System.Net;

namespace AutoPlanMCV.Models
{
    public class NewCommentEmail 
    {
        public static string GmailUsername { get; set; }
        public static string GmailPassword { get; set; }
        public static string GmailHost { get; set; }
        public static int GmailPort { get; set; }
        public static bool GmailSSL { get; set; }

        public string ToEmail { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }

        static NewCommentEmail()
        {
            GmailHost = "smtp.gmail.com";
            GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment.
            GmailSSL = true;
        }

        public void Send()
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = GmailHost;
            smtp.Port = GmailPort;
            smtp.EnableSsl = GmailSSL;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword);

            using (var message = new MailMessage(GmailUsername, ToEmail))
            {
                message.Subject = Subject;
                message.Body = Body;
                message.IsBodyHtml = IsHtml;
                smtp.Send(message);
            }
        }

}
控制器

 [HttpPost]
    public ActionResult AgregarComentario(NotasAdjuntas nota)
    {
        try
        {
            var localId = int.Parse(Request["LocalID"]);
            nota.username = Session["Username"].ToString();

            using (var db = new AutoContex())
            {
                nota.local = db.Locales.Find(localId);
                db.Entry(nota.local).Reference(p => p.Proveedor).Load();
                db.Notas.Add(nota);
                db.SaveChanges();
            }

            var email = new NewCommentEmail();
            email.To = "emilia.lasaga@pedidosya.com";
            email.UserName = nota.username;
            email.Comment = nota.Asunto;               
            email.Send();
            return RedirectToAction("Details", new { @id = localId });                
        }
        catch
        {
            return View();
        }
    }
public ActionResult AgregarComentario(NotasAdjuntas nota)
    {

        try
        {

            var localId = int.Parse(Request["LocalID"]);
            nota.username = Session["Username"].ToString();

            using (var db = new AutoContex())
            {
                nota.local = db.Locales.Find(localId);
                db.Entry(nota.local).Reference(p => p.Proveedor).Load();
                db.Notas.Add(nota);
                db.SaveChanges();

                if (nota.Asunto.Contains("capa"))
                {
                    NewCommentEmail.GmailUsername = "youremail@gmail.com";
                    NewCommentEmail.GmailPassword = "yourpassword";

                    NewCommentEmail mailer = new NewCommentEmail();
                    mailer.ToEmail = "Emailto@gmail.com";
                    mailer.Subject = nota.Asunto;
                    mailer.Body = nota.local.NuevoId + " --  --" + nota.local.NombreComercio + "<br />" + nota.Detalle + "<br /> Hora:" + nota.Hora;
                    mailer.IsHtml = true;
                    mailer.Send();
                }
            }

            return RedirectToAction("Details", new { @id = localId });

        }
        catch
        {
            return View();
        }

    }
我还将此添加到网络配置中

<system.net>
<mailSettings>
  <smtp deliveryMethod="SpecifiedPickupDirectory">
    <specifiedPickupDirectory pickupDirectoryLocation="C:\Temp\" />
  </smtp>
</mailSettings>
查看

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Notas Adjuntas</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div>
        <ledgend>Datos del Local<ledgend>
                <div class="panel-title">Id</div>
                <div class="text-left">
                    <input type="Text" name="LocalID" id="LocalID" value="@ViewBag.Local.Id" />
                </div>
            <table class="table">
                <tr>
                    <th>Id</th>
                    <th>Viejo Id</th>
                    <th>Nombre</th>
                    <th>Unificado Con</th>
                    <th>Dirección</th>
                    <th>Telefono</th>
                    <th>Localidad</th>
                    <th>Provincia</th>
                    <th>Proveedor</th>
                    <th>Estado</th>

                    <th>Fecha Instalación</th>
                </tr>
                <tr>
                    <td>@ViewBag.Local.NuevoId</td>
                    <td>@ViewBag.Local.ViejoId</td>
                    <td>@ViewBag.Local.NombreComercio</td>
                    <td>@ViewBag.Local.UnificadoCon</td>
                    <td>@ViewBag.Local.Direccion</td>
                    <td>@ViewBag.Local.Telefono</td>
                    <td>@ViewBag.Local.Localidad</td>
                    <td>@ViewBag.Local.Provincia</td>
                    <td>@ViewBag.Local.Proveedor.Nombre</td>
                    <td>@ViewBag.Local.Estado.State</td>

                    <td>@ViewBag.Local.FechaInstalacion</td>
                </tr>
            </table>

    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.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" } })
            @Html.ValidationMessageFor(model => model.Asunto, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
@Html.LabelFor(model => model.Detalle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
    @Html.EditorFor(model => model.Detalle, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Detalle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.Fecha, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Fecha, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Fecha, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.Hora, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Hora, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Hora, "", new { @class = "text-danger" })
    </div>
</div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
副官

@Html.ValidationSummary(true,“,new{@class=“text danger”}) 本地拿督酒店 身份证件 身份证件 维耶霍Id 名义 Unificado Con 迪雷西翁 电传 地方爸爸 省 校长 埃斯塔多 费查·因斯塔拉西翁酒店 @ViewBag.Local.NuevoId @ViewBag.Local.ViejoId @ViewBag.Local.NombreComercio @ViewBag.Local.UnificadoCon @ViewBag.Local.Direccion @ViewBag.Local.Telefono @ViewBag.Local.Localidad @ViewBag.Local.Provincia @ViewBag.Local.Proveedor.Nombre @ViewBag.Local.Estado.State @ViewBag.Local.FechaInstalacion @LabelFor(model=>model.Asunto,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Asunto,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Asunto,“,new{@class=“text danger”}) @LabelFor(model=>model.Detalle,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Detalle,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Detalle,“,new{@class=“text danger”}) @LabelFor(model=>model.Fecha,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Fecha,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Fecha,“,new{@class=“text danger”}) @LabelFor(model=>model.Hora,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Hora,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Hora,“,new{@class=“text danger”})
我可以通过smtps发送电子邮件

这不是我寻找的第一种方法(我想通过Postal.MVC5实现)

我所做的是:

创建电子邮件的新类

public int Id { get; set; }

    [Required]
    [MinLength(3)]
    [MaxLength(20)]
    public string Asunto { get; set; }
    [Required]
    public string Detalle { get; set; }
    [DataType(DataType.Date)]
    public DateTime Fecha { get; set; }
    [DataType(DataType.Time)]
    public DateTime Hora { get; set; }
    public  virtual Local local { get; set; }
    public virtual string username { get; set; }
public class NewCommentEmail : Email
{
    public string To { get; set; }
    public string UserName { get; set; }
    public string Comment { get; set; }
}
    using System.Net.Mail;
using System.Net;

namespace AutoPlanMCV.Models
{
    public class NewCommentEmail 
    {
        public static string GmailUsername { get; set; }
        public static string GmailPassword { get; set; }
        public static string GmailHost { get; set; }
        public static int GmailPort { get; set; }
        public static bool GmailSSL { get; set; }

        public string ToEmail { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }

        static NewCommentEmail()
        {
            GmailHost = "smtp.gmail.com";
            GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment.
            GmailSSL = true;
        }

        public void Send()
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = GmailHost;
            smtp.Port = GmailPort;
            smtp.EnableSsl = GmailSSL;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword);

            using (var message = new MailMessage(GmailUsername, ToEmail))
            {
                message.Subject = Subject;
                message.Body = Body;
                message.IsBodyHtml = IsHtml;
                smtp.Send(message);
            }
        }

}
控制器

 [HttpPost]
    public ActionResult AgregarComentario(NotasAdjuntas nota)
    {
        try
        {
            var localId = int.Parse(Request["LocalID"]);
            nota.username = Session["Username"].ToString();

            using (var db = new AutoContex())
            {
                nota.local = db.Locales.Find(localId);
                db.Entry(nota.local).Reference(p => p.Proveedor).Load();
                db.Notas.Add(nota);
                db.SaveChanges();
            }

            var email = new NewCommentEmail();
            email.To = "emilia.lasaga@pedidosya.com";
            email.UserName = nota.username;
            email.Comment = nota.Asunto;               
            email.Send();
            return RedirectToAction("Details", new { @id = localId });                
        }
        catch
        {
            return View();
        }
    }
public ActionResult AgregarComentario(NotasAdjuntas nota)
    {

        try
        {

            var localId = int.Parse(Request["LocalID"]);
            nota.username = Session["Username"].ToString();

            using (var db = new AutoContex())
            {
                nota.local = db.Locales.Find(localId);
                db.Entry(nota.local).Reference(p => p.Proveedor).Load();
                db.Notas.Add(nota);
                db.SaveChanges();

                if (nota.Asunto.Contains("capa"))
                {
                    NewCommentEmail.GmailUsername = "youremail@gmail.com";
                    NewCommentEmail.GmailPassword = "yourpassword";

                    NewCommentEmail mailer = new NewCommentEmail();
                    mailer.ToEmail = "Emailto@gmail.com";
                    mailer.Subject = nota.Asunto;
                    mailer.Body = nota.local.NuevoId + " --  --" + nota.local.NombreComercio + "<br />" + nota.Detalle + "<br /> Hora:" + nota.Hora;
                    mailer.IsHtml = true;
                    mailer.Send();
                }
            }

            return RedirectToAction("Details", new { @id = localId });

        }
        catch
        {
            return View();
        }

    }
AgregarComentario公共行动结果(NotasAdjuntas nota)
{
尝试
{
var localId=int.Parse(请求[“localId”]);
nota.username=Session[“username”].ToString();
使用(var db=new autocentex())
{
nota.local=db.Locales.Find(localId);
db.Entry(nota.local).Reference(p=>p.Proveedor.Load();
db.Notas.Add(nota);
db.SaveChanges();
如果(不包括(“capa”))
{
NewCommentEmail.GmailUsername=”youremail@gmail.com";
NewCommentEmail.GmailPassword=“yourpassword”;
NewCommentEmail mailer=新的NewCommentEmail();
mailer.ToEmail=”Emailto@gmail.com";
mailer.Subject=nota.Asunto;
mailer.Body=nota.local.NuevoId+”--“+nota.local.NombreComercio+”
“+nota.Detalle+”
Hora:“+nota.Hora; mailer.IsHtml=true; mailer.Send(); } } 返回RedirectToAction(“Details”,new{@id=localId}); } 抓住 { 返回视图(); } }
我删除了网络配置中的内容,并且没有编辑我的视图。
在那之后我没有收到任何错误

是否有更多关于错误的细节?更多关于消息?堆栈跟踪?内部异常?Detalles de la Exception:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:No se puede realizar enlace en tiempo de ejección en una referencea null你能把它翻译成英文吗?堆栈溢出是英文的h网站,很难用另一种语言帮助您。Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法在空引用中执行时建立连接,哪一行会引发错误?