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

查看:1789
本文介绍了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 Settings\Administrator\Desktop\stuff.txt");
}

不过,这似乎并没有工作。
有谁知道的一种方式,这将使这个工作!?

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

帮助很大AP preciate。

Help greatly appreciate.

问候。

推荐答案

至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"" '>

有一个更好的方式来处理这个问题是发送邮件使用的服务器<上一个href=\"http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx\">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天全站免登陆