locked
Problem with receiving emails from contact form. RRS feed

  • Question

  • User1103245085 posted

    Hi, i'm student who is trying to develop a digital portfolio for myself with a contact page, however, I have a problem with receiving the email that is sent to my outlook account. When i click the send button it will load the page and not show any error meaning that the email technically sent but when I check for any new mails there are none. I'm also wondering if this is the most secure way to do this after all this will be published eventually.

    Front-end:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
         <h1 class="MainTitle">Contact Me</h1>
    
        <div class="container">
        <div>
    
        <label for="fname">Full Name</label>
        <asp:textbox type="text" id="fname" class="fname" runat="server" name="firstname" 
         placeholder="Your name here"/>
        
         <label for="from">From</label>
        <asp:textbox type="text" id="from" class="from" runat="server" name="from" 
         placeholder="Bob@gmail.com"/>
    
        <label for="body">Message</label>
        <asp:textbox id="body" name="body" CssClass="body" runat="server" placeholder="Write 
         something..." style="height:200px" TextMode="MultiLine"></asp:textbox>
    
        <asp:button text="Send" Class="Sendbtn" value="Submit" runat="server" 
        onclick="submit_Click"/>
    
        </div>
        </div>
    
    </asp:Content>

    Back-end:

     protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            string to = "myEmail@outlook.com";
            protected void submit_Click(object sender, EventArgs e)
            {
                try
                {
                    MailMessage msg = new MailMessage(to, from.Text, fname.Text, body.Text);
    
                    //CONFIGURE SMTP OBJECT
                    SmtpClient client = new SmtpClient("smtp-mail.outlook.com", 587);
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.EnableSsl = true;
                    client.Credentials = new System.Net.NetworkCredential(to, "myPassword");
    
                    //SEND EMAIL
                    client.Send(msg);
                }
                catch(Exception ex)
                {
                    ex.Message.ToString();
                }
            }

    Saturday, April 11, 2020 10:43 PM

Answers

  • User-18289217 posted

    try setting the smtp to mail.live.com

    In addition please notice that sometimes you need to approve the sending emails from within the email settings (outlook)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 12, 2020 8:28 AM

All replies

  • User753101303 posted

    Hi,

    The message is perhaps not sent at all. In the catch block you are doing at all with the message and so you are just hiding any exception that could occur.  I would remove the try/catch block for now to see what happens.

    Once this particular problem is solved look at best practices for handling exceptions. In  particular if you just want to log exceptions it can be done at a higher level rather than locally in each method.

    Sunday, April 12, 2020 8:07 AM
  • User-18289217 posted

    try setting the smtp to mail.live.com

    In addition please notice that sometimes you need to approve the sending emails from within the email settings (outlook)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 12, 2020 8:28 AM