如何在 c# 中向 mailto 添加附件
本文关键字:添加 mailto 中向 | 更新日期: 2023-09-27 18:27:53
string email ="sample@gmail.com";
attachment = path + "/" + filename;
Application.OpenURL ("mailto:" +
email+"
?subject=EmailSubject&body=EmailBody"+"&attachment="+attachment);
在上面的代码中,attachment
不起作用。在 C# 中使用 mailto: 链接添加附件还有其他替代方法吗?
mailto:不正式支持附件。我听说Outlook 2003将使用此语法:
<a href='mailto:name@domain.com?Subject=SubjTxt&Body=Bod_Txt&Attachment=""C:'file.txt"" '>
您的问题已经得到解答:C-尖锐邮件到带附件
您可以使用具有 MailMessage.Attachments 属性的 System.Net.Mail。像这样:
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。
源
它可能不会附加文档,因为您可以自由 电子邮件客户端,以实现 mailto 协议并包括 分析附件子句。你可能不知道是什么邮件客户端 安装在PC上,因此它可能并不总是有效 - 当然是Outlook 不支持使用 mailto 的附件。
处理此问题的更好方法是使用 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();
}