locked
Sending email to an adress RRS feed

  • Question

  • User-2094959909 posted

    Hello everyone,

    i have in the footer of the page a textbox ( first name ) and an other textbox ( email ) and textarea ( message ), 

    i want to send an email to an adress (only one adresse) when the areas above are filled and clicked on "send message"

    Thanks in advance.

    Tuesday, September 18, 2018 9:29 PM

All replies

  • User283571144 posted

    Hi Omar27,

    According to your description, I couldn't understand your requirement clearly.

    Do you mean you want to send the email from the textbox  to another address's email?

    If this is your requirement, I suggest you could consider using  System.Net.Mail help you to achieve your requirement.

    More details, you could refer to below demo codes:

    Notice: You should set the email's password in the code-behind.

            first name :  <asp:TextBox ID= "name" runat="server"></asp:TextBox><br/>
            email name: <asp:textbox id="email" runat="server" /><br/>
             Message body:<br/>
            <asp:textbox id="txtBody" runat="server" height="150px" textmode="multiline" /><br/>
            <asp:button id="btn_SendMessage" runat="server" onclick="btn_SendMessage_Click" text="Send message" /><br/>
            <asp:label id="Label1" runat="server" text="" />

    Code-behind:

            protected void btn_SendMessage_Click(object sender, EventArgs e)
            {
                SmtpClient smtpClient = new SmtpClient("smtp.office365.com", 25);
                smtpClient.EnableSsl = true;
                smtpClient.Credentials = new System.Net.NetworkCredential("the email account", "password");
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                //
                MailMessage mailMessage = new MailMessage(email.Text, "to email");
                mailMessage.Subject = name.Text;
                mailMessage.Body = txtBody.Text;
    
                try
                {
                    smtpClient.Send(mailMessage);
                    Label1.Text = "Message sent";
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.ToString();
                }
            }

    Result:

    Best Regards,

    Brando

    Wednesday, September 19, 2018 2:43 AM
  • User-2094959909 posted

    Brando ZWZ.


    like i have in the bottom of my website a division i have inside it a 3 textboxes that if the user want to send a message to the company who has the website i work on, he has to fill the textbox1 with his name, the 2nd with his email, the 3rd with the message he wants to send, so when he clicks on the button "send message" it should a message to the company's email

    If you need any more explanation please ask me again.

    Thursday, September 20, 2018 5:18 PM
  • User283571144 posted

    Hi Omar27,

    I still couldnn't understand your issue clearly.

    Could you please tell me which is From email? User's Email or Company Email?

    If you want to send the Email from user to your company, you should add a textbox to let user type in his password.

    Best Regards.

    Brando

    Friday, September 21, 2018 8:46 AM
  • User-2094959909 posted

    Hi Brando ZWZ, 

    i made a website for the company i had a 2 months internship inside, so in the bottom of the website i have a contact section which the visitor of the website can contact the company so in the contact section he has a field to fill with his name and another field for his email and the 3rd field for the message ha wantss to send to the company's email.

    i hope you understand now more.

    Friday, September 21, 2018 4:44 PM
  • User1724605321 posted

    Hi OMAR27 ,

    You can use asp:RequiredFieldValidator to make sure the user enter his name/email/message :

    <asp:Textbox id="txtLastName" runat="server"></asp:Textbox>
    <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
      ControlToValidate="txtLastName"
      ErrorMessage="Last name is a required field."
      ForeColor="Red">
    </asp:RequiredFieldValidator>

    When user click the submit button , in code behind , you can get the information from textboxes , send email to your own mailbox with user's information , in this scenario , you should provide your email credential to complete the email sending behavior . To use SmtpClient to send emails , you could refer to above code sample .

    Best Regards,

    Nan Yu

    Monday, September 24, 2018 7:15 AM
  • User753101303 posted

    Hi,

    The main point is to configure the mail server you'll use to send those mails:

    https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/network/mailsettings-element-network-settings

    Using the MialMessage/SmtpClient classes is quite straigtforward. Knowing what happens when you try suggested code would help to understand which kind of help is neeed. For know my guess is that you have to coinfigure mail settings (it's sometimes done on code, but the SmtpClient class will read automatically settings found in the web.config file).

    Monday, September 24, 2018 7:49 AM