سؤال

I have to mail several files to an e-mail address. The files needs to have .tcx file extension. These are in fact xml files (Garmin bicycle logs).

Apparently when the receiver gets the email, the attachement is named xxxxx.tcx.xml. How can I force the mailer not to change the attachment file name?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.IO;

namespace stravamailer
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var files = Directory.GetFiles("E:\\JurgenS\\Downloads\\allrides");

            for (int i = 0; i < files.Length; i++)
            {
                MailMessage oMail = new MailMessage();
                oMail.Body = "";
                oMail.From = new MailAddress("jurgen@ccccc.be","Jurgen Stillaert");
                oMail.Subject = "";
                oMail.To.Add(new MailAddress("jurgen@cccc.be"));
                Attachment att = new Attachment(files[i]);
                att.Name = Path.GetFileName(files[i]);
                oMail.Attachments.Add(att);

                SmtpClient oSmtp = new SmtpClient("uit.telenet.be");
                oSmtp.Send(oMail);
            }
        }
    }
}
هل كانت مفيدة؟

المحلول

The Attachment class is used with the MailMessage class. All messages include a Body, which contains the content of the message. In addition to the body, you might want to send additional files. These are sent as attachments and are represented as Attachment instances. To add an attachment to a mail message, add it to the MailMessage.Attachments collection.

Attachment content can be a String, Stream, or file name. You can specify the content in an attachment by using any of the Attachment constructors.

The MIME Content-Type header information for the attachment is represented by the ContentType property. The Content-Type header specifies the media type and subtype and any associated parameters. Use ContentType to get the instance associated with an attachment.

The MIME Content-Disposition header is represented by the ContentDisposition property. The Content-Disposition header specifies the presentation and file time stamps for an attachment. A Content-Disposition header is sent only if the attachment is a file. Use the ContentDisposition property to get the instance associated with an attachment.

The MIME Content-Transfer-Encoding header is represented by the TransferEncoding property.

Here is the source

Your solution is in this MSDN link. you should try MIME Content-Type.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top