首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

ASP.Net 发送电子邮件并增添附件

2012-08-28 
ASP.Net 发送电子邮件并添加附件在网上参考了一些资料,或多或少有些问题,老是提示用法已过时,最无奈的是我

ASP.Net 发送电子邮件并添加附件
在网上参考了一些资料,或多或少有些问题,老是提示用法已过时,最无奈的是我的input的ID在VS不能通过VS的代码补全功能显示出来,请大家指教,附上部分代码如下:
E-mail.aspx
[code=HTML][/code]<table style="left: 196px; position: relative; top: 0px">
  <tr>
  <td colspan="2"; style="width:800px;height:30px; background-color:#99ffff; text-align:center; font-family:隶书;">发送电子邮件</td>
  </tr>
  <tr>
  <td style="width:80px;height:30px;">
  收件人:</td>
  <td style="width:720px;height:30px;">
  <asp:TextBox ID="geter" runat="server" Style="position: relative;width:715px;height:28px;"></asp:TextBox></td>
  </tr>
  <tr>
  <td style="width:80px;height:30px;">
  发件人:</td>
  <td style="width:720px;height:30px;">
  <asp:TextBox ID="senter" runat="server" Style="position: relative;width:715px;height:28px;"></asp:TextBox></td>
  </tr>
  <tr>
  <td style="width:80px;height:30px;">
  附 件:</td>
  <td style="width:720px;height:30px;">
<input id="File1" style="position: relative" type="file" name="file1"/></td> (为什么代码补全能不能显示File1,而其他的如senter,geter都可以在提示中找到???) </tr>
  <tr>
  <td style="width:80px;height:30px;">
  主 题:</td>
  <td style="width:720px;height:30px;">
  <asp:TextBox ID="TalkTitle" runat="server" Style="position: relative;width:715px;height:28px;"></asp:TextBox></td>
  </tr>
  <tr>
  <td style="width:80px;height:350px;">
  正 文:</td>
  <td style="width:720px;height:350px;">
  <asp:TextBox ID="TalkBody" runat="server" Style="position: relative;width:715px;height:348px;" TextMode="MultiLine"></asp:TextBox></td>
  </tr>
  <tr>
  <td style="width:800px;height:30px;" colspan="2">
  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  <asp:Button ID="SendButton" runat="server" Style="position: relative" Text="发送" OnClick="SendButton_Click" /></td>
  </tr>
  </table>

E-mail.aspx.cs
protected void SendButton_Click(object sender, EventArgs e)
  {
  MailMessage objMailMessage;
  MailAttachment objMailAttachment;

  // 创建一个附件对象


  objMailAttachment = new MailAttachment("D:\\mail.txt");//发送邮件的附件
  // 创建邮件消息
  objMailMessage = new MailMessage();
  objMailMessage.From = "mingsheng@yeah.com";//源邮件地址
  objMailMessage.To = this.geter.Text.ToString();//目的邮件地址,也就是发给我哈
  objMailMessage.Subject = this.TalkTitle.Text.ToString();//发送邮件的标题
  objMailMessage.Body = "邮件发送标内容:测试一下是否发送成功!";//发送邮件的内容
  objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中

  //SMTP地址
  SmtpMail.SmtpServer = "192.168.1.100";

  //开始发送邮件
  SmtpMail.Send(objMailMessage);


  }
警告2“System.Web.Mail.MailMessage”已过时:“The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202”E:\C#\ASP.net\MingSheng\Web\E-mail.aspx.cs229E:\...\Web\
警告3“System.Web.Mail.MailAttachment”已过时:“The recommended alternative is System.Net.Mail.Attachment. http://go.microsoft.com/fwlink/?linkid=14202”E:\C#\ASP.net\MingSheng\Web\E-mail.aspx.cs239E:\...\Web\
警告4“System.Web.Mail.MailAttachment”已过时:“The recommended alternative is System.Net.Mail.Attachment. http://go.microsoft.com/fwlink/?linkid=14202”E:\C#\ASP.net\MingSheng\Web\E-mail.aspx.cs2633E:\...\Web\
错误5“System.Web.UI.WebControls.FileUpload”是“类型”,但此处被当做“变量”来使用E:\C#\ASP.net\MingSheng\Web\E-mail.aspx.cs2648E:\...\Web\
警告6“System.Web.Mail.MailMessage”已过时:“The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202”E:\C#\ASP.net\MingSheng\Web\E-mail.aspx.cs2830E:\...\Web\
警告7“System.Web.Mail.SmtpMail”已过时:“The recommended alternative is System.Net.Mail.SmtpClient. http://go.microsoft.com/fwlink/?linkid=14202”E:\C#\ASP.net\MingSheng\Web\E-mail.aspx.cs




[解决办法]
td style="width:720px;height:30px;"> 
<input id="File1" style="position: relative" type="file" name="file1"/> </td> (为什么代码补全能不能显示File1,而其他的如senter,geter都可以在提示中找到???) </tr> 
------------------------------

应为那两个是服务器端控件,所以在CS页能够访问,如果你想FILE1 也在后台访问,需要设置它runat="server"
[解决办法]

C# code
    using System;    using System.Net.Mail;    using System.Net;        /// <summary>     /// 说明:在.net2.0以上版本中发送电子邮件的方法示例     /// 用到的类主要位于System.Net.Mail和System.Net命名空间下     /// 作者:周公     /// 日期:2008-08-08     /// 首发地址:http://blog.csdn.net/zhoufoxcn     /// </summary>     public class SendMail2    {        public SendMail2()        {        }            /// <summary>         /// 发送邮件         /// </summary>         /// <param name="to">收件人邮件地址</param>         /// <param name="from">发件人邮件地址</param>         /// <param name="subject">邮件主题</param>         /// <param name="body">邮件内容</param>         /// <param name="username">登录smtp主机时用到的用户名,注意是邮件地址'@'以前的部分</param>         /// <param name="password">登录smtp主机时用到的用户密码</param>         /// <param name="smtpHost">发送邮件用到的smtp主机</param>         public void Send(string to, string from, string subject, string body, string userName, string password, string smtpHost)        {            MailAddress from = new MailAddress(from);            MailAddress to = new MailAddress(to);            MailMessage message = new MailMessage(from, to);            message.Subject = subject;//设置邮件主题             message.IsBodyHtml = true;//设置邮件正文为html格式             message.Body = body;//设置邮件内容             SmtpClient client = new SmtpClient(smtpHost);            //设置发送邮件身份验证方式             //注意如果发件人地址是abc@def.com,则用户名是abc而不是abc@def.com             client.Credentials = new NetworkCredential(userName, password);            client.Send(message);        }    } 


[解决办法]
首先引用命名空间System.Net.Mail;
protected void Button1_Click(object sender, EventArgs e)
{

string sender="xx@163.com";//发件人邮箱
string receiver="xx@foxmail.com";//收件人邮箱
string subject="测试邮件"; //邮件主题
string content="测试邮件"; //邮件内容
MailMessage newMail = new MailMessage(sender,receiver, subject, content);
newMail.IsBodyHtml = true;
newMail.Priority = System.Net.Mail.MailPriority.High;//邮件发送优先级高
//创建邮件附件
string filePath = this.txtFile.PostedFile.FileName;
FileInfo fi;
if (filePath != "")
{
fi = new FileInfo(filePath);
if (fi.Exists)
{
System.Net.Mail.Attachment newAttachment = new Attachment (filePath,System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition dispostion = newAttachment.ContentDisposition;
dispostion.CreationDate = System.IO.File.GetCreationTime(filePath);
dispostion.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
dispostion.ReadDate = System.IO.File.GetLastAccessTime(filePath);
newMail.Attachments.Add(newAttachment);
}
//发送邮件
string smtpServer="smtp.163.com";
System.Net.Mail.SmtpClient client = new SmtpClient(smtpServer, 25);
string user="xx"; 登陆邮箱用户名
string password="xx"; 登陆邮箱密码
client.Credentials = new System.Net.NetworkCredential(user,password);
try
{
client.Send(newMail);
Response.Write("<script language='javascript'>alert('恭喜您,邮件发送成功!')</script>");
}
catch
{

Page.RegisterClientScriptBlock("信息提示", "<script language='javascript'>alert('错误提示:邮件发送失败,请检查网络连接是否正常或表单元素是否填写错误!')</script>");
}

}
else
{
Page.RegisterClientScriptBlock("错误提示", "<script language='javascript'>alert('错误提示:发件邮箱或收件邮箱不能为空!')</script>");
}


}

热点排行