none
ASP.NET发送邮件程序 RRS feed

  • 问题

  • 我的代码:            try
                {
                    MailMessage msg = new MailMessage();
                    MailAddress address = new MailAddress("发送的邮箱");
                    MailAddressCollection collection = new MailAddressCollection();
                    collection.Add("接收的邮箱");
                    msg.From = address;
                    msg.To.Add(collection[0].Address.ToString());
                    msg.IsBodyHtml = true;
                    msg.Body = "这是我的测试邮件";
                    string hostName = Dns.GetHostName();
                    IPAddress[] ipAddress = Dns.GetHostAddresses(hostName);
                    string ip = ipAddress[0].ToString();
                    SmtpClient client = new SmtpClient();
                    client.Host = "qq.smtp.com";
                    client.Send(msg);
                }
                catch (Exception ex)
                {
                    Response.Write("<script language='javascript'>alert('" + ex.Message + "')</script>");
                }

    但是,始终提示发送失败。在这里,我只是写了代码,没有在WEB.CONFIG里面进行配置,也没有设置SMTP服务,请问,代码有问题吗?请给出正确的提示,谢谢!

    2011年3月22日 3:17

答案

  • System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                    msg.From = new MailAddress("****@hotmail.com", "名称", System.Text.Encoding.UTF8);
                    msg.To.Add("******@qq.com");
                    msg.Subject = "这是测试邮件";//邮件标题
                    msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
                    msg.Body = "邮件验证:这里是内容";//邮件内容
                    msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
                    msg.IsBodyHtml = true;//是否是HTML邮件
                    msg.Priority = MailPriority.High;//邮件优先
                    SmtpClient client = new SmtpClient();
                    client.Credentials = new System.Net.NetworkCredential("*****@hotmail.com", "这个邮箱的密码");
                    client.Port = 25;
                    client.Host = "smtp.live.com";
                    //client.Host = "192.168.0.1";
                    client.EnableSsl = true;
                    client.Send(msg);

    这是我后来才改正的,好像只能给QQ邮箱发邮件。


    2011年3月22日 5:13