attach file from sql database to mail as attatchment.


hello,

i have question.

i'm building website can upload file ( word, excel..) to sql database. working fine. have able send documents sql database mail address attachment.

below send page code not working me i'm doing wrong.

public void button1_click(object sender, eventargs e)
    {
        int id = int.parse(request.querystring["id"].tostring());
        byte[] bytes;
        string filename, contenttype;
        string mailfrom = convert.tostring(xxxxxxxxxxxxxxxxx@gmail.com);   //your own correct gmail address
        string password = convert.tostring("xxxxxxxxxxxxxxxxx");
        string mailto = convert.tostring(txtemail.text);    //email address whom want send mail
        string constr = configurationmanager.connectionstrings["msdb"].connectionstring;
        using (sqlconnection con = new sqlconnection(constr))
        {
            sqlcommand cmdselect = new sqlcommand("select name, data, contenttype routersfiles id=@id", con);
            cmdselect.parameters.addwithvalue("@id", id);

            datatable dt = new datatable();
            con.open();
            sqldataadapter sda = new sqldataadapter();
            sda.selectcommand = cmdselect;
            sda.fill(dt);
            using (sqldatareader sdr = cmdselect.executereader())
            {
                        sdr.read();
                        bytes = (byte[])sdr["data"];
                        contenttype = sdr["contenttype"].tostring();
                        filename = sdr["name"].tostring();
                        string strfn = convert.tostring(convert.tostring("id"));
                        filestream fs = new filestream(strfn, filemode.createnew, fileaccess.write);
                        fs.flush();
                        fs.close();

                        attachment attachment;
                        attachment = new attachment(strfn);
                        system.net.mail.mailmessage email = new system.net.mail.mailmessage();
                        email.to.add(new system.net.mail.mailaddress(mailto));
                        email.from = new system.net.mail.mailaddress(mailfrom, "manuals", system.text.encoding.utf8);
                        email.attachments.add(attachment);

                        email.subject = txtsubject.text;
                        email.subjectencoding = system.text.encoding.utf8;

                        email.body = txtcomments.text;
                        email.bodyencoding = system.text.encoding.utf8;
                        email.priority = system.net.mail.mailpriority.high;

                        system.net.mail.smtpclient smtp = new system.net.mail.smtpclient();
                        smtp.credentials = new system.net.networkcredential(mailfrom, password);//add creddentials- use own email id , password
                        smtp.port = 587;   // gmail works on port
                        smtp.host = "smtp.gmail.com";
                        smtp.enablessl = true;     //gmail works on server secured layer
                        try
                        {
                            smtp.send(email);
                            lblmessage.text = "email has been sent successfully.";
                            lblmessage.visible = true;
                        }
                        catch (exception ex)
                        {
                            throw ex;
                        }

                        finally
                        {
                            if (con != null)
                            {
                                if (con.state == connectionstate.open)
                                {
                                    con.close();
                                }
                                con.dispose();
                            }
                            if (cmdselect != null)
                            {
                                cmdselect.dispose();
                            }
                        }

                    }

 

                }

            }

i looked @ original psoting , don't see variable bytes = (byte[])sdr["data"]; being sent.  bytes arre binary i'm not sure why arre trying convert string cause errors.  if need string use class uuencode/uudecode converty binary byte[] arrary ascii , reverse process.  byte[] string corrupt the data , uuencode not.

jdweng



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'