c#发送邮件,附件损坏

2020-12-09 17:14发布

问题:

//先将文件Copy成另一个文件

for (int i = 0; i < ds.Rows.Count; i++)
{
	string filepath = "被Copy文件的路径";
	string filepath_copy = "Copy文件的路径";
	if (System.IO.File.Exists(filepath) && !System.IO.File.Exists(filepath_copy))
	{
		System.IO.File.Copy(filepath, filepath_copy);
	}
	path += "," + filepath_copy;
}

//Copy出来的文件本地能打开,发送邮件后

SendMail(string email,string title, string content, out string reason, string filePath)
try
{
	MailAddress sender = new MailAddress(略);
	MailMessage message = new MailMessage();
	message.From = sender;

	string[] emails = email.Split(',');
	for (int i = 0; i < emails.Length; i++)
	{
		message.To.Add(new MailAddress(emails[i]));
	}
	message.CC.Add(......);
	message.Subject = title;
	message.Body = content;
	message.IsBodyHtml = true;
	if (!string.IsNullOrEmpty(filePath))
	{
		string[] paths = filePath.Split(',');
		for (int i = 0; i < paths.Length; i++)
		{
			message.Attachments.Add(new System.Net.Mail.Attachment(paths[i]));
		}
		
	}
	SmtpClient client = new SmtpClient();
	client.Host = "smtp.exmail.qq.com";
	client.DeliveryMethod = SmtpDeliveryMethod.Network;
	client.UseDefaultCredentials = false;
	client.Credentials = new NetworkCredential(略);
	client.Send(message);
	reason = "发送成功";
	back = 1;
}
catch (Exception ex)
{
	reason = "发送失败," + ex.Message;
	back = 0;
}

//文件大小是一模一样,但打开附件提示文件损坏
//删掉Copy的文件再来一次,又好了,怀疑是Copy中间的错误,有没有详细的解释

回答1:

试试这个
/**
* 防止截断文件,导致QQ邮箱等等,无法识别附件
*/
static {
System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
}



回答2:

设置一下MediaTypeNames试试



标签: c# smtp