protected void btnSend_Click(object sender, EventArgs e)
        {
            if (this.txtReceiver.Text != string.Empty && this.txtSender.Text != string.Empty)
            {
                MailMessage myMail = new MailMessage();
                myMail.From = new MailAddress(txtSender.Text.Trim(), "TT", Encoding.UTF8);
                myMail.To.Add(new MailAddress(txtReceiver.Text.Trim(), "Troy", Encoding.UTF8));
                myMail.Subject = txtSubject.Text.Trim();
                myMail.Body = txtContent.Text.Trim();
                myMail.Priority = MailPriority.High;
                string sFilePath = this.upFile.PostedFile.FileName;
                FileInfo fi = new FileInfo(sFilePath);
                if (fi.Exists)
                {
                    Attachment myAttachment = new Attachment(sFilePath, System.Net.Mime.MediaTypeNames.Application.Octet);
                    System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(sFilePath);
                    disposition.ModificationDate = File.GetLastWriteTime(sFilePath);
                    disposition.ReadDate = File.GetLastAccessTime(sFilePath);
                    myMail.Attachments.Add(myAttachment);
                }
                SmtpClient client = new SmtpClient("smtp.126.com", 25);
                client.Credentials = new System.Net.NetworkCredential(this.txtSUser.Text.Trim(), this.txtEPwd.Text.Trim());
                client.Send(myMail);
            }
        }这是代码,问题是,如果我在FileUpload控件中添加一个文件,路径是C:\user\desktop,那么FileInfo对象fi在调用exists()函数就永远是false,如果把if条件拿掉,提示该文件不在C:\Program Files (x86)\Common Files\microsoft shared\DevServer里面。然后我试着把文件放进这个文件夹中,又发现一个问题,提示是invalid mail header '周',为什么不添加附件什么问题都没有,只有一添加一个附件就会有问题呢?求高手解答!!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Collections;
    using System.Net;
    using System.Net.Mime;
    using System.Net.Mail;namespace SendEmail
    {
    class Program
    {
    //smtp.UseDefaultCredentials = true;
    //client.Credentials = CredentialCache.DefaultNetworkCredentials;
    static void Main(string[] args)
    {//DefaultSendEmail();
    // WriteEmail();
    Console.WriteLine(WriteEmail().ToString());
    //CreateMessageWithAttachment();
    }//Single receiver test.MSDN上最简单的发送邮件
    public static void DefaultSendEmail(string server)//你的邮件服务器
    {
    string to = "[email protected]";
    string from = "[email protected]";
    string subject = "Using the new SMTP client.";
    string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
    MailMessage message = new MailMessage(from, to, subject, body);SmtpClient client = new SmtpClient(server);
    //Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
    //client.Timeout = 100;
    // Credentials are necessary if the server requires the client
    // to authenticate before it will send e-mail on the client's behalf.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    client.Send(message);
    Console.WriteLine("Email sended.");
    }//Multiply users test.
    public static string MultiSendEmail(string mailFrom,string mailTo,string mailCC,string mailHead,string mailBody,ArrayList mailAttach,bool isHtml)
    {
    //改用XML文件在Web.Configure中配置.
    string eServer = "***.**.**.**";//需要换成你的邮件服务器
    int ePort = 25;//默认
    MailMessage eMail = new MailMessage();
    SmtpClient eClient = new SmtpClient(eServer,ePort);
    eClient.UseDefaultCredentials = true;eMail.Subject = mailHead;
    eMail.SubjectEncoding = Encoding.UTF8;eMail.Body = mailBody;
    eMail.BodyEncoding = Encoding.UTF8;eMail.From = new MailAddress(mailFrom);
    //Receiver
    string[] arrMailAddr;
    try
    {//用";"分割多个收件人.
    eMail.To.Clear();
    if(!string.IsNullOrEmpty(mailTo))
    {
    arrMailAddr=mailTo.Split(';');
    foreach(string strTo in arrMailAddr)
    {
    if(!string.IsNullOrEmpty(strTo))
    {
    eMail.To.Add(strTo);
    }
    }
    }//用";"分割多个抄送人.
    eMail.CC.Clear();
    if(!string.IsNullOrEmpty(mailCC))
    {
    arrMailAddr=mailCC.Split(';');
    foreach(string strCC in arrMailAddr)
    {
    if(!string.IsNullOrEmpty(strCC))
    {
    eMail.CC.Add(strCC);
    }
    }
    }//用";"分割多个秘件抄送人.
    //eMail.Bcc.Clear();
    //if(!string.IsNullOrEmpty(mailBCC))
    //{
    // arrMailAddr=mailBCC.Split(';');
    // foreach(string strBCC in arrMailAddr)
    // {
    // if(!string.IsNullOrEmpty(strBCC))
    // {
    // eMail.Bcc.Add(strBCC);
    // }
    // }
    //}
    if(isHtml)
    {
    eMail.IsBodyHtml=true;
    }
    else
    {
    eMail.IsBodyHtml=false;
    }//Attachment
    eMail.Attachments.Clear();
    if(mailAttach!=null)
    {
    for(int i=0;i<mailAttach.Count;i++)
    {
    if(!string.IsNullOrEmpty(mailAttach[i].ToString()))
    {
    eMail.Attachments.Add(new Attachment(mailAttach[i].ToString()));
    }
    }
    }
    eClient.Send(eMail);
    return "邮件已发送,请查收!";
    }
    catch(Exception ex)
    {
    return "邮件发送失败,原因:"+ex.Message.ToString();
    }}public static string strMailFrom;
    public static string strMailTo;
    public static string strMailCC;
    public static string strMailSubject;
    //public static string strStartTime;
    //public static string strEndTime;
    public static string strMailBody;
    public static ArrayList arrAttachment;
    public static string WriteEmail()
    {
    strMailFrom = "[email protected]";
    Console.WriteLine("请输入收件人邮箱,以"+";"+"间隔!");
    strMailTo = Console.ReadLine().ToString();
    strMailCC = "[email protected]";
    strMailSubject = "Test Mail";
    strMailBody = "Test Mail";arrAttachment=new ArrayList ();
    arrAttachment.Add(@"c:\cmd.txt");
    arrAttachment.Add(@"c:\job.txt");
    arrAttachment.Add(@"c:\t.txt");return MultiSendEmail(strMailFrom,strMailTo,strMailCC,strMailSubject,strMailBody,arrAttachment ,false );
    }//微软家自带的发送带附件.
    public static void CreateMessageWithAttachment(string server)
    {
    // Specify the file to be attached and sent.
    // This example assumes that a file named Data.xls exists in the
    // current working directory.
    string file = @"c:\t.txt";
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
    "[email protected]",
    "[email protected]",
    "Quarterly data report.",
    "See the attached spreadsheet.");// Create the file attachment for this e-mail message.
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
    // Add time stamp information for the file.
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
    // Add the file attachment to this e-mail message.
    message.Attachments.Add(data);
    //Send the message.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    client.Send(message);
    // Display the values in the ContentDisposition for the attachment.
    ContentDisposition cd = data.ContentDisposition;
    Console.WriteLine("Content disposition");
    Console.WriteLine(cd.ToString());
    Console.WriteLine("File {0}", cd.FileName);
    Console.WriteLine("Size {0}", cd.Size);
    Console.WriteLine("Creation {0}", cd.CreationDate);
    Console.WriteLine("Modification {0}", cd.ModificationDate);
    Console.WriteLine("Read {0}", cd.ReadDate);
    Console.WriteLine("Inline {0}", cd.Inline);
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
    foreach (DictionaryEntry d in cd.Parameters)
    {
    Console.WriteLine("{0} = {1}", d.Key, d.Value);
    }
    data.Dispose();
    }
    }
    }
    带附件
      

  2.   


    是不要再user文件夹里面吗?我试过了,还是无法发送附件附件只有放在DevServer文件夹里才会发送,但是mail header莫名报错不添加附件就不报错,这是为何。。
      

  3.   

    http://www.cnblogs.com/hymxtang/archive/2007/06/27/797247.html
    http://blog.csdn.net/lff642/archive/2008/07/15/2654346.aspx把这两个链接看完 你就OK了
      

  4.   


    这两篇文章用的System.Web.Mail类,已经被淘汰了。。我用的是System.Net.Mail。
    后来又看了下,发现只要给myAttachment.ContentDisposition设置各项内容的话,比如最后访问时间,最后修改时什么的,就会出错,把那段代码注释掉,附件就能发送出去了。是不是因为设置Disposition的时候编码出错了?
      

  5.   

    不允许有非英文字母?加下面的试试
    mail.SubjectEncoding = Encoding.UTF8;
      

  6.   

    可以查看一下此文章http://blog.csdn.net/flysworde/article/details/7842456
      

  7.   

    多数免费邮箱都不能用SMTP服务的~!
    QQ的才可以!
    smtp.126.com改成 smtp.qq.com
    并进入你的QQ邮箱设置允许amtp功能才能用~!