如何在C#中将RAR文件附加到电子邮件?

3
我想使用以下代码发送带有多个附件的电子邮件,但是对于RAR文件无法正常工作。问题出在哪里? 对于每个附件,我都有一个包含附加文件属性和内容的类:
public class AttachmentFile
{
    [StringLength(200)]
    public string FileName { get; set; }
    [StringLength(15)]
    public string Extension { get; set; }
    [StringLength(100)]
    public string Signature { get; set; }
    public byte[] Content { get; set; }
    [StringLength(500)]
    public string FullPath { get; set; } 
}

这里展示了Send方法:

public void Send(string from, List<string> recivers, string smtpHostName, string subject, string body, ICollection<AttachmentFile> attachmentFiles)
{
    var mailMessage = new MailMessage();
    foreach (var reciver in recivers)
    {
        mailMessage.To.Add(reciver);
    }

    mailMessage.Subject = subject;
    mailMessage.From = new MailAddress(@from);
    mailMessage.Body = RenderBody(body);
    if (attachmentFiles != null)
    {
        foreach (var attachmentFile in attachmentFiles)
        {
            var contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Octet);
            mailMessage.Attachments.Add(new Attachment(new MemoryStream(attachmentFile.Content), "application/rar"));
        }
    }

    mailMessage.IsBodyHtml = true;
    mailMessage.SubjectEncoding = Encoding.UTF8;
    var smtp = new SmtpClient { Host = smtpHostName };
    smtp.Send(mailMessage);
}
1个回答

5

请尽可能简短地描述您的问题!过长的问题会导致答案独立。

我不会更正您的代码,但您可以使用此简单代码附加rar文件:

var attachmentToSend = new Attachment(pathOfYourFile);
mailMessage.Attachments.Add(attachmentToSend);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接