我创建了一个表
create table files
(
id int identity(1,1),
Name_of_file varchar(50) null,
interviewfile varbinary(max) Null,
)在文件表中插入.doc文件
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发送它
请告诉我解决办法.
谢谢和问候,文卡特·库马尔·奇古拉。
发布于 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
也许能帮到你..。
发布于 2013-08-29 10:25:01
你可能不需要什么修改,但这就行了。
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();
}
}希望这能成功。
https://stackoverflow.com/questions/18508074
复制相似问题