现在的位置: 首页 > 综合 > 正文

c#发送带附件的邮件

2013年08月27日 ⁄ 综合 ⁄ 共 2785字 ⁄ 字号 评论关闭

网上找了很多发送邮件的代码,测试发现连接有问题,比如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("sunssss@126.com","egxsun@126.com","标题","正文",@"c:/deltable.rar"))
   {
    MessageBox.Show("发送成功!");
   }
   else
   {
    MessageBox.Show("发送失败!");
   }

【上篇】
【下篇】

抱歉!评论已关闭.