Hi,

I need to send an email which has an attachment; the attachment is stored in sql server-2005, in a table as an image data type.

Thanks

Recommended Answers

All 3 Replies

You can pull the contents of the database down in to a temporary file or create a stream. The constructor of Attachment() takes either a file name OR a stream with a file name to attach to a mail message.

using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
.......
    private void button2_Click(object sender, EventArgs e)
    {
      try
      {
        using (MailMessage msg = new MailMessage())
        {
          msg.From = new MailAddress("sender@domain.com");
          msg.To.Add(new MailAddress("recipient@domain.com"));
          msg.Subject = "Test E-Mail";
          msg.Body = "Test";
          string fNameTmp = System.IO.Path.GetTempFileName();
          string fName = Path.ChangeExtension(fNameTmp, @".txt");
          File.Move(fNameTmp, fName);

          File.WriteAllText(fName, "Test Attachment");
          using (Attachment at = new Attachment(fName))
          {
            msg.Attachments.Add(at);
            SendMail(msg);
          }
          
          try
          {
            File.Delete(fName);
          }
          catch (Exception ex)
          {
            if (Environment.UserInteractive) 
              MessageBox.Show("Error cleaning up temp file: " + Environment.NewLine + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
        }
        if (Environment.UserInteractive) 
          MessageBox.Show("Mail sent.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
      }
      catch (Exception ex)
      {
        string msg = ex.Message + Environment.NewLine;
        if (ex.InnerException != null)
          msg += Environment.NewLine + Environment.NewLine + Environment.NewLine + ex.InnerException.Message;
        if (Environment.UserInteractive) 
          MessageBox.Show("Error: " + Environment.NewLine + msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

    public static void SendMail(MailMessage Message)
    {
      bool useAuth = false; //You need to pass this as a parameter
      string userName = string.Empty; //You need to define this
      string password = string.Empty; //You need to define this
      string authDomain = string.Empty; //You need to define this
      SmtpClient cli = new SmtpClient("smtp.server.com");
      if (useAuth)
      {
        cli.UseDefaultCredentials = false;
        NetworkCredential cred = null;

        if (string.IsNullOrEmpty(authDomain))
          cred = new NetworkCredential(userName, password);
        else
          cred = new NetworkCredential(userName, password, authDomain);

        cli.Credentials = cred;
      }
      cli.Send(Message);
    }

Hi Scott,
there is a problem here, the data coming from database was stored as .txt file(opening as notepad)
but I want to open the file or send the file as .doc file.

please assist me.

Is this a file extension problem or do you need to convert a .txt file to a .doc file? Either way that is a matter for another thread... I have showed you how to send an attachment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.