Link to home
Start Free TrialLog in
Avatar of bgernon
bgernonFlag for United States of America

asked on

vb.net automate sending of outlook email

I was successfully using SendKeys.send("%{s}") to send my outlook MailItem.  This week it does not work, nothing is sent.  I'm not even sure if the above was correct to begin with.  Outlook.MailItem.send() creates the message "A program is trying to automatically send e=mail on your behalf.  Do you want to allow this?"  Yes No Help"

Obviously, I can't have the user click yes to every email.

See code.
Private Sub CreateEmail(ByVal emailAddress As String, ByVal Letter As Object)
        Dim ol As New Outlook.Application
        Dim olMail As Outlook.MailItem
        Try

            olMail = ol.CreateItem(Outlook.OlItemType.olMailItem)
            With olMail
                .To = emailAddress
                .Subject = "Report"
                .Attachments.Add(Letter)
                .Body = "This is a Test"
             End With 
             SendKeys.Send("%{s}")
             ol = Nothing
        Catch ex As System.Exception
            Dim msg As String = String.Format("An error occured: {0}", ex.Message)
            MessageBox.Show(msg)
        End Try
    End Sub

Open in new window

Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

instead of using Outlook, use System.net.Mail. Check http://www.emoreau.com/Entries/Articles/2007/09/Using-SystemNetMail.aspx

or, if you can't live without Outlook, check http://www.contextmagic.com/express-clickyes/
Avatar of bgernon

ASKER

SMPT is not an option.  Each email is sent one at a time because the attachment is unique to each receipient.  Our IT division has restricted the ability to download anything off the web.
Outlook has restriction because some malicious code can use it to send spam. The workarounds are :
1. not use outlook
2. use something like Click Yes to auto accept the message
I second emoreau's suggestion: Our shop has had good results with Click Yes. (Just be sure to have your code turn it off when you're done with it.)
Avatar of bgernon

ASKER

1) When you say not to use Outlook, you're suggesting again that I use SMTP?  I'm aware of the restriction.  
2) How do I click yes?  If you are again referring to the free download, I cannot install or download from the web.  

Click Yes would have to be downloaded, so unless you can get the okay from your IT department, that's out. I just peeked at emoreau's link to using SystemNetMail, and I don't see anything there that prevents you from customizing each message individually. Your objection, if I'm reading it correctly, to SMTP is that the emails have to be sent one at a time; that should be possible. Is there something I'm missing?
     "SMPT is not an option.  Each email is sent one at a time because the attachment is unique to each receipient."

Otherwise, it looks to me as if emoreau has your answer....
Avatar of bgernon

ASKER

SMPT requires that the exchange server name: ServerName = "<<Your SMTP server's Name>>".  Again, this is controlled by IT division.  They are very tight on security here.  Also, I was instructed to not involve our IT division.
Well, far be it from me to advocate circumventing the IT department. Security rules are there for a reason. So, Click Yes is out. As for the server name, you may be able to plug in the name of the server that hosts your own Exchange mailbox. Find that this way:
1. Open Outlook
2. Click the Tools menu
3. Select the "E-mail Accounts" option
4. Select "View or change existing e-mail accounts" and click Next
5. With your Microsoft Exchange Server highlighted (usually there's just the one), click the "Change..." button
6. The Microsoft Exchange Server name should be displayed in a text box. Write it down.
7. Cancel your way out of the dialogs, and go about your business.

Hope this helps, and neither gets you in trouble with your IT folks nor results in a security issue for you or your network. ;~)
Avatar of bgernon

ASKER

I forgot to mention, the email will be encrypted with Iron Port.  The exchange server administrator says that an email using smpt cannot use Iron Port.  I just got off the phone with the exchange server administrator and he said they had applied patches to the server this last weekend.  I suspect these patches may explain why my program worked last week and not this week.

 I am going to submit a request to have the YesNo software downloaded and installed on my desktop.  I know emoreau suggested YesNo Pro.  Is this the one I should request?
ClickYes (as opposed to YesNo) Pro eliminates the 5-second delay for Outlook 2003, while the free version waits, and then proceeds. If that's important, go for the pro version. When we started using the tool we were on Outlook XP (2002) so the restriction didn't apply. There are other advantages to the Pro version, too, so if you can get it that's the one to go with, in my opinion. Good luck.
Avatar of bgernon

ASKER

I'm not clear as to which of the two eliminates the 5-second delay, is it YesNo Pro?  It appears that the Pro does not work with SP3.
You don't have to use an Exchange server to use SMTP. If you have a gmail account, it will work as well.
Avatar of bgernon

ASKER

No.  It is very important that the email and attachment are encrypted.  The receiver has to set up and use a password to get their email from the Iron Port server.  Only emails sent through Outlook will work.
Re: YesNo Pro.
Are we sure we're talking about the same application? The link emoreau provided was to ClickYes, not YesNo; ClickYes is the tool with which I am familiar. Here's that URL again:
http://www.contextmagic.com/express-clickyes/

Avatar of bgernon

ASKER

Yes, my bad.  It is ClickYes Pro.  I'll look into ordering it. I'll get back to the two of you and let you know how it worked.

By the way, Outlook Security Manager component from Addin Express disables the Outlook Object Guard (which shows the messagebox). I am using it but it works intermittently though.
Avatar of bgernon

ASKER

Thanks CodeCruiser.  Can't afford intermittent.  We're still looking at ClickYes Pro.  IT  refused our request because it is not approved software.  So we're taking this up another level.  

I thought I got SendKeys.send("%{s}") from Experts Exchange.  Where I got it from and why it stopped working is a mystery to me.  We checked with the adminstrator of the exchange server found out they had installed some patches to the server.  Not sure why these would stop a program that was working.  Well, enough boo-hooing from me.  I'll keep you all posted on whether the software works or not.
SendKeys.Send is done on the client and has nothing to do with Exchange Server and its patches. SendKeys.Send requires accurate timing and focus on the correct control. May be the dialog appears AFTER the sendkeys.send has already executed.
Avatar of bgernon

ASKER

Well, yes it does appear after the sendkeys.send.  With SendKeys.send("%{s}") there was no dialog and the email went out.  When that stopped working, I tried SendKeys.send.  The dialog box pops up.  After clicking yes, the email goes out.  You did give me an idea though.  Get back to you.
Avatar of bgernon

ASKER

My idea bombed.  CodeCruiser, it is not Sendkey.Send that gives me the message, it's olMail.Send().  The code I listed above was the original code that worked until the week before last, without the pop up.  Last week, still no pop up, but no email went out.  olMail.Send() gives me the pop up.  

You mentioned accurate timing and focus on the correct control. Can you please elaborate?
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial