我是c#的初学者,按照书上的实例写了个email小程序,每次在发送邮件的时候都会提示“未能访问到CDO.Message对象,我要怎么办?

解决方案 »

  1.   

    private void tabManageReviews_SendMail()
       {
    try
    {
    MailMessage mail=new MailMessage();
    mail.To=tabManageReviews_txtEmail.Text;
    mail.From="[email protected]";
    mail.Subject=tabManageReviews_cmboEmail.Text;
    mail.BodyFormat=MailFormat.Html;
    mail.Body=tabManageReviews_txtEmailText.Text; if(tabManageReviews_treeView1.SelectedNode.Text!="Attachments"&&tabManageReviews_treeView1.SelectedNode.Text!="Contracts")
    {

    MailAttachment ma=new MailAttachment("e:\\exe\\"+tabManageReviews_treeView1.SelectedNode.Parent.Text+"\\"+
    tabManageReviews_treeView1.SelectedNode.Text+".doc");
    mail.Attachments.Add(ma);
    }
    else
    {
    DialogResult DiRes;
    DiRes=MessageBox.Show("You haven't selected a mail attachment"+
    "Do You want to send mail anyway?","Send Without Attachment?",
    MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation);
    if(DiRes==DialogResult.No)
    {
    MessageBox.Show("Nothing was delivered.","Mail Canceled",MessageBoxButtons.OK,MessageBoxIcon.Information);
    return;
    }
    }
    DialogResult DiRes2;
    DiRes2=MessageBox.Show("Please review your message."+
    "Click 'ok to send or 'Cancel' to do just that. \n\n"
    +tabManageReviews_txtEmailText.Text,"Send Message?",
    MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
    if(DiRes2==DialogResult.Cancel)
    {
    MessageBox.Show("Nothing was delivered.","Mail Canceled",
    MessageBoxButtons.OK,MessageBoxIcon.Information);
    tabManageReviews_treeView1.SelectedNode=tabManageReviews_treeView1.Nodes[0];
    return;
    }
    SmtpMail.SmtpServer="ENTER YOUR MAILSERVER HERE!";
    SmtpMail.Send(mail); MessageBox.Show("Mail has been sent","It's Official",MessageBoxButtons.OK,MessageBoxIcon.Information);
    tabManageReviews_treeView1.SelectedNode=tabManageReviews_treeView1.Nodes[0];
    }
    catch(Exception ex)
    {

    MessageBox.Show("There was an erroe delivering your mail"+"The specific error message was \n\n:"+
    ex.Message,"Mail Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
       }
    这是发送email的主代码了,望高手指教
      

  2.   

    这里有篇类似的问题:
    http://www.cnblogs.com/NeoLee/archive/2005/03/28/127316.aspx不过
    SmtpMail.SmtpServer="ENTER YOUR MAILSERVER HERE!";//呵呵,这样的代码再也不要出现了,发邮件总归是需要发件服务器,如果不知道SMTP服务器的名称如何发??
      

  3.   

    我这里有一份发送EMAIL的完整代码供参考:
    System.Web.Mail.MailMessage mail = new MailMessage();
    mail.To="[email protected]";
    mail.From="[email protected]";
    mail.Priority = MailPriority.High;
    mail.Subject = "It's a test!";
    mail.Body = this.txt1.Text;
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
    "1");
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
    "wacle");//your email user name
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
    "password");//your password here
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing",2);
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver","");
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/languagecode",0x0804);
    SmtpMail.Send(mail);
      

  4.   

    谢了,不过你代码中的mail.Fields.Add是在做什么呢?查了一下msdn好像对这个没有做什么解释,请指教。是不是要自己在机器上开SMTP服务,本段程序才能在我机子上运行。
      

  5.   

    很久没来拉,抢分呀。现在好多红星星呀。http://fireball.cnblogs.com/articles/266496.html还没写完,作用就是设置DO.Configuration中的参数的。要查Exchange SDK.
    CDO是用于发送邮件的组件。mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing",2);1就是通过exchange来发。2就是通过SMTP服务器,3好像是用25的端口发。