C# MailTo 带附件? [英] C# MailTo with Attachment?

查看:41
本文介绍了C# MailTo 带附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用以下方法打开用户的 Outlook 电子邮件帐户并使用相关内容填充电子邮件以进行发送:

Currently I am using the below method to open the users outlook email account and populate an email with the relevant content for sending:

public void SendSupportEmail(string emailAddress, string subject, string body)
{
   Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" 
                + body);
}

但是,我希望能够使用附件填充电子邮件.

I want to however, be able to populate the email with an attached file.

类似:

public void SendSupportEmail(string emailAddress, string subject, string body)
{
   Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" 
      + body + "&Attach="
      + @"C:Documents and SettingsAdministratorDesktopstuff.txt");
}

但是这似乎不起作用.有没有人知道一种方法可以让这个工作!?

However this does not seem to work. Does anyone know of a way which will allow this to work!?

非常感谢帮助.

问候.

推荐答案

mailto:不正式支持附件.我听说 Outlook 2003 可以使用这种语法:

mailto: doesn't officially support attachments. I've heard Outlook 2003 will work with this syntax:

<a href='mailto:name@domain.com?Subject=SubjTxt&Body=Bod_Txt&Attachment=""C:file.txt"" '>

处理此问题的更好方法是使用 System.Net.Mail.Attachment.

A better way to handle this is to send the mail on the server using System.Net.Mail.Attachment.

    public static void CreateMessageWithAttachment(string server)
    {
        // Specify the file to be attached and sent.
        // This example assumes that a file named Data.xls exists in the
        // current working directory.
        string file = "data.xls";
        // Create a message and set up the recipients.
        MailMessage message = new MailMessage(
           "jane@contoso.com",
           "ben@contoso.com",
           "Quarterly data report.",
           "See the attached spreadsheet.");

        // Create  the file attachment for this e-mail message.
        Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(file);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
        // Add the file attachment to this e-mail message.
        message.Attachments.Add(data);

        //Send the message.
        SmtpClient client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;

        try {
          client.Send(message);
        }
        catch (Exception ex) {
          Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", 
                ex.ToString() );              
        }
        data.Dispose();
    }

这篇关于C# MailTo 带附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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