attach uploaded files to email

Last post 03-16-2011, 1:24 AM by Jeff. 1 replies.
Sort Posts: Previous Next
  •  03-15-2011, 1:22 PM 66670

    attach uploaded files to email

    Hey Guys, based on some sample code that was posted in the forum, if I try to attach the uploaded file(s) to an email message, the attachements appear as "persisted" files in the email. At what point should I attach it to the message because it appears I'm doing it too early. Please advise. Here is the sample source code im working with:
     

    <%@ Page Language="C#" Title="First sample" %>

    <%@ Import Namespace="CuteWebUI" %>

    <%@ Register Assembly="CuteWebUI.AjaxUploader" Namespace="CuteWebUI" TagPrefix="CuteWebUI" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

    <script runat="server">

    protected void Button1_Click(object sender, EventArgs e)

    {

    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

    msg.From = new System.Net.Mail.MailAddress("first_last@acme.com");

    msg.To.Add("first_last@acme.com");

    msg.Subject = "Test";

    msg.Body = "Test";

    foreach (AttachmentItem item in UploadAttachments1.Items)

    {

    if (!item.Checked) continue;

    //string filename = item.FileName;

    //int filesize=item.FileSize;

    //Stream stream = item.OpenStream();

    System.Net.Mail.Attachment ma = new System.Net.Mail.Attachment(item.GetTempFilePath());

    msg.Attachments.Add(ma);

    }

    //System.Net.Mail.SmtpMail.Send(msg);

    System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();

    smtpClient.Send(msg);

    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">

    </head>

    <body>

    <form id="Form1" runat="server">

    <CuteWebUI:UploadAttachments ID="UploadAttachments1" runat="server" InsertText="UploadAttachments1">

    </CuteWebUI:UploadAttachments>

    <br />

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" Width="107px" />

    </form>

    </body>

    </html>

  •  03-16-2011, 1:24 AM 66681 in reply to 66670

    Re: attach uploaded files to email

    Hi drifter,
     
     I tested your sample,
     You add attachments too early,
     The item.GetTempFilePath() returns the template path of item, it is a '.rex' file
     Before  you add attachments, you can use item.MoveTo(NEWPATH) method 
     
    This is my demo
     
    1. protected void Button1_Click(object sender, EventArgs e)  
    2.     {  
    3.         System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();  
    4.         msg.From = new System.Net.Mail.MailAddress("XXX@ccc.net");  
    5.         msg.To.Add("XXXXX@ccc.com");  
    6.         msg.Subject = "Nbk Jeff  please save";  
    7.         msg.Body = " as Such attachments";  
    8.   
    9.         foreach (AttachmentItem item in UploadAttachments1.Items)  
    10.         {  
    11.             if (!item.Checked) continue;  
    12.             //string filename = item.FileName;  
    13.             //int filesize=item.FileSize;  
    14.             //Stream stream = item.OpenStream();  
    15.             string attachmentPath = Server.MapPath("~/") + "UploadFiles\\" + item.FileGuid.ToString() + "_" + item.FileName;  
    16.             item.MoveTo(attachmentPath);  
    17.             System.Net.Mail.Attachment ma = new System.Net.Mail.Attachment(attachmentPath);  
    18.             msg.Attachments.Add(ma);  
    19.         }  
    20.         //System.Net.Mail.SmtpMail.Send(msg);  
    21.         System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("smtp.ccc.com", 25);  
    22.         smtpClient.UseDefaultCredentials = false;  
    23.         smtpClient.Credentials = new System.Net.NetworkCredential("XXX@ccc.com""YOUR PASSWORD");  
    24.         smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;  
    25.         smtpClient.Send(msg);  
    26.     }  
     
    Regards,
    Jeff 
View as RSS news feed in XML