Myworld
Myworld

Reputation: 1899

Unable to send email from ASP.NET

I had code to send emails to user when register on our website I did the code but no message was sent and error appeared (failure sending email).

bool SendMail(string account, string password, string to, string subject, string message)
{
    try
    {
        NetworkCredential loginInfo = new NetworkCredential(elarabyAccount, password);
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(account);
        msg.To.Add(new MailAddress(to));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = true;
        SmtpClient client = new SmtpClient("mail.example.com", 8080);
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = loginInfo;
        client.Send(msg);

        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

void BtnSend_Click(object sender, EventArgs e)
{
    SendMail("[email protected]", "xxxxx", TxtEmail.Text, "Hi", "Hi");
}

Upvotes: 1

Views: 907

Answers (2)

Tim B James
Tim B James

Reputation: 20364

I would suspect it is simply down to the Port number. For trying to find problems like this in the future though, you should try and see what the error messages you get from a Try - Catch statement

try
{
   //code in here
}
catch (System.Net.Mail.SmtpException exsmtp)
{
    throw new Exception(exsmtp.ToString());
} 
catch (Exception ex)
{
   throw new Exception(ex.ToString());
}

You will get a lot of use(less) information from this :)

Upvotes: 1

Chris
Chris

Reputation: 335

you have the port set as 8080. try 25

Upvotes: 2

Related Questions