RXC
RXC

Reputation: 1233

Sending Email in C# does not work

I am writing a windows form that can send messages in C#.

I had this working and sending messages at one point, but now my program just hangs when it comes time to send the message.

Here is the code to send the message:

private void sendEmail()
    {
        string host = "";
        int port = 0;

        host = checkFromAddress(ref port);

        try
        {
            // Create the email to send
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add(recipient1.Text);
            message.Subject = "subject";
            message.From = new System.Net.Mail.MailAddress(userName.Text);
            message.Body = "Test Messge";

            // Setup smtp information related to the host used
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = host;
            smtp.Port = port;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential(userName.Text, password.Text);

            // Send the message
            smtp.Send(message);
        }
        catch (Exception)
        {
            MessageBox.Show("Please check email settings and try again");
        }
    }

The username and password are entered into the form, the host and port are determined based on the user's credentials.

I debug the program and when it gets to smtp.Send(message), it just hangs and I am unable to bring up the form. I have to stop debugging or kill the process.

Any ideas why it is not working?

Upvotes: 0

Views: 203

Answers (1)

Adam Miller
Adam Miller

Reputation: 777

Did you verify that all the values getting passed in are valid? Possible things to check are host/port. Also, are the 'from' and 'to' getting valid email addresses? Those look a little suspect.

Upvotes: 1

Related Questions