Click here to Skip to main content
15,355,833 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
this is the code error :
  Message=Failure sending mail.
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at Verification.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\daryl\source\repos\Verification - Copy\Verification\Form1.cs:line 36
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Verification.Program.Main() in C:\Users\daryl\source\repos\Verification - Copy\Verification\Program.cs:line 19

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
WebException: The remote name could not be resolved: ''

What I have tried:

this is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
using System.Windows.Forms;

namespace Verification
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            using (MailMessage mail = new MailMessage())
                mail.From = new MailAddress("***"); //enter whatever email you are sending from here 
                mail.To.Add(textBox1.Text); //Text box that the user enters their email address 
                mail.Subject = "Email Subject"; //enter whatever subject you would like 
                mail.Body = "<p> Dear ......</p> <br> <p> Enter message here </p>";
                mail.IsBodyHtml = true;

                using (SmtpClient smtp = new SmtpClient("***", 587)) //enter the same email that the message is sending from along with port 587
                    smtp.Credentials = new NetworkCredential("***", "******"); //Enter email with password 
                    smtp.EnableSsl = true;


Updated 9-Feb-22 16:42pm
Richard Deeming 4-Aug-20 12:39pm
Removing the content of your question after it has been answered is extremely rude.

I assume you were trying to hide the email address and password which you had posted publicly. That won't work - the previous revisions of your question are still available to view.

I have reverted your destructive edit, and removed the email address and password from your question. If they are your real GMail credentials, I suggest your change them urgently, before a hacker takes over your account.

This line in your code
using (SmtpClient smtp = new SmtpClient("", 587))
is certainly wrong - the SmtpClient constructor takes the SMTP Host/Server ie email server ie ""

I know not from there this
//enter the same email that the message is sending from along with port 587
came from, but it is incorrect

smtp.EnableSsl = true;
'tick', good

You can also use MailKit (You'll need to install the Nuget Package first)

var mailMessage = new MimeMessage();
mailMessage.From.Add(new MailboxAddress("from name", "from email"));
mailMessage.To.Add(new MailboxAddress("to name", "to email"));
mailMessage.Subject = "subject";
mailMessage.Body = new TextPart("plain")
    Text = "Hello"

using (var smtpClient = new SmtpClient())
    smtpClient.Connect("", 587, true);
    smtpClient.Authenticate("user", "password");
At a guess, you didn't fill in the bits right: the ones where it says "enter whatever email you are sending from here" and "Enter email with password"

Unless these match with the settings on your email server, the mail will not go out.
Use the debugger to examine teh Exception object when the error occurs and see if there is any more information in the InnerException field.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900