unable to send multiple emails through SMTP

alaric

Well-known member
Joined
Oct 12, 2005
Messages
53
Programming Experience
Beginner
[RESOLVED] unable to send multiple emails through SMTP

Hi

Im trying to send multiple emails through a smtp server.
Sending a single one goes OK (even with att).

but when trying to send more than one....
the error raised is:
Console.WriteLine(exp.Message) gives:
Could not access 'CDO.Message' object.

Console.WriteLine(exp.InnerException.Message.ToString) gives:
Exception has been thrown by the target of an invocation.


Im using the code from MS 101 examples:

I build a string (strEmailTo) with the emailadresses. and seperate the emailadresses by ';' (to be sure, I remove the last ; one)

VB.NET:
[SIZE=2][COLOR=#0000ff]
Dim[/COLOR][/SIZE][SIZE=2] sb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StringBuilder
[/SIZE][SIZE=2][COLOR=#008000]' Build the email message body.
[/COLOR][/SIZE][SIZE=2]sb.Append("The following email was sent to you from the Send Mail How-To " & _
"sample application:")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("SUBJECT: ")
sb.Append(Trim([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtSubject.Text))
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("MESSAGE: ")
sb.Append(Trim([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtMessage.Text))
sb.Append(vbCrLf)
[/SIZE][SIZE=2][COLOR=#008000]' Creating a mail message is as simple as instantiating a class and 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' setting a few properties.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mailMsg [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MailMessage
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] mailMsg
.From = strSender
.To = strEmailto.Trim
[/SIZE][SIZE=2][COLOR=#008000]'.Cc = txtCC.Text.Trim
[/COLOR][/SIZE][SIZE=2].Bcc = Mid(strEmailto, 1, Len(strEmailto.Trim) - 1)
.Subject = txtSubject.Text.Trim
.Body = sb.ToString
[/SIZE][SIZE=2][COLOR=#008000]'.Priority = CType(cboPriority.SelectedIndex, MailPriority)
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] IsNothing(arlAttachments) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mailAttachment [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] mailAttachment [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] arlAttachments
.Attachments.Add(mailAttachment)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Set the SmtpServer name. This can be any of the following depending on
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' your local security settings:
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' a) Local IP Address (assuming your local machine's SMTP server has the 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' right to send messages through a local firewall (if present).
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' b) 127.0.0.1 the loopback of the local machine.
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' c) "smarthost" or the name or the IP address of the exchange server you 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' utilize for messaging. This is usually what is needed if you are behind
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' a corporate firewall.
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' See the Readme file for more information.
[/COLOR][/SIZE][SIZE=2]SmtpMail.SmtpServer = strSMTP
[/SIZE][SIZE=2][COLOR=#008000]' Use structured error handling to attempt to send the email message and 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' provide feedback to the user about the success or failure of their 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' attempt.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]SmtpMail.Send(mailMsg)
lstAttachments.Items.Clear()
lstAttachments.Items.Add("(No Attachments)")
MessageBox.Show("Your email has been successfully sent!", _
"Email Send Status", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] exp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MessageBox.Show("The following problem occurred when attempting to " & _
"send your email: " & exp.Message, _
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Console.WriteLine(exp.Message)
Console.WriteLine(exp.InnerException.Message.ToString)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE]

I've been trying diffent way and googled myself dizzy.
Maybe anyone here seen this before and know a solution:eek:

VS 2003: 7.1.3088
.net framework: 1.1.4322
 
Last edited:
That error usually means something is wrong with smtp server or communication with smtp server.

First investigate the exception more by checking all possible innerexceptions like this, if you are lucky there will be some info there to get you on track on what the problem is specifically:
VB.NET:
Catch ex As Exception
  Dim sbex As New System.text.StringBuilder
  sbex.Append(ex.Message)
  sbex.Append(vbCrLf)
  While Not (ex.InnerException Is Nothing)
    sbex.Append("InnerException reported: ")
    sbex.Append(ex.InnerException.ToString())
    sbex.Append(vbCrLf)
    ex = ex.InnerException
  End While
  MsgBox(sbex.ToString)
End Try
There may be some security on the smtp server to prevent mass-emailing that kicks in if there are more than a certain count of recipients for a message.

You also add the same recipients to both To and BCC, from my tests I think duplicate recipients are discarded.

Have a look a this place ( http://www.systemwebmail.net/ ) also, if you don't first find a solution, several CDO.Message problems and solutions discussed there.

(I did try your code with several recipients, and did not get any error.)
 
John if you remember this thread then you will know why i am replying.
I suppose you were the who objected for using of vb6 "date" data type.
Well, now you are using the one vbCRLF (Visual Basic Carriage Return Line Feed) while it has .NET equivalent (Search MSDN knowledge base :))

I offer my appologize for the inconvinience if any but all i wanted to say is that it is sort of usuall (if we use vb6 code sometime) ... althrough we should tend always to use only pure .NET code

Regards ;)
 
thx for your quick reply.

i found the answer at the site you referred to!
It had to do with authentication. I did not authenticate on thus, i could only send to myself.



thx again
 
Good to know there was a solution, not all SMTP servers have authentication (some just filter out from sender IP to know if it's their customer or not).
 
Back
Top