locked
how to send an invoice to a mail address RRS feed

  • Question

  • User-1557173817 posted

    hello just created a form for an invoice and alongside a database but also want to mail that same invoice to the customer how can i  do that.. what razor code.. thanks 

    Tuesday, October 13, 2015 5:47 AM

Answers

All replies

  • User-1980594115 posted

    Here is an example you can modify with the invoice information that you have gathered from your form.

    @{ 
        var invoiceNumber = "5";
        var firstName = "Bill";
        var lastName = "Smith";
        var customer = "bSmith@test.com";
        var company = "nRowser@company.com";
    
        // Process Email
        // Create Email Header
        var header = new[] { "Reply-To:zzzzzz@yyyyyyyy.com" };
    
        // Create Email Message
        var message = "<br />";
        message += "<div style='color:black; font-weight:normal; font-family: Palatino;'>" + "An Invoice has been submitted. ID#: " + @invoiceNumber + "</div><br />";
    
        // Name
        message += "<div style='color:#751818;'>";
        message += "&nbsp;&nbsp;&nbsp;&nbsp;• Name:";
        message += "<span style='color:#751818;'>";
        message += "&nbsp;&nbsp;" + @firstName + " " + @lastName + "</div><br />";
    
        message += "</div><br />";
    
        message += "" + "<br />";
        message += "<div style='color:black; font-weight:normal; font-family: Palatino;'>" + "If you have any questions, or if there is an error in any of the information above, please email zzzzzz@yyyyyyyy.com" + "</div><br /><br />";
    
        // Send email as HTML
        WebMail.Send(
            from: "zzzzzz@yyyyyyyy.com",
            to: @customer,
            cc: @company,
            subject: "Invoice ID# - " + invoiceNumber,
            body: message,
            additionalHeaders: header,
            isBodyHtml: true
            );
    }
    

    Tuesday, October 13, 2015 12:03 PM
  • User-1557173817 posted

    Thanks a lot, but i also want to send the invoice values to my database, how will i incorporate that and lastly how will i trigger the email to send because i cant see the if (isPost()) statement here.

    Tuesday, October 13, 2015 12:11 PM
  • User325035487 posted

    Frown

    Tuesday, October 13, 2015 12:37 PM
  • User379720387 posted

    Dan,

    Install the startersite template.

    In there you will find an example of how you can send email  from webmatrix.

    Retrieving and updating the database for email is the same thing as you have done before.

    Tuesday, October 13, 2015 10:00 PM
  • User-1980594115 posted

    Your question sounded like you already had the database where the invoice form values were inserted. There are excellent examples of how to use databases with Web Pages, http://www.asp.net/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/entering-data.  Based on your forum questions, you seem to be progressing well. Utilize the  available resources on the http://www.asp.net/web-pages site and you will gain knowledge you will need to create complete web applications.

    You are correct that I did not include the IsPost, that you will need.

    Wednesday, October 14, 2015 10:26 AM
  • User-1557173817 posted

    hello, here is my code, i tried sending the mail but got this error message:

    The best overloaded method match for 'System.Web.Helpers.WebMail.Send(string, string, string, string, string, System.Collections.Generic.IEnumerable<string>, bool, System.Collections.Generic.IEnumerable<string>, string, string, string, string, string)' has some invalid arguments

    @{
        var customerName = Request["customerName"];
        var customerEmail = Request["customerEmail"]; 
        var customerRequest = Request["customerRequest"];
        var Deposite= Request["Deposite"];
        var Balance = Request["Balance"];
        var due = Request["due"];
        var errorMessage = "";
        var debuggingFlag = false;
        try {
            // Initialize WebMail helper
            WebMail.SmtpServer = "smtp.gmail.com ";
            WebMail.SmtpPort = 465;
            WebMail.UserName = "example@zoracom.net";
            WebMail.Password = "";
            WebMail.From = "example@zoracom.net";
     
            // Send email
            WebMail.Send(to: customerEmail,
                subject: "Invoice notification for - " + customerName,
                body: customerRequest,
                 additionalHeaders: Deposite
           
               
            );
        }
        catch (Exception ex ) {
            errorMessage = ex.Message;
        }
    }
        

    Tuesday, October 20, 2015 9:48 AM
  • User325035487 posted

    ...deleted...

    Wednesday, October 21, 2015 3:19 AM
  • User-1557173817 posted

    thanks.. but i got this error message after running the code.

    The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. ly4sm9738343wjb.4 - gsmtp

    Wednesday, October 21, 2015 7:23 AM
  • User753101303 posted

    Use WebMail.EnableSsl=true; and if I remember it's better to use port 587. See http://stackoverflow.com/questions/17286597/asp-net-razor-sending-email-from-a-website-using-gmail-smtp and scroll down to the end (uses SmtpClient but it doesn't change much and settings should be the same).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2015 7:35 AM
  • User-1557173817 posted

    thanks a lot...it worhed

    Wednesday, October 21, 2015 8:14 AM