Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.25/5 (4 votes)
See more:
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 = "";
            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;
                }
            }
            return false;//Says this is unreachable code
        }
Posted
Comments
VR Karthikeyan 16-Dec-15 23:29pm    
What's your problem? Can't you send email from this code? or unreachable code error?
Richard Deeming 18-Dec-15 13:22pm    
REPOST
You have already posted this question:
http://www.codeproject.com/Questions/1064977/Csharp-Send-Email-Cant-Send-Email-in-Csharp-Net-He[^]

If you want to update your question, DO NOT post the update as a new question! Click the "Improve question" button on your original question, and update it with the new information.

Reposting the same question duplicates work, and annoys the unpaid volunteers who answer questions here. If you keep doing it, you may end up being banned from this site.

1 solution

Hi, use this code. This is working version from my project.
C#
string smtpAddress = "smtp.gmail.com";
int portNumber = 25;

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

MailMessage objMailMessage = new MailMessage(emailFrom, emailTo, subject, body);

SmtpClient objSmtpClient = new SmtpClient(smtpAddress);
objSmtpClient.Port = portNumber;
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.Credentials = new NetworkCredential(emailFrom, password);
objSmtpClient.EnableSsl = true;

objMailMessage.Attachments.Add(new Attachment(Employee.FileLocation));

objSmtpClient.Send(objMailMessage);
 
Share this answer
 

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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900