none
使用system.net.mail出现不能发送带有冒号正文的邮件 RRS feed

  • 问题

  •     /// <summary> 
    
        /// 发送电子邮件 
    
        /// </summary> 
    
        /// <param name="MessageFrom">发件人邮箱地址</param> 
    
        /// <param name="MessageTo">收件人邮箱地址</param> 
    
        /// <param name="MessageSubject">邮件主题</param> 
    
        /// <param name="MessageBody">邮件内容</param> 
    
        /// <returns></returns> 
    
        public static bool Send(string MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
    
        {
    
            MailMessage message = new MailMessage();
    
     
    
             if (FileUpload1.PostedFile.FileName != "")
    
             {
    
             Attachment att = new Attachment("d://test.txt");//发送附件的内容
    
               message.Attachments.Add(att);
    
             }
    
     
    
            message.From = new MailAddress(MessageFrom);
    
            message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发 
    
            message.Subject = MessageSubject;
    
            message.Body = MessageBody;
    
            //message.Attachments.Add(objMailAttachment);
    
            message.IsBodyHtml = true; //是否为html格式 
    
            message.Priority = MailPriority.High; //发送邮件的优先等级 
    
     
    
            SmtpClient sc = new SmtpClient();
    
            sc.Host = "smtp.sina.cn"; //指定发送邮件的服务器地址或IP 
    
            sc.Port = 25; //指定发送邮件端口 
    
            sc.Credentials = new System.Net.NetworkCredential("****@sina.cn", "****"); //指定登录服务器的用户名和密码(发件人的邮箱登陆密码)
    
     
    
            try
    
            {
    
                sc.Send(message); //发送邮件 
    
            }
    
            catch
    
            {
    
                return false;
    
            }
    
            return true;
    
        }
    
    
    调用:
     string ss="http://baidu.com";
                    if (Send("kgfis@sina.cn", i.Trim(), "群发测试" + DateTime.Now.Second.ToString(),ss))
                    {
                        rep = "发送成功!";
                    }
                    else
                    {
                        rep = "发送失败";
                    }
    

    问题如题,症状是返回true,但目标邮箱没能收到邮件。最后用system.web.mail解决问题,想知道为什么 system.net.mail不能发送在正文中带有英文的冒号的邮件,还是我的代码有问题?希望各位高手能帮忙解答一下,谢谢。
    下面是 system.net.mail 的代码:
        public static bool Send(string from,string to,string subject,string body) 
    
    {
    
     
    
            try
    
            {
    
        string username="***@sina.cn";
    
        string password="********";
    
        MailMessage mailMsg = new MailMessage();
    
        mailMsg.BodyFormat = MailFormat.Html;
    
        mailMsg.To = to;
    
        mailMsg.From = from;
    
        mailMsg.Subject = subject;
    
        mailMsg.Body = body;
    
        mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
    
        mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",username);
    
        mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",password);
    
        SmtpMail.SmtpServer = "smtp.sina.cn";//smpt服务器
    
        SmtpMail.Send(mailMsg);
    
            }
    
            catch
    
            {
    
                return false;
    
            }
    
        return true;
    
        }
    
    
    
    2011年10月23日 4:20

答案

  • 你好,

    请参考下下面的代码:

    SmtpClient client = new SmtpClient(); 
     
            client.Host = "smtp.live.com"; 
            client.Port = 25; 
     
            client.Credentials = new NetworkCredential( 
                @"email_account", 
                @"email_password"); 
     
     
            client.EnableSsl = true; 
     
            // create message 
            MailMessage message = new MailMessage("eamil", "email"); 
            message.Body = "Hello email.<b>This is bold</b>"; 
            message.BodyEncoding = System.Text.Encoding.UTF8; 
            message.IsBodyHtml = true; 
            message.Subject = "werwerwer"; 
            message.SubjectEncoding = System.Text.Encoding.UTF8; 
     
     
            // send message 
            try 
            { 
                client.Send(message); 
     
     
            } 
            catch (SmtpException ex) 
            { 
     
                Response.Write(ex.Message); 
            } 
            finally 
            { 
                // Clean up. 
                message.Dispose(); 
            }
    

     

    2011年10月24日 8:13
    版主