Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2009, 02:12 PM
Friend of Wrox
 
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
Arrow attaching file with email

Hello everyone,
As now I'm done with sending messages to email accounts with both user controls and login controls . Now one thing that remains now is to send attachments with emails.
I have gone through one of the posts pointing to same problem and in response he wad suggested to visit http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=412 . I have gone through the same link there is the following code snippet
Code:
if (FileUpload1.HasFile)
{
  string toAddress = "[email protected]";
  string fromAddress = [email protected] (2);
  string mailServer = "smtp.yourprovider.com";

  MailMessage myMailMessage = new MailMessage();

  myMailMessage.To.Add(toAddress);
  myMailMessage.From = new MailAddress(fromAddress);
  myMailMessage.Subject = "Test Message";

  string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
  Attachment myAttachment = 
                 new Attachment(FileUpload1.FileContent, fileName);
  myMailMessage.Attachments.Add(myAttachment);

  SmtpClient mySmtpClient = new SmtpClient(mailServer);

  mySmtpClient.Send(myMailMessage);
}
I have my code accordingly as there are already "myMessage"
My code seems like this
Code:
 protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
            string mailBody = System.IO.File.ReadAllText(fileName);
            mailBody = mailBody.Replace("##Name##", txtName.Text);
            mailBody = mailBody.Replace("##Email##", txtEmailAddress.Text);
            mailBody = mailBody.Replace("##HomePhone##", txtPhoneHome.Text);
            mailBody = mailBody.Replace("##BusinessPhone##", txtPhoneBusiness.Text);
            mailBody = mailBody.Replace("##Comments##", txtComments.Text);
            
            MailMessage myMessage = new MailMessage();
            myMessage.Subject = "Response From Web Site";
            myMessage.Body = mailBody;

            myMessage.From = new MailAddress("[email protected]", "name");
            myMessage.To.Add(new MailAddress("[email protected]", "name"));

            if (FileUpload1.HasFile)
            {
                string fileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
                Attachment myAttachment = new Attachment(FileUpload1.FileContent, fileName1);
                myMessage.Attachments.Add(myAttachment);
            }
            SmtpClient mySmtpClient = new SmtpClient();
            try
            {
                mySmtpClient.EnableSsl = true;
                mySmtpClient.Send(myMessage);
            }
            catch (Exception ex)
            {
                lblMessage.Text = "An error occurred while sending your e-mail. Please try again.";
            }
            lblMessage.Visible = true;
            FormTable.Visible = false;
After adding simple this FileUpload control in ContactForm.aspx page in other row, on sending feedback with simple message without attaching any file is reported as success but no message is received at all. And if I attach file then it is reported as failed
Kindly take a look at this.........





Similar Threads
Thread Thread Starter Forum Replies Last Post
Attaching File with Mail using PHP sankar2chat Beginning PHP 1 September 24th, 2008 05:40 AM
attaching image file Matthew Dreamweaver (all versions) 3 May 25th, 2007 03:04 AM
Attaching a file using FormMail.cgi djkessler Javascript How-To 2 February 14th, 2007 09:39 AM
Attaching a report to an email marcin2k Access VBA 2 February 8th, 2005 09:54 PM
MIME Attaching PDF file jer99 Pro PHP 1 August 18th, 2003 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.