Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Why does this code return False?
Both Email Account Passwords Are The Same And I Own Them Both!

C#
internal static Boolean __send_email(String yourEmailHost,String usersEmailAddress, String userMessageSubject, String userMessageBody, String yourEmailAddress, String yourEmailAddressPassword, UInt16 yourEmailProvidersSMTPPortNumber)
        {
            string smtpAddress = yourEmailHost;
            int portNumber = yourEmailProvidersSMTPPortNumber;
            bool enableSSL = true;

            string emailFrom = "shelbypurcell000@gmail.com";
            string password = "";//Password Here
            string emailTo = "shelby.p67@gmail.com";
            string subject = userMessageSubject;
            string body = userMessageBody;

            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFrom);
                mail.To.Add(emailTo);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                // Can set to false, if you are sending pure text.
                using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFrom, password);
                    smtp.EnableSsl = enableSSL;
                    smtp.Send(mail);
                    return true;
                }
            }
        }
Posted
Comments
DamithSL 16-Dec-15 22:53pm    
There is no return false in your code, but how you receive false! have you debug your code?
Richard Deeming 18-Dec-15 13:20pm    
Your question makes no sense - the code you have posted most certainly DOES NOT return false!

The code will either run successfully and return true; or it will throw an exception.

If it's throwing an exception, then you need to post the full details of that exception. Click "Improve question" and update your question with the missing details. Remember to indicate which line of code the exception is thrown from.
Wombaticus 18-Dec-15 16:51pm    
I would suggest you edit your post to remove your email addresses.

Try This

C#
private void button1_Click(object sender, EventArgs e)
{
    try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

        mail.From = new MailAddress("someone@gmail.com");
        mail.To.Add("someone@mail.com");
        mail.Subject = "Test Mail";
        mail.Body = "This is for testing SMTP mail from C# Project";

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("someone@gmail.com", "password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);
        MessageBox.Show("mail Send");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
 
Share this answer
 
Google Gmail login mechanism require more steps for it to works.

It's either you implement their secure login procedure or disable secure login.

Disable secure login is the simpler way but less security.

About turn off secure sign in:

Google may block sign in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safer.

Some examples of apps that do not support the latest security standards include:

The Mail app on your iPhone or iPad with iOS 6 or below
The Mail app on your Windows phone preceding the 8.1 release
Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird
Therefore, you have to enable Less Secure Sign-In in your google account.

After sign into google account, go to:

https://www.google.com/settings/security/lesssecureapps
 
Share this answer
 
C#
<pre lang="C#">MailMessage Msg = new MailMessage();
               // Sender e-mail address.
               Msg.From = new MailAddress(mail id here);
               // Recipient e-mail address.
               Msg.To.Add(mail id);
               Msg.Subject = &quot;Add Subject&quot;;
               Msg.Body = &quot;Hi, &lt;br/&gt;&lt;br/&gt;Detailss&lt;br/&gt;&lt;br/&gt;:  &quot;&quot;&quot;;
               Msg.IsBodyHtml = true;
               // your remote SMTP server IP.
               SmtpClient smtp = new SmtpClient();
               smtp.Host = &quot;smtp.gmail.com&quot;;
               smtp.Port = 587;
               smtp.Credentials = new System.Net.NetworkCredential(&quot;from Mail id here&quot;,&quot;passwordhere&quot;);
               smtp.EnableSsl = true;
               smtp.Send(Msg);
               ScriptManager.RegisterStartupScript(Page, GetType(), &quot;MyScript&quot;, &quot;alert(&#39;Your Password Details Sent to your mail&#39;);&quot;, true);
               &quot;e&quot;);
              ;</pre>
 
Share this answer
 

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