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

邮件发送不成功原因
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.Mail;

namespace sent_Email
{
  public partial class Form1 : Form
  {
  //定义公用变量
  MailMessage Mail;
  SmtpClient Client;
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {

  }
  //自定义函数实现电子邮件的发送
  public void CreateTimeoutTestMessage(string server)
  {
  try
  {
  Mail = new MailMessage(this.txtfrom.Text, this.txtget.Text);
  Mail.Subject = this.txtsubject.Text.Trim().ToString();
  Mail.Body = this.txtbody.Text.Trim().ToString();
  Client = new SmtpClient(server,40);
  Client.Send(Mail);
  MessageBox.Show("邮件发送成功!");
  }
  catch (Exception ey)
  {
  MessageBox.Show(ey.Message);
  }
  }

  private void btnsent_Click(object sender, EventArgs e)
  {
  if (this.txtbody.Text.Trim().ToString() != "")
  {
  CreateTimeoutTestMessage("pop.163.com");
  }
  else
  {
  MessageBox.Show("请认真填写邮件!");
  return;
  }
  }

  private void btnquit_Click(object sender, EventArgs e)
  {
  this.Close();
  Application.Exit();
  }
  }
}
一直异常捕获,原因是哪?

------解决方案--------------------
参考这个
C# code
/// <summary>
        /// 发送邮件功能
        /// </summary>
        /// <param name="fromEmail">登录邮箱</param>
        /// <param name="password">登录密码</param>
        /// <param name="user">邮件昵称</param>
        /// <param name="title">邮件标题</param>
        /// <param name="toEmail">邮件地址</param>
        /// <param name="email">邮件内容</param>
        /// <param name="smtpServer">smtp服务器</param>
        public bool SendMessage(string fromEmail,string password, string user, string title, string toEmail, string email,string smtpServer)
        {
            try
            {              
                SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network
                smtp.EnableSsl = false;//smtp服务器是否启用SSL加密
                smtp.Host = smtpServer;//指定 smtp 服务器                   
                smtp.Credentials = new NetworkCredential(fromEmail, password);
                MailMessage mm = new MailMessage(); //实例化一个邮件类
                mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可              
                mm.From = new MailAddress(fromEmail, user, Encoding.GetEncoding(936));
                mm.CC.Add(new MailAddress(toEmail, "", Encoding.GetEncoding(936)));
                mm.Subject = title; //邮件标题
                mm.SubjectEncoding = Encoding.GetEncoding(936);
                mm.IsBodyHtml = true; //邮件正文是否是HTML