首页 新闻 会员 周边 捐助

c#发送邮件,附件损坏

0
悬赏园豆:10 [已解决问题] 解决于 2020-12-09 17:54

//先将文件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中间的错误,有没有详细的解释

TenFly的主页 TenFly | 初学一级 | 园豆:69
提问于:2020-11-20 11:35
< >
分享
最佳答案
0

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

收获园豆:5
此心不动,随机而动 | 菜鸟二级 |园豆:207 | 2020-11-27 17:22
其他回答(1)
0

设置一下MediaTypeNames试试

收获园豆:5
Jonny-Xhl | 园豆:691 (小虾三级) | 2020-11-20 13:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册