本文介绍了我试了几次但是在邮件excel文件中没有收到邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的代码如下 int count = 0; string connectionstring =Server =(local); initial catalog = Test; Trusted_Connection = True; SqlConnection sqlConnection = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand(); SqlDataReader阅读器; DataSet ds = new DataSet(); cmd.CommandText =select * from Empdetails; cmd.CommandText + =where shifttype = @par; cmd.Parameters.Add(@ par,SqlDbType.Int).Value = j; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection; sqlConnection.Open(); reader = cmd.ExecuteReader(); if(reader.HasRows) { System.IO.StreamWriter sw_In = new System.IO.StreamWriter(@C:\ Users\God\Desktop \ DataDump \+ j +Excel.xls); while(reader.Read()) { if(count == 0) { for(int i = 0; i< ; reader.FieldCount; i ++) { MailMessage mis = new MailMessage(); SmtpClient smtpserver = new SmtpClient(smtp.gmail.com); smtpserver.Credentials = new System.Net.NetworkCredential([email protected],< password>); smtpserver.Host =smtp.gmail.com; smtpserver.Port = 587; smtpserver.EnableSsl = true; m is.From = new MailAddress([email protected],Report); mis.IsBodyHtml = true; mis.To.Add([email protected]); mis.CC.Add(new MailAddress([email protected])); mis.Subject =数据转储报告; smtpserver.Send(mis); sw_In.AutoFlush = true; sw_In.Write(reader.GetName(i)+\t); } sw_In.Write(\ n); count = 1; } for(int i = 0; i< reader.FieldCount; i ++) { sw_In.AutoFlush = true; sw_In.Write(reader [i] .ToString()+\t); } sw_In.Write(\ n); } } sqlConnection.Close(); reader.Close(); 桌面下的将以Data Dump文件夹excel文件的名义下载。 i我将那个excel文件发送到邮件。 发送邮件,我写了上面的代码。 邮件功能代码如下 MailMessage mis = new MailMessage(); SmtpClient smtpserver = new SmtpClient(smtp.gmail.com); smtpserver.Credentials = new System.Net.NetworkCredential([email protected],12345); smtpserver.Host =smtp.gmail.com; smtpserver.Port = 587; smtpserver.EnableSsl = true; mis.From = new MailAddress([email protected],Report); mis.IsBodyHtml = true; mis.To.Add([email protected]); mis.CC.Add(new MailAddress([email protected])); mis.Subject =数据转储报告; smtpserver.Send(mis); < / ore> 但是当我运行上面的代码时,在邮件中我只获得数据转储报告。 excel文件是我没有收到邮件。 请帮我解决上面代码中的错误 我尝试了什么: i多次试过使用c# 将excel文件发送到邮件当我运行上面的代码时,在邮件中我只获得数据转储报告。 excel文件是我没有收到邮件。 请帮我解决上面代码中的错误 解决方案 在您显示的代码中, MailMessage mis = new MailMessage(); SmtpClient smtpserver = new SmtpClient( smtp .gmail.com); smtpserver.Credentials = new System.Net.NetworkCredential( [email protected], 12345); smtpserver.Host = smtp.gmail.com; smtpserver.Port = 587 ; smtpserver.EnableSsl = true ; mis.From = new MailAddress( [email protected], 报告); mis.IsBodyHtml = true ; mis.To.Add( [email protected]); mis.CC.Add( new MailAddress( [email protected])); mis.Subject = 数据转储报告; smtpserver.Send(mis); 你在哪里附上Excel文件?其次, 引用:当我运行上面的代码时,在邮件中我只获得数据转储报告主题。 究竟在哪里添加除主题之外的其他内容?身体缺失,附件丢失,这就是为什么你没有得到除此之外的任何东西。 首先添加以下行, mis.Body = < p>此电子邮件包含Excel电子表格的附件。< / p>; mis.Attachments.Add( new 附件( C:\Path\To\ExcelFile.xls)); // 发送此信息。 最后,电子邮件将使用正文内容和附加到电子邮件的附件发送。 如需更多信息,请阅读: 附件构造函数(String)(System.Net.Mail) [ ^ ] MailMessage.Attachments Property(System.Net.Mail) [ ^ My code as follows int count = 0; string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";SqlConnection sqlConnection = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand(); SqlDataReader reader; DataSet ds = new DataSet(); cmd.CommandText = "select * from Empdetails"; cmd.CommandText += " where shifttype = @par "; cmd.Parameters.Add("@par", SqlDbType.Int).Value = j; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection; sqlConnection.Open(); reader = cmd.ExecuteReader(); if (reader.HasRows) { System.IO.StreamWriter sw_In = new System.IO.StreamWriter(@"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls"); while (reader.Read()) { if (count == 0) { for (int i = 0; i < reader.FieldCount; i++) { MailMessage mis = new MailMessage(); SmtpClient smtpserver = new SmtpClient("smtp.gmail.com"); smtpserver.Credentials = new System.Net.NetworkCredential("[email protected]","<password>"); smtpserver.Host = "smtp.gmail.com"; smtpserver.Port = 587; smtpserver.EnableSsl = true; mis.From = new MailAddress("[email protected]", "Report"); mis.IsBodyHtml = true; mis.To.Add("[email protected]"); mis.CC.Add(new MailAddress("[email protected]")); mis.Subject = "Data Dump Report"; smtpserver.Send(mis); sw_In.AutoFlush = true; sw_In.Write(reader.GetName(i) + "\t"); } sw_In.Write("\n"); count = 1; } for (int i = 0; i < reader.FieldCount; i++) { sw_In.AutoFlush = true; sw_In.Write(reader[i].ToString() + "\t"); } sw_In.Write("\n"); } } sqlConnection.Close(); reader.Close();in desktop under the name of Data Dump folder excel file will be download.i am sending that excel file to mail.for that sending mail, i written the code above.Mail function code as follows MailMessage mis = new MailMessage(); SmtpClient smtpserver = new SmtpClient("smtp.gmail.com"); smtpserver.Credentials = new System.Net.NetworkCredential("[email protected]","12345"); smtpserver.Host = "smtp.gmail.com"; smtpserver.Port = 587; smtpserver.EnableSsl = true; mis.From = new MailAddress("[email protected]", "Report"); mis.IsBodyHtml = true; mis.To.Add("[email protected]"); mis.CC.Add(new MailAddress("[email protected]")); mis.Subject = "Data Dump Report"; smtpserver.Send(mis);</ore>but when i run the above code, in mail i getting only subject as Data Dump Report.The excel file is i am not getting in mail.please help me what is the mistake in my above codeWhat I have tried:i tried several times to send excel file to mail using c#When i run the above code, in mail i getting only subject as Data Dump Report.The excel file is i am not getting in mail.please help me what is the mistake in my above code 解决方案 In the code that you show, MailMessage mis = new MailMessage(); SmtpClient smtpserver = new SmtpClient("smtp.gmail.com"); smtpserver.Credentials = new System.Net.NetworkCredential("[email protected]","12345"); smtpserver.Host = "smtp.gmail.com"; smtpserver.Port = 587; smtpserver.EnableSsl = true; mis.From = new MailAddress("[email protected]", "Report"); mis.IsBodyHtml = true; mis.To.Add("[email protected]"); mis.CC.Add(new MailAddress("[email protected]")); mis.Subject = "Data Dump Report"; smtpserver.Send(mis);Where exactly are you attaching the Excel file? Secondly, Quote:when i run the above code, in mail i getting only subject as Data Dump Report.Where exactly, are you adding anything other than the subject? The body is missing, attachments are missing and that is why you don't get anything other than that. Start by adding the following lines, mis.Body = "<p>This email contains attachment for an Excel spreadsheet.</p>";mis.Attachments.Add(new Attachment("C:\Path\To\ExcelFile.xls"));// Send this.Finally, the email would be sent using the body content and the attachments attached to the email. For more, please read:Attachment Constructor (String) (System.Net.Mail)[^]MailMessage.Attachments Property (System.Net.Mail)[^] 这篇关于我试了几次但是在邮件excel文件中没有收到邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 14:38