电子邮件附件作为文件而不是 zip 文件发送
Email attachment is being sent as a file instead of a zip file
所以我试图将 pdf 的 zip 文件附加到电子邮件中,它以文件而不是 zip 的形式出现。我可以用记事本打开它,但它只是一堆随机字符。
这是我发送邮件的方法:
public static void SendEmail(List<string> recipients, MemoryStream output, string from, string subject, string htmlMessage, bool isHtml = true)
{
var host = ConfigurationManager.AppSettings["emailHost"];
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
foreach (var r in recipients)
{
mail.To.Add(r);
}
mail.Subject = subject;
mail.IsBodyHtml = isHtml;
mail.Body = htmlMessage;
//string result = System.Text.Encoding.UTF8.GetString(output.ToArray());
SmtpClient SmtpServer = new SmtpClient(host);
SmtpServer.Port = 25;
Attachment myZip = new Attachment(output, "Client Statement");
mail.Attachments.Add(myZip);
SmtpServer.Send(mail);
}
catch (Exception ex)
{
FMBUtilities.Logger.LogErrorToSql2012PrdAndEmailTeam("DBQueries", "SendEmail", ex);
}
}
我在这里从我的控制器调用它:
// Make array of emails into List for sending in email
if (emails.ToString() != "")
{
var allEmails = emails[0].Split(',');
foreach (var email in allEmails)
{
if (emailValid.IsMatch(email))
{
everyEmail.Add(email);
}
else
{
return Json(new { success = false, message = $"* Not valid email address: {email}.\n\n * Please double check and try again." });
}
MemoryStream output = new MemoryStream();
List<string> distinctFiles = allPaths
.GroupBy(x => x.Split(new char[] { '\' }).Last())
.Select(x => x.First())
.ToList();
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(distinctFiles, @"\");
zip.Save(output);
output.Position = 0;
DBQueries.SendEmail(everyEmail, output, fromAddress, "Client Statement Reports", "Here are your requested Client Statements", true);
}
我有另一种用于下载 zip 文件的控制器方法,因此我知道它正在正确下载并且一切正常。我似乎无法弄清楚如何将此 zip 文件附加到电子邮件中。
编辑:
Here is an image of the file thats coming in from the attachment.
[![enter image description here][1]][1]
该文件以“PK”开头 - 它是一个 zip,但因为您是从内存流中制作的并且似乎没有指定文件名,所以它会像默认名称一样通过
查看附件的 ContentDisposition
属性 和附件的 FileName
属性,例如
myZip.ContentDisposition.FileName = "myFile.zip";
看起来它实际上是一个 zip 文件。但没有文件扩展名 .zip。我想有一种方法可以手动命名文件以提供扩展名为 .zip 的名称(例如 test.zip)
所以我试图将 pdf 的 zip 文件附加到电子邮件中,它以文件而不是 zip 的形式出现。我可以用记事本打开它,但它只是一堆随机字符。
这是我发送邮件的方法:
public static void SendEmail(List<string> recipients, MemoryStream output, string from, string subject, string htmlMessage, bool isHtml = true)
{
var host = ConfigurationManager.AppSettings["emailHost"];
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
foreach (var r in recipients)
{
mail.To.Add(r);
}
mail.Subject = subject;
mail.IsBodyHtml = isHtml;
mail.Body = htmlMessage;
//string result = System.Text.Encoding.UTF8.GetString(output.ToArray());
SmtpClient SmtpServer = new SmtpClient(host);
SmtpServer.Port = 25;
Attachment myZip = new Attachment(output, "Client Statement");
mail.Attachments.Add(myZip);
SmtpServer.Send(mail);
}
catch (Exception ex)
{
FMBUtilities.Logger.LogErrorToSql2012PrdAndEmailTeam("DBQueries", "SendEmail", ex);
}
}
我在这里从我的控制器调用它:
// Make array of emails into List for sending in email
if (emails.ToString() != "")
{
var allEmails = emails[0].Split(',');
foreach (var email in allEmails)
{
if (emailValid.IsMatch(email))
{
everyEmail.Add(email);
}
else
{
return Json(new { success = false, message = $"* Not valid email address: {email}.\n\n * Please double check and try again." });
}
MemoryStream output = new MemoryStream();
List<string> distinctFiles = allPaths
.GroupBy(x => x.Split(new char[] { '\' }).Last())
.Select(x => x.First())
.ToList();
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(distinctFiles, @"\");
zip.Save(output);
output.Position = 0;
DBQueries.SendEmail(everyEmail, output, fromAddress, "Client Statement Reports", "Here are your requested Client Statements", true);
}
我有另一种用于下载 zip 文件的控制器方法,因此我知道它正在正确下载并且一切正常。我似乎无法弄清楚如何将此 zip 文件附加到电子邮件中。
编辑:
Here is an image of the file thats coming in from the attachment. [![enter image description here][1]][1]
该文件以“PK”开头 - 它是一个 zip,但因为您是从内存流中制作的并且似乎没有指定文件名,所以它会像默认名称一样通过
查看附件的 ContentDisposition
属性 和附件的 FileName
属性,例如
myZip.ContentDisposition.FileName = "myFile.zip";
看起来它实际上是一个 zip 文件。但没有文件扩展名 .zip。我想有一种方法可以手动命名文件以提供扩展名为 .zip 的名称(例如 test.zip)