C# MailTo 带附件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1195111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# MailTo with Attachment?
提问by Goober
Currently I am using the below method to open the users outlook email account and populate an email with the relevant content for sending:
目前我正在使用以下方法打开用户的 Outlook 电子邮件帐户并使用相关内容填充电子邮件以进行发送:
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.
但是,我希望能够使用附加文件填充电子邮件。
something like:
就像是:
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!?
然而,这似乎不起作用。有谁知道一种方法可以让这个工作!?
Help greatly appreciate.
非常感谢帮助。
Regards.
问候。
采纳答案by Jon Galloway
mailto: doesn't officially support attachments. I've heard Outlook 2003 will work with this syntax:
mailto:不正式支持附件。我听说 Outlook 2003 可以使用以下语法:
<a href='mailto:[email protected]?Subject=SubjTxt&Body=Bod_Txt&Attachment=""C:\file.txt"" '>
A better way to handle this is to send the mail on the server using System.Net.Mail.Attachment.
处理此问题的更好方法是使用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(
"[email protected]",
"[email protected]",
"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();
}
回答by David
Does this app really need to use Outlook? Is there a reason for not using the System.Net.Mail namespace?
这个应用真的需要使用 Outlook 吗?是否有理由不使用 System.Net.Mail 命名空间?
If you really do need to use Outlook ( and I would not recommend it because then you're basing your app on 3rd party dependencies that are likely to change) you will need to look into the Microsoft.Office namespaces
如果您确实需要使用 Outlook(我不推荐它,因为您的应用程序基于可能会更改的 3rd 方依赖项),您将需要查看 Microsoft.Office 命名空间
I'd start here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx
我会从这里开始:http: //msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx
回答by Alex
If you want to access the default email client then you can use MAPI32.dll (works on Windows OS only). Take a look at the following wrapper:
如果要访问默认电子邮件客户端,则可以使用 MAPI32.dll(仅适用于 Windows 操作系统)。看看下面的包装器:
http://www.codeproject.com/KB/IP/SendFileToNET.aspx
http://www.codeproject.com/KB/IP/SendFileToNET.aspx
Code looks like this:
代码如下所示:
MAPI mapi = new MAPI();
mapi.AddAttachment("c:\temp\file1.txt");
mapi.AddAttachment("c:\temp\file2.txt");
mapi.AddRecipientTo("[email protected]");
mapi.AddRecipientTo("[email protected]");
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");
回答by kernowcode
Try this
尝试这个
var proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = string.Format("\"{0}\"", Process.GetProcessesByName("OUTLOOK")[0].Modules[0].FileName);
proc.StartInfo.Arguments = string.Format(" /c ipm.note /m {0} /a \"{1}\"", "[email protected]", @"c:\attachments\file.txt");
proc.Start();
回答by Mahesh
Officially yes the mailTo protocol doesn't support the Attachments. But Williwyg explained it very well here that there is a way to do that - Open default mail client along with a attachment
官方是的,mailTo 协议不支持附件。但是 Williwyg 在这里很好地解释了有一种方法可以做到这一点 - 打开默认邮件客户端和附件