天天看點

c#發送帶附件的郵件

網上找了很多發送郵件的代碼,測試發現連接配接有問題,比如tcp = new TcpClient (server, port);用server=smtp.126.com,port=25,連接配接不上,下面這個簡單的如果關閉防火牆和mcafee防毒軟體就可以發送帶附件的郵件。

using System;

using System.Web.Mail;

namespace egxsun

{

 ///

 /// SystemMail 的摘要說明。

 ///

 public class SystemMail

 {

  private string _adminEmail;

  private string _smtpServer = "localhost";

  private string _password;

  private string _userName;

  public SystemMail()

  {            

  }

  public string AdminEmail

  {

   get{return _adminEmail;}

   set{_adminEmail = value;}

  }

  public string SmtpServer

  {

   get{return _smtpServer;}

   set{_smtpServer = value;}

  }

  public string Password

  {

   get{return _password;}

   set{_password = value;}

  }

  public string UserName

  {

   get{return _userName;}

   set{_userName = value;}

  }

  public bool Send(string to, string from, string subject, string message)

  {

   try

   {

    MailMessage em = new MailMessage();

    em.To = to;

    em.From = from;

    em.Subject = subject;

    em.Body = message;

    em.Attachments.Add( new MailAttachment("filename"));

    //Found out how to send authenticated email via System.Web.Mail at http://SystemWebMail.com (fact 3.8)

    if(this.UserName != null && this.Password != null)

    {

     em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");   //basic authentication

     em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.UserName); //set your username here

     em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.Password); //set your password here

    }

    SmtpMail.SmtpServer = this.SmtpServer;

    SmtpMail.Send(em);

    return true;

   }

   catch

   {

    return false;

   }

  }

  public bool Send(string to, string from, string subject, string body,string filename)

  {

   try

   {

    MailMessage em = new MailMessage();

    em.To = to;

    em.From = from;

    em.Subject = subject;

    em.Body = body;

    em.Attachments.Add( new MailAttachment(filename));

    //Found out how to send authenticated email via System.Web.Mail at http://SystemWebMail.com (fact 3.8)

    if(this.UserName != null && this.Password != null)

    {

     em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");   //basic authentication

     em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.UserName); //set your username here

     em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.Password); //set your password here

    }

    SmtpMail.SmtpServer = this.SmtpServer;

    SmtpMail.Send(em);

    return true;

   }

   catch//(Exception ex)

   {

    //string ss =ex.ToString();

    return false;

   }

  }

 }

}

使用:

  SystemMail sm = new SystemMail();

   sm.SmtpServer ="smtp.126.com";

   sm.UserName = "******";

   sm.Password ="******";

   if(sm.Send("[email protected]","[email protected]","标題","正文",@"c:/deltable.rar"))

   {

    MessageBox.Show("發送成功!");

   }

   else

   {

    MessageBox.Show("發送失敗!");

   }

繼續閱讀