Monthly Archives: February 2011

Email with attachment from byte array

How to Send Email with Attachment using ASP.NET 2.0 and C#

This article tells how to send an email with an attachment. I made a few modifications to make it work because I had to attach a byte array that came from a database. So this is how you do that:


void SendEmail()
{
List<String> recipientList = GetEmailAddressList();

SmtpClient client = new SmtpClient(GetSMTPServer(), GetSMTPPort());

MailAddress from = new MailAddress(ConfigurationManager.AppSettings["EmailContacts.From"]);

 if (recipientList.Count == 0)
 {
 AddError("no email addresses found.");
 return;
 }

 MailMessage message = new MailMessage();
 message.From = from;
 foreach (string to in recipientList)
 {
 message.To.Add(to);
 }

 //create the message
 message.Body = "A new file is attached for your review.";

 message.BodyEncoding = Encoding.UTF8;

 // Get pdf binary data and save the data to a memory stream
 System.IO.MemoryStream ms = new System.IO.MemoryStream(GetPdf());

 // Create the attachment from a stream. Be sure to name the data with a file and
 // media type that is respective of the data
 message.Attachments.Add(new Attachment(ms, "myFile.pdf", "application/pdf"));

 message.Subject = "New File for Review";

 //send it
 client.Send(message);
}

Tony Sneed’s Blog -> Michael C. Kennedy’s Weblog -> Facebook C# SDK

This:

Using Open Source? Get NuGet. | Tony Sneed’s Blog

has a reference to this:

Michael C. Kennedy’s Weblog – 11 Killer Open Source Projects I Found with NuGet

which has a reference to this:

Facebook C# SDK

which looks extermely cool and useful. More as I dig in.

Thanks Tony!

 

Nicely written post extending the MSDN audio CD writing sample to make it actually write CDs.

Once I have implemented this, I will update with feedback.

C# Audio CD Writer for Windows XP – Sichbo Interactive

 

Using Open Source? Get NuGet. | Tony Sneed’s Blog

 

Iterating Through Databases on Sql Server

This is a way to find all of the databases matching a particular name on a SQL Server and process something in those databases.

USE master
GO

set ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

— iterate through DBs
DECLARE @DbNames TABLE (
rowNum int identity (1,1),
dbname sysname NOT NULL )

INSERT INTO @DbNames
SELECT name
FROM sys.databases
WHERE state=0 AND user_access=0 and has_dbaccess(name) = 1
AND [name] like ‘NameToMatch%’
ORDER BY [name]

DECLARE @EndCount int;
SELECT @EndCount = count(*) FROM @DbNames

DECLARE @RowCounter int;
SELECT @RowCounter = 1;

DECLARE @DbName varchar(20);
DECLARE @sql varchar(2000);

WHILE (@RowCounter <= @EndCount)
BEGIN
SELECT @DbName = dbname FROM @DbNames WHERE @RowCounter = rowNum;
SELECT @sql =
‘USE ‘ + @DbName — do something here…
EXEC (@sql)
–PRINT @sql
SELECT @RowCounter = @RowCounter + 1
END