首页 新闻 搜索 赞助

asp.net如何实现给别人发邮件的功能?

0
悬赏园豆:200 [已关闭问题] 解决于 2009-03-25 16:38

用户在文本框里面输入了自己的邮箱地址后,点击发送按钮时,用户的邮箱就给我的邮箱发送了一封邮件!

请问如何实现???

网上有的代码不行,请测试可以之后在发过来!
谢谢!

(⊙o⊙)…的主页 (⊙o⊙)… | 初学一级 | 园豆:130
提问于:2009-03-24 17:48
< >
分享
其他回答(1)
0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace TestEmail
{
    public class Email
    {
        private string title;
        /// <summary>
        /// 邮件标题(主题)
        /// </summary>
        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        private string from;
        /// <summary>
        /// 发件人(XXXX@XXX.XXX)
        /// </summary>
        public string From
        {
            get { return from; }
            set { from = value; }
        }

        private string[] to;
        /// <summary>
        /// 收件人(XXXX@XXX.XXX)可多发
        /// </summary>
        public string[] To
        {
            get { return to; }
            set { to = value; }
        }
        private string content;
        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Content
        {
            get { return content; }
            set { content = value; }
        }
        private string serverName;
        /// <summary>
        /// 邮件服务器的地址(stmp.XXX.XXX)
        /// </summary>
        public string ServerName
        {
            get { return serverName; }
            set { serverName = value; }
        }
        private int serverPort;
        /// <summary>
        /// 邮件服务器的端口(一般为25)
        /// </summary>
        public int ServerPort
        {
            get { return serverPort; }
            set { serverPort = value; }
        }

        private string[] attach;
        /// <summary>
        /// 附件地址
        /// </summary>
        public string[] Attach
        {
            get { return attach; }
            set { attach = value; }
        }

        public bool IsSSL { get; set; }
        /// <summary>
        /// 发邮件的方法(bool ishaveAtt 表示是否有附件)
        /// </summary>
        public Boolean sendMail(string user, string password, bool ishaveAtt)
        {
            MailMessage mymail = new MailMessage();
            mymail.From = new MailAddress(From);

            foreach (string tmp in To)
            { mymail.To.Add(tmp); }
            if (ishaveAtt == true)
            {
                foreach (string att in Attach)
                {
                    Attachment atta = new Attachment(att);
                    mymail.Attachments.Add(atta);
                }
            }
            mymail.IsBodyHtml = true;
            mymail.Body = Content;
            mymail.Subject = Title;

            SmtpClient sc = new SmtpClient(ServerName, ServerPort);
            sc.EnableSsl = IsSSL;
            sc.Credentials = new System.Net.NetworkCredential(user, password);
            try
            {
                sc.Send(mymail);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

}

JGD | 园豆:205 (菜鸟二级) | 2009-03-24 18:00
0
winzheng | 园豆:8797 (大侠五级) | 2009-03-25 01:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册