Click here to Skip to main content
15,991,360 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have tried this code.but it shows error.

What I have tried:

System.Net.Mail.MailMessage Email = new System.Net.Mail.MailMessage("xyz@gmail.com", YourEmail.Text);
Email.Subject = YourSubject.Text;
Email.Body = Comments.Text;
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
//This object stores the authentication values
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("xyz@gmail.com", "9583364957");
//Put your own, or your ISPs, mail server name onthis next line
mailClient.Host = "Mail.Gmail.com";
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
mailClient.Send(Email);
Posted
Updated 12-Feb-19 2:58am
Comments
OriginalGriff 12-Feb-19 3:16am    
What error?
Where?
When does it show it?
What have you tried to fix it? What happened?

Use the "Improve question" widget to edit your question and provide better information.
Member 13999803 12-Feb-19 3:47am    
mailClient.Send(Email);
in this line it showing failure sending mail
Richard MacCutchan 12-Feb-19 4:05am    
Mail.Gmail.com is not a valid mail server name. Also, you should be using your local ISP mail system, as Google does not always allow this sort of message sending.
OriginalGriff 12-Feb-19 4:05am    
And what more detail does it give you? What is in the inner exception? What does the debugger show? What have you tried to fix it?

Remember, we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with, and we can't access your data while your app is running, unlike you!
F-ES Sitecore 12-Feb-19 5:22am    
This is one of the most frequently asked questions and it is extensively documented. Google "c# send email gmail" and you'll find the code you need as well as a list of common problems and solutions you'll find. The ultimate answer is that you shouldn't use gmail to send email at all, you should use the smtp server provided by your web host or network admin.

1 solution

The correct SMTP host for gmail is smtp.gmail.com

Other common issues I have seen with gmail:

1. Google does not recognize the connection being used for the particular email account being used. The best thing to do is to go to their Login Activity page and tell them basically that "yes, that was me"
Sign in - Google Accounts[^]

2. Google by default blocks SMTP access under the premise that it is less secure. There is an option to allow "Less Secure Apps" which will allow SMTP access
Account settings: Your browser is not supported.[^]
 
Share this answer
 
Comments
Member 13999803 14-Feb-19 2:40am    
may i know the port name?
Member 13999803 14-Feb-19 3:03am    
ya got it.it works fine.thank u so much

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