开发者

c# Sending email using POST and Mime

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: no-reply@foo.com\n" + "To: foo@foo.com\n" + "Subject: test\n" + "jkjlkjkj\n");
try
{
  request.ContentLength = Bytes.Length;
  os = request.GetRequestStream();
  os.Write(Bytes, 0, Bytes.Length);
}
catch (Exception开发者_C百科 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