如何在c#中添加附件到mailto?

时间:2015-12-24 06:56:21

标签: c# unity3d

string email ="sample@gmail.com";
attachment = path + "/" + filename;
Application.OpenURL ("mailto:" + 
                      email+"
                      ?subject=EmailSubject&body=EmailBody"+"&attachment="+attachment);

在上面的代码中,attachment无效。是否还有其他替代方法可以使用C#中的mailto:link添加附件?

3 个答案:

答案 0 :(得分:2)

您可以使用具有System.Net.Mail属性的MailMessage.Attachments。类似的东西:

message.Attachments.Add(new Attachment(yourAttachmentPath));

您可以尝试这样:

using SendFileTo;

namespace TestSendTo
{
    public partial class Form1 : Form
    {
        private void btnSend_Click(object sender, EventArgs e)
        {
            MAPI mapi = new MAPI();

            mapi.AddAttachment("c:\\temp\\file1.txt");
            mapi.AddAttachment("c:\\temp\\file2.txt");
            mapi.AddRecipientTo("person1@somewhere.com");
            mapi.AddRecipientTo("person2@somewhere.com");
            mapi.SendMailPopup("testing", "body text");

            // Or if you want try and do a direct send without displaying the 
            // mail dialog mapi.SendMailDirect("testing", "body text");
        }
    }
}

上面的代码使用MAPI32.dll。

Source

  

它可能不会附加文件,因为你是自由的   电子邮件客户端实现mailto协议和包括   解析附件子句。您可能不知道什么是邮件客户端   安装在PC上,因此它可能并不总是有效 - Outlook当然   不使用mailto支持附件

答案 1 :(得分:2)

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

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

您的问题已经得到解答: c-sharp-mailto-with-attachment

答案 2 :(得分:-1)

处理此问题的更好方法是使用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();
}