Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jamie Wu 19 posts 79 karma points
    Jul 25, 2011 @ 17:00
    Jamie Wu
    0

    Send email in razor macro

    Hi, I created a contact form by using razor macro. 

    @using System;
    @using System.Linq;
    @using System.Net.Mail;
    @{
    if (IsPost) { 
        string name = Request["Name"]; 
        string email = Request["Email"];
        string telephone = Request["Telephone"];
        try
                {
                MailMessage mail = new MailMessage();
                //var from = new MailAddress(Email.Text);
                mail.From = new MailAddress(email);
                mail.To.Add("[email protected]");
                mail.Subject = "Contact Us";
                mail.IsBodyHtml = false;
                mail.Body = "You just got a contact email:\n" +
                            "Name: " + name + "\n"
                            + "Phone: " + telephone + "\n"
                            + "Email: " + email + "\n";
                            
                SmtpClient smtp = new SmtpClient();
                
                    smtp.Send(mail);
                    
                }
                catch(Exception ex)
                {
                    
                }     
        <text> 
          You entered: <br /> 
          Name: @name <br /> 
          Email: @email <br /> 
          Telephone: @telephone <br />
        </text> 
        }


    <div class="inputForm">
    <form method="post" action=""> 
    <fieldset> 
    <h2></h2>
    <div> 
    <label for="Name">Name</label> 
    <input type="text" id="Name" name="Name" value="" /> 
    </div> 
    <div> 
    <label for="Email">Email:</label> 
    <input type="text" id="Email" name="Email" value="" /> 
    </div> 
    <label for="Telehphone">Telehphone:</label> 
    <input type="text" id="Telephone" name="Telephone" value="" /> 
    </div> 
    <div> 
    <label>&nbsp;</label> 
    <input type="submit" value="Submit" class="submit" /> 
    </div> 
    </fieldset> 
    </form> 

    However, it fails with error ''Failure sending mail." when the form is submitted. I am just wonderring if the above code is correct for razor macro? I am new to razor. 

    Thanks

    Jamie

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 25, 2011 @ 17:05
    Sebastiaan Janssen
    0

    You might save yourself a lot of trouble by using my contact mail package for Razor? ;-)

    As for the error you're getting, make sure that the mail configuration in your web.config is correct. There is some sample configs in the documentation of my package which you could try to test things out.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 25, 2011 @ 17:23
    Sebastiaan Janssen
    0

    Update: Just tested your code and it is fine, so either there's a problem in one of the variables (email, name or telephone), or it's the mail config.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jul 25, 2011 @ 17:56
    Dan Diplo
    1

    The first thing I would do is remove the empty Catch block so you can see if there is any SMTP exception being raised. It's generally bad-practice to swallow exceptions without doing anything, too.

Please Sign in or register to post replies

Write your reply to:

Draft