vb.net automate sending of outlook email

bgernon
bgernon used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
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/

Author

Commented:
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.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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.)

Author

Commented:
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....

Author

Commented:
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. ;~)

Author

Commented:
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.

Author

Commented:
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.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
You don't have to use an Exchange server to use SMTP. If you have a gmail account, it will work as well.

Author

Commented:
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/

Author

Commented:
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.

Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
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.
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
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.

Author

Commented:
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?
Most Valuable Expert 2012
Top Expert 2014
Commented:
I never said SendKeys.Send causes the popup. The popup is poped by Outlook when some other program tries to access protected objects in the Outlook Object Model. SendKeys.Send is supposed to suppress the popup by automatically sending clicks to the buttons on the popup.

Accurate timing:
Depending on the hardware configuration and load on the computer, the popup may take an arbitrary amount of time to appear after the call to the olMail.Send method. You need to execute SendKeys just after the popup appears; not before it appears and not after it has been on screen for sometime.

Focus:
The popup appears with No as the default button. You need to move focus to Yes button by sending Shift+Tab key. You may want to make it more intelligent and tick the box and also select 10 Mins from the drop down because the popup may appear more than once.
Commented:
Ok, I understand.  I did find the fix though.  The email needed to be displayed, then sent.  Apparently, alt + s works the same as clicking on the send button.

 With olMail
                .To = emailAddress
                .Subject = "Report"
                .Attachments.Add(Letter)
                .Body = "This is a Test"
                .Display
             End With  
             SendKeys.Send("%{s}")

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial