简单的ASP.Net邮件发送程序(一)
    今天学着写了一个简单的邮件发送程序,是用IIS的SMTP服务器发送的(下一步学习用Jmail组件发送),经测试后,成功向自己的邮箱发送了一封邮件。感觉挺有意思,希望自己以后能写出类似于Foxmail的软件,嗯,加油吧~~~
1、安装SMTP服务
安装此组件:'控制面板'->'添加或删除程序'->'添加或删除Windows组件'->'应用程序服务器'->'Internet信息服务(IIS)'->SMTP Service
2、配置SMTP虚拟机
在'控件面板'->'管理工具'->'IIS管理器'->'默认SMTP虚拟服务器'->'属性'中,填写本机的IP地址,其他项视需要选择。
3、设计邮件发送界面
收件人:tbReceiver
发件人:tbSender
发件人邮箱用户名:tbSenderUsername
发件人邮箱密码:tbSenderPsw
主题:tbSubject
内容:tbContent
附件:fuAttachment //fileUpload控件
发送:btnSend
4、主要用System.Net.Mail中的类来编写邮件发送程序
主要代码如下:
//引入3个命名空间
using System.IO;
using System.Net.Mail;
using System.Net.Mime;      
   protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.tbReceiver.Text != string.Empty && this.tbSender.Text != string.Empty)
            {
                //创建邮件
                MailMessage myMail = new MailMessage(this.tbSender.Text.Trim(), this.tbReceiver.Text.Trim(), this.tbSubject.Text.Trim(), this.tbContent.Text.Trim());
                myMail.Priority = MailPriority.High;
                //创建附件
                if (this.fuAttach.FileName!=null)
                {
                    string filePath = this.fuAttachment.PostedFile.FileName;
                    FileInfo fi = new FileInfo(filePath);
                    if (fi.Exists)
                    {
                        Attachment myAttachment = new Attachment(filePath, MediaTypeNames.Application.Octet);
                        ContentDisposition disposition = myAttachment.ContentDisposition;
                        disposition.CreationDate = File.GetCreationTime(filePath);
                        disposition.ModificationDate = File.GetLastWriteTime(filePath);
                        disposition.ReadDate = File.GetLastAccessTime(filePath);
                        myMail.Attachments.Add(myAttachment);
                    }
                }
                //发送附件
                SmtpClient client = new SmtpClient("smtp.163.com", 25);
                client.Credentials = new System.Net.NetworkCredential(this.tbUser.Text.Trim(), this.tbPsw.Text.Trim());
                client.Send(myMail);
                Response.Redirect("mailInfo.aspx"); //发送成功提示页面
            }
        }
        catch
        {
         Response.Write("<font color=red>邮件发送失败!原因可能是:1)收件人地址不存在 2)发件人地址不存在 3)用户名或密码错误</font>"); //错误信息提示
        }
        finally
        { }
    }
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号