0

I have created a mail program that will send the email to my account. I have established System.Net, System.Net.Mail. I works on some computers (my main one), but whenever I try and do another computer, It doesn't send the message. I have it surrounded in try, catch blocks and I have my exception variable set.

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.IO;
using System.Windows;
using System.Text;
using System.Collections.Generic;

namespace WindowsFormsApplication3
{
public partial class trinate : Form
{
    public trinate()
    {
        InitializeComponent();
    }

    private void MenuClose_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (textBox1.Enabled == false)
        {
            textBox1.Enabled = true;
        }
        else {
            textBox1.Enabled = false;
            textBox1.Text = "some text";
        }

    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (textBox2.Enabled == false)
        {
            textBox2.Enabled = true;
        }
        else
        {
            textBox2.Enabled = false;
            textBox2.Text = "some more text";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        progressBar1.Maximum = 100;
        progressBar1.Value = 0;
        progressBar1.Minimum = 0;
        if (authenticate(textBox1.Text) == true && email1.TextLength != 0 && accPass.TextLength != 0)
        {
            if (radioButton1.Checked == true || radioButton2.Checked == true)
            {
                SmtpClient client = new SmtpClient("smtp.comcast.net", 25);
                client.Credentials = new NetworkCredential();
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(email1.Text);
                msg.To.Add(new MailAddress("my email"));
                msg.Subject = "mailmessage";
                msg.Body = "body text";
                try
                {
                    client.Send(msg);
                    ProgRun(true);
                }
                catch (Exception err)
                {
                    MessageBox.Show("Unable to create valid connection! ", "Fatal Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                    progressBar1.Value = 0;
                    ProgRun(false);
                }
            }
            else
            {
                MessageBox.Show("One or more values not entered or correct", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        else {
            MessageBox.Show("One or more values not entered or correct", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {

    }

    private Boolean authenticate(string code) {
       //This is specific to my application. Nothing to do with mail if (code == "A1C4M98")
        {
            return true;
        }
        else {
            return false;
        }
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (authenticate(textBox1.Text) != true)
        {
            errorProvider1.SetError(textBox1, "string!");
        }
        else {
            errorProvider1.SetError(textBox1, "");
        }
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            accPass.PasswordChar = char.MinValue;
        }
        else {
            accPass.PasswordChar = Convert.ToChar("*");
        }
    }

    private void ProgRun(Boolean ch) {

        if (ch == true)
        {
            for (int i = 0; i <= 100; i++)
            {
                progressBar1.Value = i;
                Thread.Sleep(100);
                if (progressBar1.Value == 100)
                {
                    MessageBox.Show("Valid Key Created!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                    break;
                }

            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (progressBar1.Value == 100)
        {
            ProgRun1(true);
        }
        else
        {
            MessageBox.Show("text", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
        }
    }

    private void ProgRun1(Boolean ch)
    {

        if (ch == true)
        {
            for (int i = 0; i <= 100; i++)
            {
                progressBar1.Value = i;
                Thread.Sleep(100);
                if (progressBar1.Value == 100)
                {
                    MessageBox.Show("Process Completed!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    break;
                }
            }
            Application.Exit();
        }
    }

}

}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
alaney
  • 1
  • 1
  • 1
    When you say it does not send the message, does that mean the code executes successfully without any exceptions but you never receive the email in your inbox? – Ameen Feb 13 '13 at 18:50
  • Do you really think we need to read all of that code in order to help you? Please try to narrow down your questions to just the code necessary to reproduce the problem. – John Saunders Feb 13 '13 at 18:54
  • Also, what do you mean, "I have my exception variable set"? – John Saunders Feb 13 '13 at 18:55

1 Answers1

1
msg.To.Add(new MailAddress("my email")); 

my email is not a valid email address.

Updated:

You also need username and password for credential.

client.Credentials = new NetworkCredential(username, password);
Win
  • 61,100
  • 13
  • 102
  • 181
  • 1
    I think he must have changed that before posting the question to protect his privacy. `MailAddress` will throw if the address is in an invalid format. – Ameen Feb 13 '13 at 18:53
  • I never thought of that :) – Win Feb 13 '13 at 19:21