0

i want to send email using this code but there is the problem appear so how can i solve this issue

<asp:TextBox ID="To"  runat="server"></asp:TextBox><br />
            <asp:TextBox ID="from" runat="server"></asp:TextBox><br />
                      <asp:TextBox ID="subject" runat="server"></asp:TextBox><br />
                      <asp:TextBox ID="body" runat="server"></asp:TextBox><br />
                      <asp:Button ID="Button3" OnClick="send" runat="server" Text="send" />


 protected void send(object sender, EventArgs e)
    {
        MailMessage message = new MailMessage(To.Text,from.Text,subject.Text,body.Text);
        message.IsBodyHtml = true;
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true;

        client.Credentials = new System.Net.NetworkCredential("email","pass");
        client.Send(message);


    }



at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at AmbulanceManagementSystem.Front_End_Paramedics.SavePatientInfo.send(Object sender, EventArgs e) in c:\Users\Zoulfikar\Documents\Visual Studio 2013\Projects\AmbulanceManagementSystem\AmbulanceManagementSystem\Front End Paramedics\SavePatientInfo.aspx.cs:line 161
  • Can you provide the full error message? Perhaps even a stack trace? – Tim Oct 17 '18 at 17:43
  • Also, feel free to search here on SO to make sure your question has not been asked already. Have you checked out this question? https://stackoverflow.com/q/704636/229778 – Tim Oct 17 '18 at 17:50
  • Tim there is the full error –  Oct 17 '18 at 18:05

1 Answers1

1

I just ran into the same problem, you have to turn on "less secure access". You should have an email in your inbox stating this, assuming the password is correct. Here is the code I'm using that works.

public void SendEmail(string address, string attatchment)
    {
        using (MailMessage mail = new MailMessage())
        {
            mail.From = new MailAddress("FromEmail@gmail.com");
            mail.To.Add("ToEmail@gmail.com");
            mail.Subject = "Report";
            mail.Body = "Report";
            mail.IsBodyHtml = true;
            mail.Attachments.Add(new Attachment(attatchment));

            using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
            {
                smtp.Credentials = new NetworkCredential("FromEmail@gmail.com", "Password");
                smtp.EnableSsl = true;
                smtp.Send(mail);
            }
        }
    }
John112358
  • 98
  • 10
  • I don't think it's the code as I just tested this again. Can you check if the firewall is blocking you? **Edit** If the Firewall is blocking you then it shouldn't get an authentication problem. – John112358 Oct 17 '18 at 18:14
  • Do you have another network you can try your code on? – John112358 Oct 17 '18 at 18:17