日期:2014-05-18  浏览次数:21013 次

C# winform 邮件发送 这样的错误
C# code

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

namespace MyEmail
{
    public partial class EmailMain : Form
    {
        public EmailMain()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 全局变量 SmtpClient
        /// </summary>
        private SmtpClient sc = new SmtpClient();
        string smtp = "";

        private void EmailMain_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage Email = new MailMessage();
                Email.To.Clear();
                Email.From = new MailAddress(textBox1.Text.Trim() + smtp.Substring(4));
                Email.To.Add(textBox3.Text.Trim());
                Email.Subject = textBox4.Text;
                Email.SubjectEncoding = Encoding.UTF8;
                Email.Body = textBox6.Text;
                Email.BodyEncoding = Encoding.UTF8;
                Email.IsBodyHtml = true;
                sc.Send(Email);//在这个地方出的错误。
                MessageBox.Show("邮件已发送!","发送提示");
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                MessageBox.Show("网络状况不好,发送失败!","发送提示");
            }
        }

        #region --邮件发送另一方法--
        /*
        EmailParms mailstr = new EmailParms();
        mailstr.EmailBody = "aaaa";
        mailstr.EmailHostName = "smtp.XXXX.com";
        mailstr.EmailPersonName = "admin";
        mailstr.EmailPort = 25;
        mailstr.EmailPriority = "high";
        mailstr.EmailSubject = "主题";
        mailstr.EncodingType = "UTF8";
        mailstr.FromEmailAddress = "a@a.com";
        mailstr.FromEmailPassword = "1111111";
        mailstr.isBodyHtml = false;
        mailstr.isEnableSsl = false;
        mailstr.ToEmailAddress = "b@b.com";
        sendmail.SendingEmail(mailstr);
         */
        #endregion

        #region -----登录验证-----
        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="Pwd">密码</param>
        /// <returns>验证结果</returns>
        private  bool SetUserInfo(string UserName, string Pwd, string host, int port)
        {
            try
            {
                NetworkCredential NC = new NetworkCredential(UserName, Pwd);
                sc.Host = host;
                sc.Port = port;
                sc.Timeout = 1000000;
                sc.Credentials = new System.Net.NetworkCredential(UserName,Pwd);
                return true;
            }
            catch(Exception e)
            {
                e.Message.ToString();
                return false;
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入用户名!","验证提示");
            }
            if (textBox2.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入密码!", "验证提示");
            }
            if (textBox2.Text.Trim().Length < 6)
            {
                MessageBox.Show("密码输入不正确!", "验证提示");
            }
            if (!comboBox1.SelectedItem.ToString().Equals(""))
            {
                switch (comboBox1.SelectedIndex)
                {