c# 使用 POST 和 Mime 发送电子邮件

发布于 2024-10-06 08:18:43 字数 838 浏览 2 评论 0原文

我正在编写一个程序,我的主管希望我利用他们的内部沙箱电子邮件系统。

本质上,我到目前为止的代码是:

WebRequest request = WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

Stream os = null;
byte[] Bytes = Encoding.ASCII.GetBytes("From: [email protected]\n" + "To: [email protected]\n" + "Subject: test\n" + "jkjlkjkj\n");
try
{
  request.ContentLength = Bytes.Length;
  os = request.GetRequestStream();
  os.Write(Bytes, 0, Bytes.Length);
}
catch (Exception e)
{
  Console.WriteLine("error");
}

这工作正常并按预期发送电子邮件。但是如何使用这种方法发送附件呢?它们可能会打开小型小型转储文件。

谢谢。

I am writing a program whereby my supervisor wants me to make use of their internal sandbox email system.

Essentially, the code I have so far is:

WebRequest request = WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

Stream os = null;
byte[] Bytes = Encoding.ASCII.GetBytes("From: [email protected]\n" + "To: [email protected]\n" + "Subject: test\n" + "jkjlkjkj\n");
try
{
  request.ContentLength = Bytes.Length;
  os = request.GetRequestStream();
  os.Write(Bytes, 0, Bytes.Length);
}
catch (Exception e)
{
  Console.WriteLine("error");
}

This works fine and send the e-mail as expected. But how can I send attachments using this method? They are likely to open be small minidump files.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-10-13 08:18:43

如果您使用标准电子邮件协议,只需查找有关如何在电子邮件中发送附件的规范。我刚刚在 PHP 中找到了这个示例,它创建了一条带有附件的消息,也许它在 .NET 中的工作方式相同(更改调用):

尝试在 Bytes 数组上添加这些行:

Content-Type: application/zip;名称=“附件.zip”
内容传输编码:base64
内容处置:附件
AttachmentContents

其中attachmentContents 是文件的base64 编码转储(显然,根据您发送的内容更改名称和mime 类型。

我从这里获取此代码 http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment

希望有帮助

希望有帮助。

If you're using a standard email protocol just look for the specifications on how an attachment is sent in an email. I just found this example in PHP that creates a message with an attachment, maybe it will work the same way in .NET (changing the calls):

Try adding those lines on the Bytes array:

Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
attachmentContents

where attachmentContents is a base64 encoded dump of the file (obviously, change the names and mime types according to what you're sending.

I took this code from here http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment

Hope it helps

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文