Problem in sending the email

Hi

I am using the following code to send an email

But I am getting runtime error

Can you please tell me the reason
MailMessage message = new MailMessage();
        message.From = new MailAddress("sender@domain.com");
        message.To.Add(new MailAddress(MailTo));
        message.Subject = Subject;
        message.Body = Body.ToString();
        
        SmtpClient client = new SmtpClient("localhost");                       
        client.EnableSsl = true;
        client.Send(message);

Open in new window

KaranGuptaAsked:
Who is Participating?
 
Pratima PharandeConnect With a Mentor Commented:
message.From.Add(new MailAddress("sender@domain.com"));
        message.To.Add(new MailAddress(MailTo));


also try hard coded values

client.Send("to","Form","subject","body");
also cj=heck your localhost is confogured for SMTP or not ?
0
 
Pratima PharandeCommented:
put tje valid mail address  here

message.From = new MailAddress("sender@domain.com");

What error you are getting ?
0
 
KaranGuptaAuthor Commented:
Hi

I have changed it to valid email id

But I am getting the same error

"Failure sending mail. "
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
käµfm³d 👽Commented:
Is the machine you are running the code on (localhost) an SMTP server?
0
 
KaranGuptaAuthor Commented:
No

I think we are using the the SMTP service in services.msc to send an email
0
 
KaranGuptaAuthor Commented:
I have an outlook configured on my machine

so I think my machine also configures for SMTP
0
 
käµfm³d 👽Commented:
Does your Outlook go to localhost as well? If not, the copy the server address that Outlook is using into your

    SmtpClient client = new SmtpClient("server_name_here");

line.
0
 
KaranGuptaAuthor Commented:
Hi

SmtpClient client = new SmtpClient()

with this I want to use gmails smtp server

How can I specify here
0
 
tetorvikConnect With a Mentor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.