0

Im working on an web application i got stuck between this email sending code.I am working on contact page where anyone can send you email as you guys know that.This code in working fine but i can't pass through this error

enter image description here

as i said im working with Asp.Net Mvc so here is my POST controller im using gmail account so that it won't conflict between any mail services.

  public ActionResult sendemail()
    {
        return View();
    }

    [HttpPost]
    public ActionResult sendemail(string to, string from, string subject, string body, string pwd)
    {
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;

        client.UseDefaultCredentials = true;
        client.Credentials = new NetworkCredential("faseehyasin12@gmail.com", pwd);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;

        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from);
        mail.Subject = subject;
        mail.Body = body;
        try
        {
            client.Send(mail);
            Response.Write("ok");
            return View();
        }
        catch(Exception e)
        {
            throw e;
        }

    }

and here is my view and i want to ask do i really need a password to send email to someone in this code ? enter image description here

and my GET controller is empty with only written code is return view() so i won't bother to take ss for that. i have also allowed "less secure app" but it still gives me this error. Need Help

faseeh
  • 131
  • 2
  • 10
  • ASP.NET MVC is a *web framework*. It has nothing to do with sending emails. Post your *code*, not images of the code. Images can't be compiled and tested, and nobody is going to type all that just to test it. As far as *this* error is concerned though, the message already explains what's wrong. The *server* requires SSL, or the credentials were wrong – Panagiotis Kanavos Mar 21 '18 at 11:55
  • 1
    There are duplicates that explain how to use GMail to send emails already. Use unit tests or a small console application to test your settings instead of a full-blown web site. It's a *lot* faster to try things, hit F5 or `Run Tests` and change settings until you get it right. – Panagiotis Kanavos Mar 21 '18 at 11:58
  • i have tried this same code in the past and it worked that time i even got the email.f it was SSL problem then why this error didn't come at that time. – faseeh Mar 21 '18 at 11:59
  • 1
    Because you have a problem this time. Wrong password, no authentication, wrong settings. Whatever. BTW you still haven't posted your code. Images can't be compiled. If I wanted to test your code I *can't*. – Panagiotis Kanavos Mar 21 '18 at 11:59
  • try settng "UseDefaultCredentials" to true. – Nirman Mar 21 '18 at 12:00
  • yes!! that is what im doing i have created different app to check this code if this code work i'll put it into main website – faseeh Mar 21 '18 at 12:00
  • yes, agreed with @PanagiotisKanavos, you need to post code instead of images. That way, others can reproduce without rewriting at their end. – Nirman Mar 21 '18 at 12:01
  • im posting code my internet is slow -_- – faseeh Mar 21 '18 at 12:02
  • you can check the code – faseeh Mar 21 '18 at 12:08
  • @Nirman that will try to pass the user's Windows credentials. Do you really think Google will accept that? – ADyson Mar 21 '18 at 12:09
  • @faseeh the port for SSL connections is 465. Google "gmail smtp settings" – ADyson Mar 21 '18 at 12:10
  • Port 25, 465, or 587 – faseeh Mar 21 '18 at 12:13
  • 465 = SSL and 587 = TSL – faseeh Mar 21 '18 at 12:13
  • check the two-factor authentication related answer from this post, and see if it helps you - https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp/9572958#9572958 – Nirman Mar 21 '18 at 12:15
  • i have use client.enablessl so only 465 port will work with this code – faseeh Mar 21 '18 at 12:15
  • Yes that's true. But your code says `client.Port = 587;` so you need to change it to 465 – ADyson Mar 21 '18 at 14:25
  • i checked it today it is working now with the same port (587) thanks for helpig me out :-) – faseeh Mar 22 '18 at 12:05

1 Answers1

1

First go to https://myaccount.google.com/lesssecureapps and change status as open. And go to https://accounts.google.com/b/0/displayunlockcaptcha and click Continue button.

Please try with following code.

string host = "smtp.gmail.com";
int port = 587;
bool ssl = true;
string fromAddress = "faseehyasin12@gmail.com";
string fromPassword = "your password here";
using (var mail = new MailMessage())
{
    string subject = "Test";
    string body = "Mail test";
    mail.From = new MailAddress(fromAddress);
    mail.Subject = subject;
    mail.IsBodyHtml = true;
    mail.Body = body;
    mail.To.Add("mail@domain.com");
    using (var smtpServer = new SmtpClient(host,port))
    {
        smtpServer.UseDefaultCredentials = false;
        smtpServer.Credentials = new System.Net.NetworkCredential(fromAddress, fromPassword);
        smtpServer.EnableSsl = ssl;
        smtpServer.Send(mail);
    }

}