How to use gmail SMTP in ASP.NET form

前端 未结 7 592
眼角桃花
眼角桃花 2020-12-04 02:00

I am a ASP novice and am troubleshooting a form for work. None of us here are ASP experts as we use PHP. But I am on the bottom of PHP experience as well, mostly working wit

相关标签:
7条回答
  • 2020-12-04 02:32

    You can add this in your web.config file

     <system.net>
        <mailSettings>
          <smtp from="yourMailId@gmail.com ">
            <network host="smtp.gmail.com" defaultCredentials="false"
          port="587" userName ="yourmail@gmail.com" password="yourpassword" />
          </smtp>
        </mailSettings>
       </system.net>
    
    0 讨论(0)
  • 2020-12-04 02:38

    Google now blocks sign-ins from an app (even over SSL) to send an email. You have two options:

    1. Enable less-secure apps for the account you are using; OR
    2. Generate an app-password from Google (recommended)

    For option 2, you will have to enable 2-factor authentication before you can generate an app pasword. Here are the steps I took to get this working on my ASP.NET web app

    How to configure an ASP.NET web app with Gmail SMTP

    0 讨论(0)
  • 2020-12-04 02:46

    Create a System.Net.Mail.SmtpClient object, set the SMTP server info you are using.

    Then create a System.Smtl.MailMessage with the message data and send it:

    using (System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient()) {
        using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("from*where.com", "to@where.com") {
            IsBodyHtml = true,
            Subject = "Subject text",
            Body = messageBody,
        }) {
            mail.Send(message);
    } // using
    

    You can configure your SmtpClient in the constructor, we use web.comfig, so I don't have that code.

    0 讨论(0)
  • 2020-12-04 02:46

    Try this.

    Dim client As New Net.Mail.SmtpClient
    client.UseDefaultCredentials = False
    client.Credentials = New System.Net.NetworkCredential("sender@gmail.com", "password")
    client.Host = "smtp.gmail.com"
    client.Port = 587
    client.EnableSsl = True
    client.Send("sender@gmail.com","reciever@gmail.com","subject","body")
    
    0 讨论(0)
  • 2020-12-04 02:52

    There's no shortage of tutorials on how to send an email from within .NET.

    Essentially you want a System.Net.Mail.SmtpClient object to interact with the SMTP server, a System.Net.Mail.MailMessage object to hold the message data, and configuration data in your config file to direct the client on how/where to send the message.

    0 讨论(0)
  • 2020-12-04 02:55
    protected void SendMail()
            {
                MailMessage msg = new MailMessage();
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                try
                {
                    msg.Subject = "Add Subject";
                    msg.Body = "Add Email Body Part";
                    msg.From = new MailAddress("Valid Email Address");
                    msg.To.Add("Valid Email Address");
                    msg.IsBodyHtml = true;
                    client.Host = "smtp.gmail.com";
                    System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential("Valid Email Address", "Password");
                    client.Port = int.Parse("587");
                    client.EnableSsl = true;
                    client.UseDefaultCredentials = false;
                    client.Credentials = basicauthenticationinfo;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Send(msg);
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
            }
    
    0 讨论(0)
提交回复
热议问题