Question

I want to send an email that has multiple attachments using the following code but it doesn't work for RAR files. What is the problem? For every attachment I have a class that contains some properties of the attached file and its content:

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; } 
}

The Send method is shown here:

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);
}
Was it helpful?

Solution

Please write your problem as short as possible! Long questions cause independent answers. I don't correct your code but you can attach a rar file using this simple code:

var attachmentToSend = new Attachment(pathOfYourFile);
mailMessage.Attachments.Add(attachmentToSend);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top