Send an SMTP email [duplicated]

1

Hello, how am I trying to send emails through a form in asp but it does not work for me and this is what I have behind the code (.cs)

  protected void btnform_Click(object sender, EventArgs e)
  {
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("[email protected]");
    msg.To.Add(email.Value);
    msg.Body = comentarios.Value;
    msg.IsBodyHtml = true;

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
    NetworkCred.UserName = "[email protected]";
    NetworkCred.Password = "mipasswords";
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587 ;
    smtp.EnableSsl = true ;

    try
    {
        smtp.Send(msg);
    }

    catch (Exception ec)
    {

    }


}

This is my code on the front but I do not know which part does not work XP, oh and by the way I have another question, if I have my host service as I do to send emails from my domain / host or as it says XD

 <div class="seven_col_to_one_col">
        <form action="/contacto.aspx" method="post" runat="server" id="test">
            <input runat="server"  required name="nombre" type="text" class="form" id="nombre" placeholder="nombre"/>
            <input runat="server" required pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" name="email" type="email" class="form" id="email" placeholder="e-mail"/>
            <input runat="server" type="text" name="comentarios"  rows="8"  class="form comentarios" id="comentarios" placeholder="comentarios" />
            <button runat="server" id="submit" type="submit" class="btn_form" name="submit" value="enviar" onclick="btnform_Click">Enviar</button>
        </form>
    </div>
    
asked by Godeolo 21.04.2017 в 00:08
source

1 answer

1

The same thing happens to me once and it's a matter of permissions in the GMAIL account, try the following steps:

  • In the Gmail account you are using, go to the settings:
  • Go to the accounts and import tab and select Other google account settings:
  • The page link will open and you will be able to access the Applications and Connected Sites option:
  • There you go to the last box and activate the option Allow access to less secure applications

With that you should be able to send email with google accounts from your own application.

In the case of your own HOST, you only have to put your server as you configured it in Outlook, SMTP server, ports, encryption type, a user and a password.

    
answered by 21.04.2017 / 02:53
source