Mailing problem - update what?

  • Thread starter Thread starter Rob Oldfield
  • Start date Start date
R

Rob Oldfield

I'm using the following to send mail from within apps....

Imports System.Web.Mail

Public Sub email(ByVal subject As String, ByVal msg As String, ByVal
whoFrom As String, _
ByVal whoTo As String, Optional ByVal attach As String = "")
'Original version taken from Q314201
Dim eMsg As MailMessage = New MailMessage
Dim eAttch As MailAttachment, str As String
Dim SMTPServer = "MyServer"
eMsg.From = whoFrom
eMsg.To = whoTo
eMsg.Subject = subject
eMsg.Body = msg
If attach <> "" Then
Dim eFile As String = attach
eAttch = New MailAttachment(eFile, MailEncoding.Base64)
eMsg.Attachments.Add(eAttch)
End If
SmtpMail.SmtpServer = SMTPServer
Try
SmtpMail.Send(eMsg)
Catch ex As Exception
str = "The attempt to send an email has failed." + vbCrLf +
vbCrLf + ex.Message
MessageBox.Show(str, "Error sending mail", MessageBoxButtons.OK)
End Try
eMsg = Nothing
eAttch = Nothing
End Sub

And that normally works. On some machines I'm getting the error "Could not
access 'CDO.Message' object." and on workstations I've got around that by
running Office update over the machines. It then works fine.

But I'm now getting the same error on a server. I run Office update and, of
course, it says there's nothing to do as Office isn't installed. So what do
I need to update?
 
I was fighting with this a while back. It has all to do with the mail server and its relaying capabilities. Here is something I found

4.2.3 The scary "Could not access 'CDO.Message' object" Printer Friendly Email This FAQ Discuss

This is probably the most common error thrown by System.Web.Mail. If you get this error, the FIRST THING TO DO, is to write out all InnerException messages.
This will tell you the true error and it will be easer to fix. See Checking the Exception (READ THIS FIRST) for more information.

Although this error message implies there is a permission problem, typically there isn't. However, after trying everything else, you may will want to try the suggestions listed at Error loading type library/DLL.

Now, on to some suggestions:

Suggestion 1
Specify a valid mail server for the SmtpMail.SmtpServer property. If that property is not set, at least set it to 127.0.0.1. For example:
SmtpMail.SmtpServer = "127.0.0.1

Suggestion 2
If you are using "localhost" or "127.0.0.1" as the SmtpMail.SmtpServer, you may not have permissions to relay through the IIS SMTP Service. To allow access, open up the IIS Admin MMC. Locate the SMTP Virtual Server, and right-click, then select Properties. On the Access tab, click the Relay button. In the Relay Restrictions dialog, grant your IP address (127.0.0.1) to the Computers listbox. Close down all dialogs, and restart the SMTP Service.

Suggestion 3
If you are using "localhost" or "127.0.0.1" as the SmtpMail.SmtpServer, make sure Anonymous access is allowd. To allow access, open up the IIS Admin MMC. Locate the SMTP Virtual Server, and right-click, then select Properties. On the Access tab, click the Authentication button. Be sure "Anonymous Access" is the only checkbox checked. Close down all dialogs, and restart the SMTP Service.

Suggestion 4
The email address does not have a valid TO address. After iterating through the InnerExceptions, you may find this error message actually has to do with relaying. Try sending a test email to an email address that exists on the server specified by SmtpMail.SmtpServer. If you can send an email to that server, then it is a relay issue. Talk to your mail server administrator about letting your code relay through the mail server.

Suggestion 5
Use a real FROM address that exists on the SmtpMail.SmtpServer. Do not use something like "(e-mail address removed)", or some other bogus address as your MailMessage.FromProperty. More advanced mail servers will catch this, and will deny relaying.

