1

This is my code try to send mailif you have any idea to solve this help me guys

enter code here
        public ActionResult ContactForm(Contact cnt)
    {
        BlogApplicationEntities db = new BlogApplicationEntities();
        if (ModelState.IsValid)
        {
            try
            {
                MailMessage msg = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                msg.To.Add(new MailAddress("send to mail address"));
                msg.Subject = "Contact Us";
                msg.Body += "\nFirst Name=" + cnt.FirstName;
                msg.Body += "Last Name=" + cnt.LastName;
                msg.Body += "Email=" + cnt.Email;
                msg.From = new MailAddress("mailaddress", "Jhon");
                msg.Body += "Comments=" + cnt.Comment;
                msg.IsBodyHtml = true;

                smtp.Credentials = new System.Net.NetworkCredential("network mail address", "**password**");
                smtp.Host = "https://smtp.gmail.com";
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Port = 587;
                smtp.Send(msg);
                msg.Dispose();
                db.Contacts.Add(cnt);
                db.SaveChanges();
                return View("Success");
            }
            catch (Exception)
            {

                return View("Error");
            }
        }


        return View();

    }

When the below line is executed:

smtp.Send(msg);

Its throws an error : 'The remote name couldn't be resolved: https://smtp.gmail.com'

What can I do to solve this, can someone help me?

İbrahim
  • 37
  • 1
  • 8

3 Answers3

0

Change https://smtp.gmail.com, to smtp.gmail.com, and you need to validate a proper gmail account. Follow examples showed in this post.

Community
  • 1
  • 1
Mez
  • 4,666
  • 4
  • 29
  • 57
0

You need to change your SMTP host to smtp.gmail.com:

public ActionResult ContactForm(Contact cnt)
{
    BlogApplicationEntities db = new BlogApplicationEntities();
    if (ModelState.IsValid)
    {
        try
        {
            MailMessage msg = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            msg.To.Add(new MailAddress("ibrahimtirampaci@hotmail.com"));
            msg.Subject = "Contact Us";
            msg.Body += "\nFirst Name=" + cnt.FirstName;
            msg.Body += "Last Name=" + cnt.LastName;
            msg.Body += "Email=" + cnt.Email;
            msg.From = new MailAddress("ibrahimtirampaci@hotmail.com", "İbrahim");
            msg.Body += "Comments=" + cnt.Comment;
            msg.IsBodyHtml = true;

            smtp.Credentials = new System.Net.NetworkCredential("ibrahimtirampaci@hotmail.com", "2117542ibo");
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Port = 587;
            smtp.Send(msg);
            msg.Dispose();
            db.Contacts.Add(cnt);
            db.SaveChanges();
            return View("Success");
        }
        catch (Exception)
        {

            return View("Error");
        }
    }

    return View();
}

Host names don't include the transport protocol, but when you specify EnableSsl=true, you're setting the transport protocol to SSL. One side note, make sure that you're using a gmail account for your network credentials if you're sending from gmail.

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
0

I changed the smtp to smtp.Host = "smtp.gmail.com" but now Its throws an error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

I think my problem is null Credentials.

İbrahim
  • 37
  • 1
  • 8