我写了一个邮件发送程序,但是运行老是失败,不知问题在何处,请大家帮忙看看,谢谢!我的Mail类代码如下:
using System;
using System.Collections.Generic;
using System.Text;namespace MailSendPrj
{
    using System.Net;
    using System.Net.Mail;    public static class Mail
    {
        public static bool MailSend(string MailFrom, string MailPwd, string MailSmtp, string Mailto, string MailSubject, string MailBody, string MailAddFile)
        {
            try
            {
                SmtpClient objSmtpClient = new SmtpClient();
                objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                objSmtpClient.Host = MailSmtp;
                objSmtpClient.Credentials = new NetworkCredential(MailFrom.Split('@').GetValue(0).ToString(), MailPwd);
                MailMessage objMail = new MailMessage(MailFrom, Mailto);
                objMail.Body = MailBody;
                objMail.Subject = MailSubject;
                if (MailAddFile != "")
                {
                    Attachment objAttachment = new Attachment(MailAddFile);
                    objMail.Attachments.Add(objAttachment);
                }
                objSmtpClient.Send(objMail);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}发送事件代码如下:
 private void btnSend_Click(object sender, EventArgs e)
        {
            //不知道哪边写错了,发送总是失败,Mail类?Click事件?郁闷ing!
            if (MailSendPrj.Mail.MailSend(this.txtMailForm.Text, this.txtMailPwd.Text, this.txtMailSmtp.Text, this.txtMailTo.Text, this.txtMailSub.Text, this.txtMailContent.Text,this.txtAddFile.Text ))
            {
                MessageBox.Show("邮件发送成功!","系统消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("邮件发送失败!","系统消息",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

解决方案 »

  1.   

    你和我一样运气不好,我也一直发送失败.给两个据说成功的例子你.(其实我用别人成功的程序还是发送失败的)
    http://www.cnblogs.com/youring2/archive/2008/11/29/1343911.html
    http://www.zu14.cn/2008/11/05/net_mail/
      

  2.   

    //成功的例子
    private void button1_Click(object sender, EventArgs e)//send
            {
               if(this.tb_to.Text.Length<1|this.tb_from.Text.Length<1|this.rtb_body.Text.Length<1)
    {
    MessageBox.Show("必须填写收件人地址、发件人地址和内容","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    return;
    }            MailMessage mymail = new MailMessage(tb_from.Text ,tb_to.Text);
                mymail.Subject = tb_subject.Text;//主题
                mymail.Body = rtb_body.Text;//正文
                mymail.IsBodyHtml = false;//是否用html格式
                mymail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mymail.Priority = MailPriority.High;//优先级
                if (tb_cc.Text != "")
                {
                    mymail.CC.Add(tb_cc.Text);//抄送
                }
                if (tb_add.Text != "")
                {
                    Attachment data = new Attachment(tb_add.Text);
                    mymail.Attachments.Add(data);
                    //mymail.Attachments.Add( new Attachment(tb_add.Text));//附件
                }
                SmtpClient mysmtp = new SmtpClient(tb_mailserver.Text);
                //mysmtp.Host = tb_mailserver.Text;//mail服务器
                mysmtp.UseDefaultCredentials = false;
                mysmtp.Credentials  = new NetworkCredential(tb_id.Text,mtb_pwd.Text);//发送着身份验证
                mysmtp.DeliveryMethod = SmtpDeliveryMethod.Network;            try
                {
                    mysmtp.Send(mymail);
                    MessageBox.Show("发送成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                
            }