Suggestion 6
I have no idea why this suggestion works, but I found it on the web. I figured I would mention it, just in case Suggestion 1 did not work. Instead of specifying
SmtpMail.SmtpServer = "127.0.0.1
try
SmtpMail.SmtpServer.Insert( 0, "127.0.0.1 or your mail server name here"

Like I said, I don't know why this would work, but here is the thread: http://groups.google.com/groups?hl=...num=50&hl=en&lr=&ie=UTF-8&oe=UTF8&newwindow%3
 
* "Rob Oldfield said:
And that normally works. On some machines I'm getting the error "Could not
access 'CDO.Message' object." and on workstations I've got around that by
running Office update over the machines. It then works fine.

Are you sure, CDO is installed on the machine?
 
Many thanks for the suggestions, but before I get into the detail of
checking your suggestions, I'd like to just check on something...

At present, the SMTP server that I'm specifying is the company Exchange
server. That is working running the same code from at least one other
server in the same domain, set up in very much the same way (i.e. running
from the same account). Are you saying that, in general terms, you should
use 127.0.0.1 instead of the existing server? It seems a bit like overkill
to me to have to do this.

--
For real reply address, replace the _surprised_ bits with dots

Iulian Ionescu said:
I was fighting with this a while back. It has all to do with the mail
server and its relaying capabilities. Here is something I found:
4.2.3 The scary "Could not access 'CDO.Message' object" Printer Friendly Email This FAQ Discuss

This is probably the most common error thrown by System.Web.Mail. If you
get this error, the FIRST THING TO DO, is to write out all InnerException
messages.
This will tell you the true error and it will be easer to fix. See
Checking the Exception (READ THIS FIRST) for more information.
Although this error message implies there is a permission problem,
typically there isn't. However, after trying everything else, you may will
want to try the suggestions listed at Error loading type library/DLL.
Now, on to some suggestions:

Suggestion 1
Specify a valid mail server for the SmtpMail.SmtpServer property. If that
property is not set, at least set it to 127.0.0.1. For example:
SmtpMail.SmtpServer = "127.0.0.1"

Suggestion 2
If you are using "localhost" or "127.0.0.1" as the SmtpMail.SmtpServer,
you may not have permissions to relay through the IIS SMTP Service. To allow
access, open up the IIS Admin MMC. Locate the SMTP Virtual Server, and
right-click, then select Properties. On the Access tab, click the Relay
button. In the Relay Restrictions dialog, grant your IP address (127.0.0.1)
to the Computers listbox. Close down all dialogs, and restart the SMTP
Service.
Suggestion 3
If you are using "localhost" or "127.0.0.1" as the SmtpMail.SmtpServer,
make sure Anonymous access is allowd. To allow access, open up the IIS Admin
MMC. Locate the SMTP Virtual Server, and right-click, then select
Properties. On the Access tab, click the Authentication button. Be sure
"Anonymous Access" is the only checkbox checked. Close down all dialogs, and
restart the SMTP Service.
Suggestion 4
The email address does not have a valid TO address. After iterating
through the InnerExceptions, you may find this error message actually has to
do with relaying. Try sending a test email to an email address that exists
on the server specified by SmtpMail.SmtpServer. If you can send an email to
that server, then it is a relay issue. Talk to your mail server
administrator about letting your code relay through the mail server.
Suggestion 5
Use a real FROM address that exists on the SmtpMail.SmtpServer. Do not use
something like "(e-mail address removed)", or some other bogus address as your
MailMessage.FromProperty. More advanced mail servers will catch this, and
will deny relaying.
Suggestion 6
I have no idea why this suggestion works, but I found it on the web. I
figured I would mention it, just in case Suggestion 1 did not work. Instead
of specifying
 
Yes. The SMTP service is running. (...which links in to Iulain's post....
does that service actually need to be running?)

Am I missing something in it's configuration?
 
Problem sorted. Turned out to be your suggestion 6 that seemed to do the
job, but interesting stuff about the InnerExceptions as well - which I'd
never looked at before.

Thanks.

(I love solutions that I really don't understand.)
 
Back
Top