Solved

asp Email attachment

Posted on 2010-08-30
11
268 Views
Last Modified: 2012-05-10
why is it when i insert into the database 1st than email an "attachment" , it does not open in my email ?


but if i just email an attachment using c# code it works fine?

here's my code
protected void btnplaceOrder_Click(object sender, EventArgs e)

    {

        //FileUpload img = (FileUpload)FileUpload1;

        byte[] imgByte = null;



        if (FileUpload1.HasFile == true)

        {

            HttpPostedFile File = FileUpload1.PostedFile;

            imgByte = new Byte[File.ContentLength];

            File.InputStream.Read(imgByte, 0, File.ContentLength);

        }



        string s = C.Insert(lblrefno.Text, txtFname.Text, txtLname.Text, txtContactNo.Text, txtCollage.Text, txtRoomNo.Text, txtStreet.Text, txtCity.Text, txtState.Text, txtZipcode.Text, txtCountry.Text, txtCollage.Text, imgByte, txtAdditionNotes.Text, lblNoBP.Text, lblNoC.Text, lblNoC.Text, lblTT.Text, lblNOLB.Text, lblNOLBCC.Text, lblNN.Text, lblNNCC.Text, lblEC.Text, lblDC.Text, lblTTC.Text);



        if (s == "")

        {

            string body = returnEmail();

            SendMail(body, "ritesh@jcweb.co.za", "Order Confirmation Details", "yes");

            SendMail(body, "ritesh@jcweb.co.za", "Order Confirmation Details", "no");

        }

}









    public void SendMail(string body, string to, string Subject, string attachment)

    {



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



        MailMass.From = new System.Net.Mail.MailAddress("info@theses.jcwebhostza.com");

        MailMass.To.Add(new System.Net.Mail.MailAddress(to));

        MailMass.Subject = Subject;

        MailMass.Body = body;

        MailMass.IsBodyHtml = true;



        if (attachment == "yes")

        {

            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

            Attachment myAttachment =new Attachment(FileUpload1.FileContent, fileName);

            MailMass.Attachments.Add(myAttachment);



        }



        MailMass.ReplyTo = new System.Net.Mail.MailAddress("info@theses.jcwebhostza.com");

        MailMass.Priority = System.Net.Mail.MailPriority.High;

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

        System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("info@theses.jcwebhostza.com", "inf01the");



        smtpC.Host = "mail.theses.jcwebhostza.com";

        smtpC.Port = 25;

        smtpC.UseDefaultCredentials = false;

        smtpC.Credentials = basicAuthenticationInfo;



        smtpC.Send(MailMass);

    }

Open in new window

0
Comment
Question by:JCWEBHOST
11 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33557393
it seems that C.Insert() returns something other than "".cause in the next line you check if (s == "")i'd suggest first of all debug the code and check what C.Insert() returns, and also dig in into the implementation of Insert() to see what it returns and why.it's hard to tell cause you didn't post the implementation of C.Insert() function.but debugging will sure reveal you the reason for that.
0
 

Author Comment

by:JCWEBHOST
ID: 33557479
the email comes throu bt the attachment not displaying if its a pic
0
 

Author Comment

by:JCWEBHOST
ID: 33557510
if it take out this it works fine, but nothing is been entered into the data base
0
 

Author Comment

by:JCWEBHOST
ID: 33557515
 //HttpPostedFile File = FileUpload1.PostedFile;
            //imgByte = new Byte[File.ContentLength];
            //File.InputStream.Read(imgByte, 0, File.ContentLength);
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33557516
and if you comment the C.Insert() line the attachment comes in just fine???
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:JCWEBHOST
ID: 33557535
Yes
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33557558
what's the implementation of C.Insert() ??
0
 

Author Comment

by:JCWEBHOST
ID: 33557605
inserting into the database
0
 
LVL 4

Expert Comment

by:kjetilroe
ID: 33557757

It would be helpful if you also included the code for the C.Insert() procedure. Is the code for the C.Insert() procedure returning an empty string, or does it actually returns something else. If C.Insert() returns anything other than an empty string, you will not send any emails based on the code you wrote here.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33557820
can u assure that the C.Insert always return result equal to ""?
which means the SendEmail always called?
0
 
LVL 9

Accepted Solution

by:
puru1981 earned 500 total points
ID: 33558271
please dispose the file object. it might help you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now