邮件可以发送,但是文件格式不对...
代码如下:
private void sendToEmail(string toEmail, string subject, byte[] by, string name)
{
MailMessage mail = new MailMessage("发送人邮件", toEmail);
mail.SubjectEncoding = Encoding.Default;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.BodyEncoding = Encoding.Default;
mail.Body = "<strong>System.Net.Mail</strong>";
Stream stream = new MemoryStream(by);
mail.Attachments.Add(new Attachment(stream, "prova.xlsx", MediaTypeNames.Text.Plain));
SmtpClient smtp = new SmtpClient("mail.ultrapower.com.cn");
smtp.Port = 587;
smtp.Credentials = new NetworkCredential("发送人邮件", "密码"); //邮件 验证
smtp.Send(mail);
mail.Attachments.Dispose(); //邮件发送完毕,释放对附件的锁定
}
protected void btnsender_Click(object sender, EventArgs e)
{
List<en> list = new List<en>();
for (int i = 0; i < 100; i++)
{
en enen = new en();
enen.name = " name " + i;
enen.age = " age " + i;
list.Add(enen);
}
byte[] by = ObjectToByte((object)list);
Stream stream = new MemoryStream(by);
sendToEmail("收信人邮件", "hello", by, "");
}
public static byte[] ObjectToByte(object obj)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.IFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf.Serialize(stream, obj);
byte[] bytes = stream.GetBuffer();
stream.Close();
return bytes;
}