首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过附加文件发送邮件(文件应取自Server数据库)

如何通过附加文件发送邮件(文件应取自Server数据库)
EN

Stack Overflow用户
提问于 2013-08-29 10:10:07
回答 2查看 827关注 0票数 0

我创建了一个表

代码语言:javascript
复制
create table files
(
id int identity(1,1),
Name_of_file varchar(50) null,
interviewfile varbinary(max) Null,
)

在文件表中插入.doc文件

代码语言:javascript
复制
insert into files 
select 'DB Creation' as filetype, *
from openrowset
(BULK 'E:\Office Works\Get SMS\DBA SMS Exam Stuff with 
DocumentationStandards\Interview Questions\Indexes-I.doc', SINGLE_BLOB)
as x

现在,我想发送“索引-I.doc”文件作为附件的特定邮件ID。

我应该如何附加和如何使用Asp.Net 3.5发送它

请告诉我解决办法.

谢谢和问候,文卡特·库马尔·奇古拉。

EN

回答 2

Stack Overflow用户

发布于 2013-08-29 10:17:32

通过下面的链接saxena/send-email-in-Asp-Net-with-attached-file/http://www.codeproject.com/Articles/10828/Sending-Email-with-attachment-in-ASP-NET-using-SMT

也许能帮到你..。

票数 0
EN

Stack Overflow用户

发布于 2013-08-29 10:25:01

你可能不需要什么修改,但这就行了。

代码语言:javascript
复制
private void SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml)
    {
        try
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();

            MailAddress fromAddress = new MailAddress("sample@hotmail.com");

            smtpClient.Host = "xxx.xxx.com";
            smtpClient.Port = 25;

            message.From = fromAddress;
            message.Priority = priority;
            message.To.Add(toAddress);
            message.Subject = subject;
            if (ccAddress.Length > 0)
            {
                message.CC.Add(ccAddress);
            }
            if (bccAddress.Length > 0)
            {
                message.Bcc.Add(bccAddress);
            }

            message.IsBodyHtml = isHtml;
            message.Body = body;

            //load attachment from database
            sConn = ConfigurationManager.ConnectionStrings["eCIConnectionString"].ToString();
            try
            {
                sSQL = @"SELECT id, Name_of_file,  interviewfile" +
                        @"FROM files " +
                        @"WHERE Name_of_file = 'Your FileName'";

                conn.ConnectionString = sConn;
                conn.Open();
                cmd.CommandText = sSQL;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                cmd.Parameters.Clear();

                dAdapter.SelectCommand = cmd;
                DataTable dt = new DataTable();
                dAdapter.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        byte[] barrImg = (byte[])dt.Rows[i]["interviewfile"];
                        string strfn = Convert.ToString(dt.Rows[i]["Name_of_file"]);
                        FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
                        fs.Write(barrImg, 0, barrImg.Length);

                        fs.Flush();
                        fs.Close();
                        fs.Dispose();

                        Attachment att = new Attachment(strfn);
                        message.Attachments.Add(att);
                    }
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message + ex.StackTrace;
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    conn.Dispose();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
            }

            // Send SMTP mail
            smtpClient.Send(message);

            lblMessage.Text = "Email sent to " + toAddress + " successfully !";
        }
        catch (Exception ee)
        {
            lblMessage.Text = ee.ToString();
        }
    }

希望这能成功。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18508074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档