当前位置:编程学习 > C#/ASP.NET >>

.net 制作能发送EMail

我现在要做一个  用户成功注册后 就发一个email给用户 里面有相关的密码保护信息,但不知道如何下手,请高手指教,希望能说的详细点,源代码也可以, 谢谢 --------------------编程问答-------------------- 当然可以啊,你在网上看看JMail的使用吧! --------------------编程问答-------------------- 可以的
假如是买的域名送企业邮箱就好办

假如是本机试,会有些麻烦 --------------------编程问答-------------------- 用MSDN查一下system.net.mail类(.NET 2.0)。
下面是个工具代码
引用System.Net.Mail;命名空间
  /// <summary>
        /// 发送E-mail
        /// </summary>
        /// <param name="TO">收件人(仅支持单个收件人)</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容,支持 HTML</param>
        public static bool SendMail(string TO, string subject, string body, bool isHtml)
        {
            string[] batchTo = TO.Split(';');
            foreach (var to in batchTo)
            {
                MailMessage mail = new MailMessage("a@a.com", to);
                mail.From = new MailAddress(ConfigurationManager.AppSettings["SmtpSender"],
                                        ConfigurationManager.AppSettings["DisplayName"]);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = isHtml;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = ConfigurationManager.AppSettings["SmtpServer"];
                smtp.Port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
                System.Net.NetworkCredential credential = new System.Net.NetworkCredential();
                credential.UserName = ConfigurationManager.AppSettings["SmtpUsername"];
                credential.Password = ConfigurationManager.AppSettings["SmtpPin"];
                smtp.Credentials = credential;
                try
                {
                    smtp.Send(mail);
                    if (batchTo.Length == 1)
                        return true;
                }
                catch (SmtpException)
                {
                    if (batchTo.Length == 1)
                        return false;
                }
            }

            return true;
        }
--------------------编程问答--------------------

using System.Web.Mail;
using System.Net;
public void Fyj()//用户注册完可以调用这个发邮件方法
    {

        try
        {
            MailMessage Mail = new MailMessage();
            Mail.From = "wo123@163.com";//你要发邮件地址
            Mail.To = "ceshi@163.com";//目标地址(要发的用户邮箱地址)
            Mail.Subject = "测试发送邮件!";//邮件标题
            Mail.Body = "哈哈测试下";   //邮件内容
            Mail.BodyFormat = MailFormat.Html;
            Mail.Priority = MailPriority.High;
            Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "你的用户名");//用户名
       Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "你的邮箱密码");//密码
            System.Web.Mail.SmtpMail.SmtpServer = "smtp.163.com";
            System.Web.Mail.SmtpMail.Send(Mail);
            Response.Write("<script>alert('发送成功!')</script>");
        }
        catch (Exception exp)
        {
            Response.Write(exp.Message);
        }
    }
--------------------编程问答--------------------


先在bin目录里添加引用jmail.dll,然后注册一下,用regsvr32 路径\jmail.dll命令注册一下,就可以写程序了!
程序如下:
jmail.Message jmessage=new jmail.MessageClass();
jmessage.Charset="GB2312";//编码
jmessage.From="flcandclf@sohu.com";//发送人的邮件地址
jmessage.FromName= "系统管理员" ;//发送人的姓名
//jmessage.ReplyTo="dafen@ynable.com";//回复时的地址
jmessage.Subject="你的密码!";//邮件主题
jmessage.AddRecipient(email ,"","");//接收人的邮件地址,姓名
jmessage.Body= "你在本网站的帐号密码已经成功找回,请你妥善保管!卡号:"+cardid+"密码:"+cardpassword ;//邮件内容
jmessage.MailServerUserName= "flcandclf" ;//发送者邮箱的用户名
jmessage.MailServerPassWord=  "flc198068" ;//发送者邮箱的密码
jmessage.Send("smtp.sohu.com",false)  ;//发送着邮箱的smtp服务器地址
Response.Write("<script language='javascript'>alert('密码已成功发送到指定邮箱,请查收!');</script>");
jmessage.Close() ;


--------------------编程问答-------------------- http://www.cnblogs.com/doymoneya/articles/645645.html
看看这个 --------------------编程问答-------------------- 路过

上面写的已经很详细了

