0

我创建了一个邮件程序,将电子邮件发送到我的帐户。我已经建立了 System.Net、System.Net.Mail。我在一些计算机上工作(我的主要计算机),但是每当我尝试做另一台计算机时,它都不会发送消息。我将它包围在 try、catch 块中,并且设置了异常变量。

代码:

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();
        }
    }

}

}
4

1 回答 1

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

my email不是有效的电子邮件地址。

更新:

您还需要用户名和密码才能获得凭据。

client.Credentials = new NetworkCredential(username, password);
于 2013-02-13T18:51:35.810 回答