Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
See more:
this is the code error :
System.Net.Mail.SmtpException
  HResult=0x80131500
  Message=Failure sending mail.
  Source=System
  StackTrace:
   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: 'darylds2001@gmail.com'


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()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress("***@gmail.com"); //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("***@gmail.com", 587)) //enter the same email that the message is sending from along with port 587
                {
                    smtp.Credentials = new NetworkCredential("***@gmail.com", "******"); //Enter email with password 
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }

            }
        }
    }


}
Posted
Updated 4-Aug-20 6:37am
v4
Comments
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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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

I know not from there this
Quote:
//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("smtp.gmail.com", 587, true);
    smtpClient.Authenticate("user", "password");
    smtpClient.Send(mailMessage);
    smtpClient.Disconnect(true);
}
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100