自动发送E-mail

private void btnSend_Click(object sender, System.EventArgs e)
  {
   
//创建一个MailMessage对象
   MailMessage aMail=new MailMessage ();
   
//收信人地址
   aMail.To=txtTo.Text;
   
//发信人地址
   aMail.From=txtFrom.Text;
   
//主题
   aMail.Subject=txtSubject.Text;
   
//正文
   aMail.Body=txtBody.Text;
   aMail.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");//设置服务器验证
   aMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","songhuan1229@163.com");//设置用户名
   aMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","530520");//设置用户密码
   
//传先级别
   aMail.Priority=(MailPriority)ddPriority.SelectedIndex;
   
//正文格式
   aMail.BodyFormat=(MailFormat)ddBodyFormat.SelectedIndex;
   ddPriority.SelectedIndex
=1;
   ddBodyFormat.SelectedIndex
=0;
   
if(File1.PostedFile.FileName  !=" ")
   {
    
//上传文件的文件名(含完整路径)
    String fileName=File1.PostedFile.FileName;
    fileName
=fileName.Substring(fileName.LastIndexOf(@""));
    
//使用SaveAs方法,将文件保存在项目路径/temp 下,
    
//需要在项目路径下创建TEMP 目录
    fileName=Server.MapPath(@" emp"+fileName);
    File1.PostedFile.SaveAs(fileName);
    
//添加附件
    aMail.Attachments.Add(new MailAttachment(fileName));
   }
   
string result="";
   
try
   {
    
//发送邮件
    SmtpMail.SmtpServer="smtp.163.com";
    SmtpMail.Send(aMail);
    result
="Email  发送成功!";
   }
   
catch(Exception ex)
   {
    result
="Email 发送失败."+ex.Message;
   }
   Response.Write(
"");
             
  }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值