C# 用电子邮件C将目录作为附件发送#
可以通过电子邮件发送目录吗 我正在使用此方法将日志文件发送到我的电子邮件,但我想发送一个目录,该目录将是我在Unity中工作的游戏的“保存游戏”文件夹。 我用这个作为bug报告,因为我需要得到游戏保存文件来查找bugC# 用电子邮件C将目录作为附件发送#,c#,email,unity3d,unity5,C#,Email,Unity3d,Unity5,可以通过电子邮件发送目录吗 我正在使用此方法将日志文件发送到我的电子邮件,但我想发送一个目录,该目录将是我在Unity中工作的游戏的“保存游戏”文件夹。 我用这个作为bug报告,因为我需要得到游戏保存文件来查找bug using System.Net; using System.Net.Mail; public void email_send() { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = ne
using System.Net;
using System.Net.Mail;
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your mail@gmail.com");
mail.To.Add("to_mail@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
如果无法发送目录,解决方法是什么?您可以压缩完整的目录内容并将存档文件作为附件发送。解决方法:只需压缩并作为文件发送即可 .NET的GZip类可能正是您想要的! 压缩片段就在那里
public static void Compress(DirectoryInfo directorySelected)
{
foreach (FileInfo fileToCompress in directorySelected.GetFiles())
{
using (FileStream originalFileStream = fileToCompress.OpenRead())
{
if ((File.GetAttributes(fileToCompress.FullName) &
FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
{
using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
{
using (GZipStream compressionStream = new GZipStream(compressedFileStream,
CompressionMode.Compress))
{
originalFileStream.CopyTo(compressionStream);
}
}
FileInfo info = new FileInfo(directoryPath + "\\" + fileToCompress.Name + ".gz");
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fileToCompress.Name, fileToCompress.Length.ToString(), info.Length.ToString());
}
}
}
}
您还可以使用标准的压缩类从这里开始使用该目录创建一个zip并发送zipy您可以压缩目录并发送它您可以将目录中的每个文件作为单独的附件发送。