I have a simple C# aspx script page hosted on IIs that uploads a file along with some email addresses and message etc. and sends an email with uploaded file as attachment. This all works fine from a browser, no problem, but that is not what I am trying to do :).
I want to do this from within a c++ application so I wrote a class to mimic the browser, it pulls down the HTTP get page load. gets the validate ID and state codes and builds up a post message. This works fine with text files but as soon as I try binary it chokes.
It appears to time out waiting for a reply from the server as if it is expecting more data. Does anyone have any ideas? I have been banging my head against this wall for a bit. Thanks!
the aspx website code is:
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO " %>
<%@ Import Namespace="System.Text " %>
<%@ Import Namespace="System.Net.Mail " %>
<%@ Import Namespace="System.Net.Mime " %>
<%@ Import Namespace="System.Net " %>
<html>
<head>
<script language="C#" runat="server">
void Button1_Click(object Source, EventArgs e)
{
if (FromAddr.Value == "")
{
Span1.InnerHtml = "Error: you must enter a Email Addr";
return;
}
if (File1.PostedFile.FileName != "")
{
try
{
MailMessage aMessage = new MailMessage(FromAddr.Value, ToAddr.Value);
aMessage.Subject = Subject.Value;
aMessage.Body = Message.Value;
// set up the attachment
ContentType ct = new ContentType(MediaTypeNames.Application.Octet);
Attachment data = new Attachment(File1.PostedFile.InputStream, ct);
ContentDisposition disposition = data.ContentDisposition;
disposition.FileName = AttachName.Value;
aMessage.Attachments.Add(data);
SmtpClient client = new SmtpClient("<ourhost>");
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(aMessage);
data.Dispose();
Span1.InnerHtml = "Send Success: ";
}
catch (Exception exc)
{
Span1.InnerHtml = "Send failure: " +
ToAddr.Value + "</b><br>" + exc.ToString();
}
}
else
{
try
{
MailMessage aMessage = new MailMessage(FromAddr.Value, ToAddr.Value);
aMessage.Subject = Subject.Value;
aMessage.Body = Message.Value + "\r\nNo Attachment!";
SmtpClient client = new SmtpClient("<ourhost>");
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(aMessage);
Span1.InnerHtml = "Send Success: ";
}
catch (Exception exc)
{
Span1.InnerHtml = "Send failure: " +
ToAddr.Value + "</b><br>" + exc.ToString();
}
}
}
</script>
</head>
<body>
<h3>Sonnet Send Encoded Email</h3>
<form enctype="multipart/form-data" runat="server">
<p>
Select File to Send:
<input id="File1"
type="file"
runat="server">
<p>
Email Address:
<input id="FromAddr"
type="text"
runat="server">
<p>
Attachement Name:
<input id="AttachName"
type="text"
runat="server">
<p>
To Address:
<input id="ToAddr"
type="text"
runat="server">
<p>
Subject:
<input id="Subject"
type="text"
runat="server">
<p>
Message:
<input id="Message"
type="text"
runat="server">
<p>
<span id=Span1
style="font: 8pt verdana;"
runat="server" />
<p>
<input type=button
id="Button1"
value="Upload"
OnServerClick="Button1_Click"
runat="server">
</form>
</body>
</html>