none
Whats wrong with my code? C#

    Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    
    using OutLook = Microsoft.Office.Interop.Outlook;
    
    namespace Access_Honeywell_V8
    {
      
      public partial class email : Form
      { 
        
        public email(TextBox t)
        {
          InitializeComponent();
          this.mailto.Text = t.Text;
          mailto.Focus();
        }
    
        public email()
        {
          InitializeComponent();
        }
        private string ReadSignature() 
    
        { 
         string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signatures"; 
         string signature = string.Empty; 
         DirectoryInfo diInfo = new DirectoryInfo(appDataDir); 
         if 
          (diInfo.Exists) 
         { 
          FileInfo[] fiSignature = diInfo.GetFiles("*.htm"); 
            
         if (fiSignature.Length > 0) 
         { 
           StreamReader sr = new StreamReader(fiSignature[0].FullName, Encoding.Default); 
           signature = sr.ReadToEnd(); 
           if (!string.IsNullOrEmpty(signature)) 
           { string fileName = fiSignature[0].Name.Replace(fiSignature[0].Extension, string.Empty); 
             signature = signature.Replace(fileName + "_files/", appDataDir + "/" + fileName + "_files/"); 
           }
         } 
    
         } 
          return signature; 
        } 
    
    
        private void button1_Click(object sender, EventArgs e)
        {
          OutLook.Application mailApp = new OutLook.Application();
          OutLook.NameSpace myNam = mailApp.GetNamespace("MAPI");
    
          myNam.Logon(null, null, true, true);
    
    
          OutLook.MAPIFolder ofold = myNam.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderSentMail);
          OutLook._MailItem mi = (OutLook._MailItem)mailApp.CreateItem(OutLook.OlItemType.olMailItem);
          mi.To = mailto.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.SentOnBehalfOfName = "mycompany.com";
          mi.Subject = subject.Text;
          mi.HTMLBody = body.Text;
          mi.Display(true);
          mi.SaveSentMessageFolder = ofold;
          mi.Send();
    
    
        }
    

    Im trying to add a signature to an outlook email.....(ive asked a thousand times in the outlook forum...and no answer) Please just take a look...i can send the mail just fine...and all works great...just no signature!!

    HELP Please this one has been eating me for a week!!

     


    Loving life since 1981 Preston Lambeth
    Wednesday, April 20, 2011 3:02 AM

Answers

  • Hi,

    You should assign signature to the HTMLBody property.

    mi.HTMLBody = body.Text + "<br/><br/>" + ReadSignature();

    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by Ptiwstp1 Wednesday, April 20, 2011 2:30 PM
    Wednesday, April 20, 2011 5:38 AM

All replies

  • Please Debug the ReadSignature() method and check if its returning Signature in expected format.

    Secondly... I dont see a Call to this ReadSignature method any where in the posted code... Check if you missed it.


    .NET Maniac -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Wednesday, April 20, 2011 5:36 AM
  • Hi,

    You should assign signature to the HTMLBody property.

    mi.HTMLBody = body.Text + "<br/><br/>" + ReadSignature();

    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by Ptiwstp1 Wednesday, April 20, 2011 2:30 PM
    Wednesday, April 20, 2011 5:38 AM
  • YOU HAVE NO IDEA HOW MUCH YOU HAVE HELPED ME OUT!!!! Its amazaing how much a second set of eyes can help....wow is all i can say...thank you so much for helping me out with the problem that has been killing me for weeks!!!!! It was hard enough to get the the files with the signature...then emplimenting it was a whole other story......thank you!!!!!!!
    Loving life since 1981 Preston Lambeth
    Wednesday, April 20, 2011 2:32 PM
  • By the way, depending on exactly how you're emailing this, another "gotcha" with sending e-mails can be virus scan software. I see you're using Outlook here, so that shouldn't be a problem, but keep in mind that if you ever change architectures and try to access the e-mail port directly to send outgoing e-mail that most virus scan software will prevent that from functioning unless you give your application "permission" to use that port. (I learned this the hard way; I had an application that would periodically generate and send e-mails - couldn't get it to work, even though the code looked OK - turned out that the problem was with my virus scan software).

     

    I obviously don't have enough information to determine if this will be an issue for your particular application, but it's definitely worth taking the time to make sure you're not going to accidentally run afoul of some kind of security measure (e.g. firewall, OS security measures, Outlook security measures, virus scan/anti-malware software, etc.).

    Wednesday, April 20, 2011 7:48 PM