I have the following code:
Set myMailanon = CreateObject("CDO.Message")
myMailanon.MimeFormatted = True
Set myConfanon = Server.CreateObject("CDO.Configuration")
Set objBPanon = myMailanon.AddRelatedBodyPart("http://www.foo.bar/img/logo1.jpg", "http://www.foo.bar/img/logo1.jpg", CdoReferenceTypeName)
objBPanon.Fields.Item("urn:schemas:mailheader:Content-ID") = "<http://www.foo.bar/img/logo1.jpg>"
objBPanon.Fields.Update
ConfURLanon = "http://schemas.microsoft.com/cdo/configuration/"
with myConfanon
.Fields.Item(ConfURLanon & "sendusing") = 2
.Fields.Item(ConfURLanon & "smtpserver") = "smtp.foo.bar"
.Fields.Item(ConfURLanon & "smtpserverport") = 25
.Fields.Item(ConfURLanon & "smtpusessl") = false
.Fields.Item(ConfURLanon & "smtpauthenticate") = 1
.Fields.Item(ConfURLanon & "sendusername") = "foo#bar.com"
.Fields.Item(ConfURLanon & "sendpassword") = "foobarpass"
.Fields.Update
end with
with myMailanon
.Subject='Foo!! Bar!!'
.From='Foo!! Bar!! <foo#bar.com>'
.To='foo#bar.com,bar#foo.com'
txt="This is foo... bar... text... SPARTAAAAAAAAA"
.HTMLBody = txt
Set .Configuration = myConfanon
On Error Resume Next
.Send
end with
Problem is that this code, run 100 times, will work 30 and fail 70. Randomly.
And by "fail" I mean that it will cause Script execution timeout.
After commenting line by line, I got to the conclusion that the error comes from ".Send".
But why? Any ideas?
Regards
There isn't anything wrong with code (athough CdoReferenceTypeName looks a little suspect but that could be just poor variable naming). It could just be that the SMTP server is busy and is therefore taking a while to process the send.
You could check the Script Time-out value specified for the ASP feature in IIS manager. By default it should be 90 seconds but perhaps its been set to a low value for some reason.
You can increase the amount of time you script has by specifying it in your code.
Server.ScriptTimeout = 300
Which would give your script a 5 minutes. This may well be masking some set up problem between your web server and SMTP server but it may increase the success rate. OTH you may get the same results but the 70% failures take longer to occur.
Whatever the cause I don't think its your code.
Related
I have an HTA and using VBScript to loop through Outlook email folders and get folder size. The HTA is run on a shared drive by staff, it is not an administrator tool. On occasion, my company will send encrypted emails. When the VBS hits one of these emails, the following happens:
1) VBS pauses.
2) Outlook displays the 'Entrust Entelligence Security Provider' error and asks the user to click 'OK'.
3) Once OK is clicked, the VBS continues.
The Outlook message does not bring focus to Outlook, so it is possible the user will not notice the message and continue to wait for the VBS to finish.
Is there any way of avoiding this message?
Here is my code:
public TotalSize
Sub OutlookDetail
TotalSize = 0
msgbox "Depending on the size of your Outlook account, this process may take up to 60 seconds" & vbcrlf & vbcrlf & _
"If you have encrypted emails, Outlook will make a sound and give you the 'Entrust Entelligence Security Provider' message. Click 'OK' to proceed."
Const olFolderInbox = 6
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set colItems = objFolder.Items
For Each objItem in colItems
'THE OUTLOOK ERROR MAY OCCUR HERE
TotalSize = TotalSize + objItem.Size
Next
Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)
GetSubfolders(objInbox)
msgbox "The size of Inbox and all Subfolders: " & Round((TotalSize / 1048576),2) & " MB"
End Sub
Sub GetSubfolders(objParentFolder)
Set colFolders = objParentFolder.Folders
For Each objFolder in colFolders
Set objSubfolder = objParentFolder.Folders(objFolder.Name)
intSize = 0
Set colItems = objSubfolder.Items
For Each objItem in colItems
'THE OUTLOOK ERROR MAY ALSO OCCUR HERE
intSize = intSize + objItem.size
next
TotalSize = TotalSize + intSize
GetSubfolders objSubfolder
Next
End Sub
The following is a VBScript (VBS) that I use the check for and process certain Outlook emails and attachments. The script finds the emails via their email address and subject. It then saves the attachment in a folder and moves the email to a folder within Outlook. (Most of this code was adapted from a stackoverflow.com post, but I have since forgotten which one.)
My issue: Sometimes this script has to be run before the user has opened Outlook for the day; therefore, none of the Outlook folders have been updated and the script can't find emails that have been sent to the user since the user shut Outlook down last.
My question: How do I update the Outlook inbox then proceed with the rest of the script ensuring that the Inbox is (or all Outlook folders are) completely updated? I'm not sure if VBS will wait for the folder update to happen, but if it won't, I, of course, need it to. I don't know how to update the inbox or wait for it to update if waiting is applicable.
Other tips on how to make the script better are welcome.
My VBScript:
Const olFolderInbox = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Call SaveAndMoveAttachment("'subject 1'", objFolder)
Call SaveAndMoveAttachment("'subject 2'", objFolder)
Call SaveAndMoveAttachment("'subject 3'", objFolder)
Set objFSO = Nothing
Set objOutlook = Nothing
Set objNamespace = Nothing
WScript.Quit
Sub SaveAndMoveAttachment(sSubject, objFolder)
Set colItems = objFolder.Items
Set colFilteredItems = colItems.Restrict("[Subject] = " & sSubject)
If colFilteredItems.count = 0 then
Msgbox "An email with subject " & sSubject & " in it was not found in your Outlook Inbox"
WScript.Quit
end if
For Each objMessage In colFilteredItems
Set colAttachments = objMessage.Attachments
intCount = colAttachments.Count
If intCount <> 0 and objMessage.Sender.Address = "support#somesite.com" Then
For i = 1 To intCount
strFileName = "Z:\somepath\" & objMessage.Attachments.Item(i).FileName
objMessage.Attachments.Item(i).SaveAsFile strFileName
'move the message to somefolder folder
Set objFoldersomefolder = objNamespace.GetDefaultFolder(olFolderInbox).Folders("somefolder")
objMessage.Move objFoldersomefolder
Next
End If
Next
Set colFilteredItems = Nothing
Set colAttachments = Nothing
Set colItems = Nothing
End Sub
Add logon step between above 2 lines
WSCript.Sleep 2000
objNamespace.Logon
objNamespace.SendAndReceive(True)
Below this line:
Set objNamespace = objOutlook.GetNamespace("MAPI")
Add this:
WSCript.Sleep 2000
objNamespace.SendAndReceive(True)
I found it here:
http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_28215854.html
I have tried the following code, No errors thrown. and no emails have been sent either.
Dim i, objEmail
' Use custom error handling
On Error Resume Next
Set objEmail = CreateObject( "CDO.Message" )
' Fill in the field values
With objEmail
.From = "xyz#abc.com"
.To = "pqr#abc.com"
.Subject = "Test"
.TextBody = "Email from VB Script"
If mySMTPPort = "" Then
mySMTPPort = 25
End If
With .Configuration.Fields
.Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ) = 2
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ) = "HostNameHere"
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = 25
.Update
End With
.Send
End With
' Return status message
If Err Then
EMail = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
Else
EMail = "Message sent ok"
End If
' Release the e-mail message object
Set objEmail = Nothing
When I execute this VBS file, It just does nothing at all. Please help me out. I have to send simple text emails from my domain to another domain. Or is there a work around for the task i want to perform?
You're probably getting an error when trying to send mail. However, you have error handling enabled (On Error Resume Next), but your error handler just puts the error information in a variable without actually doing anything with that variable. Adding a line that echoes the variable should provide you with more information about what's going on:
If Err Then
EMail = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
Else
EMail = "Message sent ok"
End If
WScript.Echo EMail
should provide you with more information about what's going on.
Edit: Apparently you're getting a connection error. There are several possible causes for that. First check that your name resolution works correctly:
nslookup HostNameHere
If the name doesn't resolve either use the IP address in your script or get the name resolution fixed.
Next check if you can connect to port 25 on the remote host, both via name and IP address:
telnet HostNameHere 25
telnet a.b.c.d 25
If you get a result like this:
C:\>telnet HostNameHere 25
Connecting To HostNameHere...Could not open connection to the host, on port 25:
Connect failed
something is blocking your connection. Could be a network firewall, a host-based firewall on the remote host, or the service not listening on port 25 in the first place. Check with your network admins and/or the admin(s) of the mail server.
Your code hides errors. err default property is number. So You say if err.number = 0 (same as false) then error, if err.number not 0 (ie error) everything fine.
since last week, I am no longer able to send email via gmail's smtp server. I get the error " -2147220973 the transport failed to connect to the server" whenever I try to send email. However, if I try using my another network (ex. my mobile broadband) it works.
Below is my code for sending email.
Set cdomsg = CreateObject("CDO.Message")
Set cdoconf = CreateObject("CDO.Configuration")
cdoconf.Load -1 ' CDO Source Defaults
Set cdoFields = cdoconf.Fields
With cdoFields
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "example#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "abc"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Update
End With
What is the possible problem? Thank you.
I'm using ASP code and AspEmail component to send emails to our clinets, but I have some problmes...
I have more then 1000 email address that I need to send them an email, becuase of my SMTP provider limitation, I can't add them all as BCC in one email but I need to send each email seperatly, therefor looping on +1000 times witch takes forever and fires the server timeout error.
I need to send those emails about 20 times a day.
This is my script:
on error resume next
msg = "SOME TEXT HERE"
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "SMPT.HOST.ADDRESS"
Mail.Port = 25
Mail.CharSet = "UTF-8"
Mail.ContentTransferEncoding = "Quoted-Printable"
Mail.From = "noreply#mydomain.co.il"
MailSubject = "email subject"
Mail.Subject = Mail.EncodeHeader(MailSubject, "utf-8")
Mail.Body = msg
Mail.IsHTML = True
zBcc = split(zBcc, ";") '1000 emails here
for i=0 to Ubound(zBcc)
zBcc(i) = trim(zBcc(i))
if len(zBcc(i))>0 then
if inStr(zBcc(i), " ")>0 then
else
if (Mail.ValidateAddress(zBcc(i)) = 0) then
Mail.Reset()
Mail.AddAddress zBcc(i)
Mail.Send
end if
end if
end if
next
set Mail=nothing
why dont you do it using a pagination type logic i.e.
limit by 100,
loop thru that batch,
once that has completed,
reload the page with the next offset in mind like send-email.asp?offset=100, send-email.asp?offset=200, etc.
use that offset value to get next batch
repeat process until end of recordset.
At least you have less chance of it timing out altho you can increase it: server.ScriptTimeout = 180
First of all I'd maximize the number of BCC's per cycle. Let's say you can email 50 BCC's in one go; you should: especially when you need this page about 20 times a day.
Anyway. Before you start; maximize the scripttimeout
Server.ScriptTimeout = 2147483647