简体   繁体   English

如何使用Asp.Net MVC数据库中的电子邮件地址在邮政上发送电子邮件

[英]How to send an email on Postal using an email address from the database Asp.Net MVC

I've currently implemented Postal and I'm using that to send emails on my MVC application. 我目前已经实现了邮政,并且正在使用它在我的MVC应用程序上发送电子邮件。 The problem I'm having is that I want to retrieve an email address from my database, instead of hard-coding the address I want to send the email to. 我遇到的问题是我想从数据库中检索电子邮件地址,而不是对要发送电子邮件的地址进行硬编码。

So to give a bit of context on where I'd like to implement this. 因此,在我想要实现的地方给出一些背景信息。 After I create my Order, I want to send an email address to the Hospital that is linked to that order. 创建订单后,我想向该订单链接的医院发送电子邮件地址。

This is what I have tried, but it throws an exception on this line email.To = order.Hospital.Email; 这是我尝试过的方法,但是在此行上引发了一个异常email.To = order.Hospital.Email; - It does work if I hard code the email address like, for example , email.To = "hospital@hospital.com". -如果我对电子邮件地址进行硬编码(例如, email.To = "hospital@hospital.com". ,它确实可以工作email.To = "hospital@hospital.com".

An exception of type 'System.NullReferenceException' occurred in HealthHabitat.dll but was not handled in user code HealthHabitat.dll中发生类型'System.NullReferenceException'的异常,但未在用户代码中处理

I've included it in my Create POST method so that it triggers once the model has been saved. 我已将其包含在我的Create POST method以便在保存模型后触发它。

The email address is saved as a string in my Hospital Model. 电子邮件地址保存为我的医院模式的字符串

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "OrderID,HospitalID,StaffID,Date,Time,Expected_Date")] Order order)
{
    if (ModelState.IsValid)
    {
        order.StaffID = 5;
        db.Orders.Add(order);
        db.SaveChanges();
        dynamic email = new Email("Example");
        email.To = order.Hospital.Email; // this is where you set the email you are sending to. 
        email.Send();
        Success(string.Format("<i class='fa fa-plus-circle'></i> Please Add your Items!", order.OrderID), true);
        return RedirectToAction("Details", new { id = order.OrderID });
    }


    ViewBag.HospitalID = new SelectList(db.Hospitals, "HospitalID", "Name", order.HospitalID);
    Danger("<i class='fa fa-warning'></i> Error!", true);
    return View(order);
}

Does any one have a solution to this? 有人对此有解决方案吗? I'll greatly appreciate it and thanks in advance! 我将不胜感激,并在此先感谢!

If any additional information is needed, please let me know and I'll add it to the question. 如果需要任何其他信息,请告诉我,我将其添加到问题中。

Hospital Model: 医院型号:

public class Hospital
{
    public int HospitalID { get; set; }

    public string Name { get; set; }

    public string Province { get; set; }

    public string Email { get; set; }

    public string Phone_Number { get; set; }


    public virtual ICollection<Order> Orders { get; set; }
}
}

You could do something like this: 您可以执行以下操作:

order.StaffID = 5;
db.Orders.Add(order);
db.SaveChanges();

//select hospital from database.
Hospital hospital = db.Hospitals.First(h => h.HospitalId == order.HospitalId);

dynamic email = new Email("Example");
email.To = hospital.Email; // this is where you set the email you are sending to. 
email.Send();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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