--------------------编程问答-------------------- 很多方法了,顶 --------------------编程问答-------------------- 我听说.net里面的using System.Web.Mail;
装了防火墙就不好用了。。。。 --------------------编程问答-------------------- 支持4楼 他的方法最简单 --------------------编程问答--------------------
protected void View3_Activate(object sender, EventArgs e)
    {
        ////设置发件人信箱,及显示名字
        MailAddress from = new MailAddress("gongxinkang@163.com", "J.L.C");
        //设置收件人信箱,及显示名字 
        MailAddress to = new MailAddress(model.Email, model.LoginName);
        //创建一个MailMessage对象
        MailMessage oMail = new MailMessage(from, to);

        oMail.Subject = "购物网";      //邮件标题       
        oMail.Body = "你的恢复密码是" + model.LoginName;         //邮件内容

        oMail.IsBodyHtml = true;            //指定邮件格式,支持HTML格式        
        oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码        
        oMail.Priority = MailPriority.High;//设置邮件的优先级为高

        //发送邮件服务器
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.163.com";    //指定邮件服务器
        client.Credentials = new NetworkCredential("gongxinkang@163.com", "*****");//指定服务器邮件,及密码

        //发送
        try
        {
            client.Send(oMail);  //发送邮件
            lbEmail.Text = "恭喜你!邮件发送成功。";

        }
        catch
        {
            lbEmail.Text = "邮件发送失败,检查网络及信箱是否可用。";
        }

        oMail.Dispose();        //释放资源     


    }
--------------------编程问答-------------------- 用System.Net.Mail发送就可以了,详细请看MSDN。 --------------------编程问答-------------------- 可以用JMAIL,也可用Mail
参考
http://www.cnblogs.com/jailu/archive/2007/04/10/707036.html

http://www.cnblogs.com/mac-j/archive/2008/08/08/1263705.html --------------------编程问答-------------------- http://topic.csdn.net/u/20080301/17/18568a4c-0c34-4bad-b0c5-08bc16a297ef.html --------------------编程问答-------------------- 1 System.Net.Mail
2 jmail --------------------编程问答-------------------- 学习了
--------------------编程问答-------------------- mark --------------------编程问答-------------------- using System.Net.Mail;试了很多SMTP发送邮件的代码,结果相同。 

代码: 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Net; 
using System.Net.Mail; 
using System.Net.Mime; 
using System.Threading; 
using System.ComponentModel; 

namespace 测试 
{              //smtp类 
    class Class1 
    { 
        static bool mailSent = false; 
        public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) 
        { 
            // Get the unique identifier for this asynchronous operation. 
            String token = (string)e.UserState; 

            if (e.Cancelled) 
            { 
                Console.WriteLine("[{0}] Send canceled.", token); 
            } 
            if (e.Error != null) 
            { 
                Console.WriteLine("[{0}] {1}", token, e.Error.ToString()); 
            } 
            else 
            { 
                Console.WriteLine("Message sent."); 
            } 
            mailSent = true; 
        } 
        public static void Main(string[] args) 
        { 
            string svr = "mail.lierda.com"; 
            // Command line argument must the the SMTP host. 
            SmtpClient client = new SmtpClient(svr, 25); 

            // Specify the e-mail sender. 
            // Create a mailing address that includes a UTF8 character 
            // in the display name. 
            MailAddress from = new MailAddress("zxyu@lierda.com"); 
              // "yuxiaozhong" + (char)0xD8 + "4821698",System.Text.Encoding.UTF8); 
            // Set destinations for the e-mail message. 
            MailAddress to = new MailAddress("zxyu@lierda.com"); 
            // Specify the message content. 
            MailMessage message = new MailMessage(from, to); 
            message.Body = "This is a test e-mail message sent by an application. "; 
            // Include some non-ASCII characters in body and subject. 
            string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' }); 
            message.Body += Environment.NewLine + someArrows; 
            message.BodyEncoding = System.Text.Encoding.UTF8; 
            message.Subject = "test message 1" + someArrows; 
            message.SubjectEncoding = System.Text.Encoding.UTF8; 
            // Set the method that is called back when the send operation ends. 
            client.SendCompleted += new  SendCompletedEventHandler(SendCompletedCallback); 
            // The userState can be any object that allows your callback  
            // method to identify this send operation. 
            // For this example, the userToken is a string constant. 
            string userState = "test message1"; 
            client.Credentials = new System.Net.NetworkCredential(from.Address, "11181118", "lierda.com");// 
            client.SendAsync(message, userState); 
            Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit."); 

            string answer = Console.ReadLine(); 

            // If the user canceled the send, and mail hasn't been sent yet, 
            // then cancel the pending operation. 
            //client.SendAsyncCancel(); 
            if (answer.StartsWith("c") && mailSent == false) 
            { 
                client.SendAsyncCancel(); 
            } 
            // Clean up. 
            message.Dispose(); 
            Console.WriteLine("Goodbye."); 
        } 
    } 
} --------------------编程问答-------------------- 在.net 这是很容易的事情
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,