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

mustang00

I've currently implemented Postal and I'm using that to send emails on my MVC application. 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; - It does work if I hard code the email address like, for example , email.To = "[email protected]".

An exception of type 'System.NullReferenceException' occurred in HealthHabitat.dll but was not handled in user code

I've included it in my Create POST method so that it triggers once the model has been saved.

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

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to send an email to multiple email addresses in the database using Postal in ASP.NET MVC

From Dev

Send EMail using asp.net mvc

From Dev

Send Scheduled Email using Azure Scheduler / Postal / MVC

From Dev

how to send an email in asp.net mvc4

From Dev

Cannot send email in ASP.NET MVC

From Dev

How can I send email using Gmail SMTP in asp.net mvc application?

From Dev

Send mail using the email address from a variable

From Dev

How to fetch record from database and send it to email using codeigniter?

From Dev

How to take users email address from a datagrid and populate it in an email in asp.net

From Dev

Send email from database using phpmailer

From Dev

how to fetch email address from oracle database

From Dev

Email gets send from own email address

From Dev

How to send email from MVC 5 application

From Dev

Validate ASP.NET MVC model to contain only a single email address using RegularExpressionAttribute

From Dev

Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5

From Dev

Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5

From Dev

Send email in asp.net mvc without using credentials like in php

From Dev

Send email in asp.net mvc without using credentials like in php

From Dev

How to get Email address of external login in ASP.NET MVC 5

From Dev

How do I change the ASP.NET MVC default login page to login by username instead of email address?

From Dev

How do I get the Id of a user given the email address in ASP.NET MVC Entity Framework

From Dev

Send simple Email ASP.NET MVC, IIS7

From Dev

email doesnt send in mvc but works in asp.net webform

From Dev

How to send an email using mailx so that From and To appear correctly in the email?

From Dev

convert function from asp to asp.net send the email

From Dev

Using aliased email address to send mail from Google App Engine

From Dev

How to send email from PHP using XAMPP

From Dev

PHPMailer Send Email To Each Address In Database Individually

From Dev

Get firstname, lastname and email address from Facebook using ASP.NET

Related Related

  1. 1

    How to send an email to multiple email addresses in the database using Postal in ASP.NET MVC

  2. 2

    Send EMail using asp.net mvc

  3. 3

    Send Scheduled Email using Azure Scheduler / Postal / MVC

  4. 4

    how to send an email in asp.net mvc4

  5. 5

    Cannot send email in ASP.NET MVC

  6. 6

    How can I send email using Gmail SMTP in asp.net mvc application?

  7. 7

    Send mail using the email address from a variable

  8. 8

    How to fetch record from database and send it to email using codeigniter?

  9. 9

    How to take users email address from a datagrid and populate it in an email in asp.net

  10. 10

    Send email from database using phpmailer

  11. 11

    how to fetch email address from oracle database

  12. 12

    Email gets send from own email address

  13. 13

    How to send email from MVC 5 application

  14. 14

    Validate ASP.NET MVC model to contain only a single email address using RegularExpressionAttribute

  15. 15

    Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5

  16. 16

    Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5

  17. 17

    Send email in asp.net mvc without using credentials like in php

  18. 18

    Send email in asp.net mvc without using credentials like in php

  19. 19

    How to get Email address of external login in ASP.NET MVC 5

  20. 20

    How do I change the ASP.NET MVC default login page to login by username instead of email address?

  21. 21

    How do I get the Id of a user given the email address in ASP.NET MVC Entity Framework

  22. 22

    Send simple Email ASP.NET MVC, IIS7

  23. 23

    email doesnt send in mvc but works in asp.net webform

  24. 24

    How to send an email using mailx so that From and To appear correctly in the email?

  25. 25

    convert function from asp to asp.net send the email

  26. 26

    Using aliased email address to send mail from Google App Engine

  27. 27

    How to send email from PHP using XAMPP

  28. 28

    PHPMailer Send Email To Each Address In Database Individually

  29. 29

    Get firstname, lastname and email address from Facebook using ASP.NET

HotTag

Archive