.Net(C#)里邮件的发送

 .NET类库中名为System.Web.Mail的命名空间提供了3个不同的类,用来实现.Net平台下邮件的发送,分别是:
SmtpMail , MailMessage, MailAttachment

下面是用MailAttachment 实现带附件邮件的发送:

public void Mail_Send(Object src,EventArgs e)
{
 //创建MailMessage对象
 MailMessage MyMsg = new MailMessage();
 MyMsg.From  = tbFrom.Text;
 MyMsg.To  = tbTo.Text;
 MyMsg.Subject = tbSubject.Text;
 MyMsg.Priority = (MailPriority)ddlPriority.SelectedIndex;
 MyMsg.BodyFormat= (MailFormat)ddlBodyFormat.SelectedIndex;
 MyMsg.Body  = tbBody.Text;
 
 //如果有附件则上传
 HttpPostedFile hpfFile = AttachFile.PostedFile;
 if(hpfFile.FileName!="")
 {
  //有附件,则上传到Temp目录中
  //取得文件名(不含路径)
  char[] de = {'//'};
  string[] AFilename  = hpfFile.FileName.Split(de);
  string strFilename  = AFilename[AFilename.Length-1];
  string strPath  = Server.MapPath(".")+"//Temp//"+strFilename;
  hpfFile.SaveAs(strPath);
  //添加附件
  MyMsg.Attachments.Add(new MailAttachment(strPath));
 }

 try
 {
  //发送
  SmtpMail.Send(MyMsg);
  lblShowMsg.Text ="发送成功";
  tbTo.Text  = "";
  tbSubject.Text = "";
  tbBody.Text  = "";
  ddlPriority.SelectedIndex = 1;
  ddlBodyFormat.SelectedIndex = 0;
 }
 catch(Exception ee)
 {
  lblShowMsg.Text = "发送失败:"+ee.ToString();
 }
}

经测试,我163的邮箱能收到信,但是显示内容为:
This is an automatically generated Delivery Status Notification.

Delivery to the following recipients failed.
附件也不没有正常显示,且显示的发信人为postmaster@qgy .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值