首页 新闻 会员 周边

用Attachments stream 发邮件附件exexl?

0
悬赏园豆:80 [已关闭问题] 关闭于 2012-05-08 19:54

邮件可以发送,但是文件格式不对...

代码如下:

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;
}

乐上的主页 乐上 | 初学一级 | 园豆:8
提问于:2012-05-08 17:17
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册