 |
|
|
Upload and Email an Attachment using aspNetEmail
|
|
|
The following code snippets will demonstrate how to upload a file to be emailed as an attachment, without having to save the uploaded
file to the hard drive.
|
|
|
|
|
|
 |
|
The box is not shipped.
aspNetEmail is a
downloadable product.
|
|
|
|
[C#]
<%@ Page language="c#" %>
<%@ Import Namespace="aspNetEmail" %>
<%@ Import Namespace="System.IO" %>
<script runat=server>
private void Page_Load(object sender, System.EventArgs e)
{
if( Page.IsPostBack )
{
if( file1.PostedFile != null )
{
//declare the email message
EmailMessage msg = new EmailMessage();
msg.Server = "mail.mycompany.com";
msg.To = "me@mycompany.com";
msg.FromAddress = "you@mycompany.com";
msg.Subject = "This email contains an uploaded file";
msg.Body = "see attached file.";
//get the attachment
FileInfo fi = new
FileInfo( file1.PostedFile.FileName );
Attachment a = new
Attachment( file1.PostedFile.InputStream, fi.Name );
a.ContentType = file1.PostedFile.ContentType;
//add the attachment and send it
msg.AddAttachment( a );
msg.Send();
Response.Write( "<BR><font color=red>
The email was sent.</font>" );
}
else
{
Response.Write( "<BR>Please
select a file to upload.");
}
}
}
</script>
<html>
<head>
<title>Upload a file to be sent as an attachment.</title>
</head>
<body >
<form id="temp" method="post" enctype="multipart/form-data" runat="server">
Browse to a file to upload and email.
<br>
<input type=file id=file1 runat=server>
<br>
<input type=submit name=submit1 value="Send Email">
</form>
</body>
</html>
[Visual Basic]
<%@ Page language="vb" %>
<%@ Import Namespace="aspNetEmail" %>
<%@ Import Namespace="System.IO" %>
<script runat=server>
Private Sub Page_Load(sender As Object, e As System.EventArgs)
If Page.IsPostBack Then
If Not (file1.PostedFile Is Nothing) Then
'declare the email message
Dim msg As New EmailMessage()
msg.Server = "mail.mycompany.com"
msg.To = "me@mycompany.com"
msg.FromAddress = "you@mycompany.com"
msg.Subject = "This email contains an uploaded file"
msg.Body = "see attached file."
'get the attachment
Dim fi As New FileInfo(file1.PostedFile.FileName)
Dim a As New Attachment(file1.PostedFile.InputStream, fi.Name)
a.ContentType = file1.PostedFile.ContentType
'add the attachment and send it
msg.AddAttachment(a)
msg.Send()
Response.Write("<BR><font color=red>The email was sent.</font>")
Else
Response.Write("<BR>Please select a file to upload.")
End If
End If
End Sub 'Page_Load
</script>
<html>
<head>
<title>Upload a file to be sent as an attachment.</title>
</head>
<body >
<form id="temp" method="post" enctype="multipart/form-data" runat="server">
Browse to a file to upload and email.
<br>
<input type=file id=file1 runat=server NAME="file1">
<br>
<input type=submit name=submit1 value="Send Email">
</form>
</body>
</html>
|