简单的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
        { }
    }

       

转载于:https://www.cnblogs.com/ruiyi1987/archive/2009/03/14/1411328.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值