如何将RAR文件附加到c#中的电子邮件? [英] How to attach a RAR file to an email in c#?

查看:171
本文介绍了如何将RAR文件附加到c#中的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲发送具有用下面的代码多个附件的电子邮件,但它不为RAR文件工作。问题是什么?
。对于每个附件我有一个包含附件文件的一些属性,其内容的类:

 公共类AttachmentFile 
{
[StringLength(200)]
公共字符串文件名{获得;组; }
[StringLength(15)]
公共字符串扩展{搞定;组; }
[StringLength(100)]
公共特征码{搞定;组; }
公众的byte []内容{搞定;组; }
[StringLength(500)]
公共字符串FULLPATH {搞定;组; }
}



Send方法如下所示:

 公共无效发送(字符串从,列表<串> recivers,串smtpHostName,弦乐主题,绳体,ICollection的< AttachmentFile> attachmentFiles)
{
MAILMESSAGE变种新= MAILMESSAGE();
的foreach(在recivers VAR reciver)
{
mailMessage.To.Add(reciver);
}

mailMessage.Subject =主体;
mailMessage.From =新的MailAddress(@from);
mailMessage.Body = RenderBody(体);
如果(attachmentFiles!= NULL)
{
的foreach(在attachmentFiles VAR attachmentFile)
{
变种的contentType =新System.Net.Mime.ContentType(系统。 Net.Mime.MediaTypeNames.Application.Octet);
mailMessage.Attachments.Add(新附件(新的MemoryStream(attachmentFile.Content),应用程序/ RAR));
}
}

mailMessage.IsBodyHtml = TRUE;
mailMessage.SubjectEncoding = Encoding.UTF8;
变种SMTP =新SmtpClient {主机= smtpHostName};
smtp.Send(MAILMESSAGE);
}


解决方案

请写你的问题,因为短越好!龙的问题引起的独立的答案。
我不纠正你的代码,但你可以使用这个简单的代码附加一个RAR文件:

  VAR attachmentToSend =新的附件(pathOfYourFile); 
mailMessage.Attachments.Add(attachmentToSend);


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

解决方案

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

这篇关于如何将RAR文件附加到c#中的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