Send Mail  
Author Message
Zen_Rain





PostPosted: Visual Basic Express Edition, Send Mail Top

I'm having trouble from two ends with this bit of code to send form letters. First, the "InvalidOperationException" box appears when "Send" is clicked, and "A first chance exception of type 'System.InvalidOperationException' occurred in System.dll" appears in the Immediate Window. Second, and probably related, the mail is not sent. Can anyone help here

Imports System

Imports System.Net.Mail

Imports System.Text

Public Sub Mail(ByVal togroup As String, ByVal type As String, ByVal office As String, ByVal jobname As String)

Dim fromaddress As MailAddress = New MailAddress( )

Dim SmtpServer As New SmtpClient

Dim subj As New StringBuilder

Dim body As New StringBuilder

Try

Try

Dim Message As MailMessage = New MailMessage()

Message.From = fromaddress

If togroup = "Test" Then

Message.To.Add( )

ElseIf togroup.ToString = "All" Then

Message.To.Add( )

End If

If type = "New Residential" Then

subj.Append("New Residential Project in ")

subj.Append(office)

body.Append("The ")

body.Append(office)

body.Append(" has started a new job! The ")

body.Append(jobname)

body.Append(" is starting its design phase.")

body.Append(vbCrLf)

body.Append("This message was automatically generated. Do not reply.")

Message.Subject = subj.ToString

Message.Body = body.ToString

MsgBox(body.ToString, MsgBoxStyle.OkOnly, "ATOKAD")

End If

Try

Dim client As New SmtpClient()

Dim username As String =

Dim password As String = "cred"

Dim credentials As New Net.NetworkCredential(username, password)

With client

.Host = "mail.clydescad.com"

.Port = 25

.Credentials = credentials

.Send(Message)

End With

Catch ehttp As System.Net.HttpListenerException

Console.WriteLine("0", ehttp.Message)

Console.WriteLine("Here is the full error message")

Console.Write("0", ehttp.ToString())

End Try

Catch erecip As SmtpFailedRecipientsException

'Notifies of mail failure due to invalid addresses

Dim counter As Integer = 0

Dim errormsg As New StringBuilder

While counter < erecip.InnerExceptions.Length

errormsg.Append("Failed to deliver message to ")

errormsg.Append(erecip.FailedRecipient.ToString)

errormsg.Append(vbCrLf)

End While

MsgBox(errormsg.ToString, MsgBoxStyle.OkOnly, "ATOKAD")

Catch eblank As ArgumentNullException

'Notifies of mail failure due to blank fields

MsgBox("Recipients not specified", MsgBoxStyle.OkOnly, "ATOKAD")

Catch esmtp As SmtpException

'Notifies of mail failure due to mail server connection failure

MsgBox("SMTP Server Error; mail not sent", MsgBoxStyle.OkOnly, "ATOKAD")

Catch e As IndexOutOfRangeException

' Display usage instructions if error in arguments.

Dim use As usage = New usage()

use.DisplayUsage()

End Try

Catch e As NullReferenceException

MsgBox("Null Reference", MsgBoxStyle.OkOnly, "ATOKAD")

Catch e As InvalidOperationException

MsgBox("Invalid Operation", MsgBoxStyle.OkOnly, "ATOKAD")

Catch e As System.Exception

' Display text of unknown error.

Console.WriteLine("Unknown Exception occurred 0", e.Message)

Console.WriteLine("Here is the Full Error Message")

Console.WriteLine("0", e.ToString())

End Try

End Sub



Visual Studio Express Editions6  
 
 
Zen_Rain





PostPosted: Visual Basic Express Edition, Send Mail Top

Note, this code also fails with port 587.
 
 
Babak Izadi





PostPosted: Visual Basic Express Edition, Send Mail Top

Hi there,

Change this line of your code with mine and try it again:

smtp.Credentials = New Net.NetworkCredential("username", "password")

****************************************************************************************

smtp.Credentials = CredentialCache.DefaultNetworkCredentials

Babak Izadi
LotraSoft Ltd.



 
 
Sven De Bont





PostPosted: Visual Basic Express Edition, Send Mail Top

You have to create new instance of the System.Web.Mail.MailMessage class by using the new keyword

Dim message As New System.Web.Mail.MailMessage()

The reason you don't have to do this for the SmtpMail.SmtpServer and SmtpMail.Send, is because these are Shared methods of the SmtpMail class



 
 
Ashish Basran





PostPosted: Visual Basic Express Edition, Send Mail Top

I think your local IIS's SMTP server might not be capable to relay the mail. (I am assuming everything is working fine in your code as there is not exception, including security settings). What you can do is, check the folders under Inetpub\mailroot... your SMTP might be keeping mails there in Queue, pickup or bad mails.



 
 
Spidermans_DarkSide





PostPosted: Visual Basic Express Edition, Send Mail Top

Hi,

For VB6 questions see one of the forums listed here.>>

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=551512&SiteID=1

or ask on this forum site.>>

http://www.programmersheaven.com/c/MsgBoard/wwwboard.asp Board=14&src=20&Setting=

Regards,

S_DS



 
 
Mike Danes





PostPosted: Visual Basic Express Edition, Send Mail Top

It seems that your mail server is not configured to relay messages. You need to check its configuration.
 
 
wizkid1





PostPosted: Visual Basic Express Edition, Send Mail Top

You can't use a loopback address for the SMTPClient.

Use this:

SmtpClient smtp = new SmtpClient(the host for your mail server. Mine is smtp.comcast.net)

Does this help



 
 
mkrtchyan.arsen





PostPosted: Visual Basic Express Edition, Send Mail Top

Thank you for replay. Yes you are right my mails are in the queue what can i do And yes can i write anything


 
 
LilianaNT





PostPosted: Visual Basic Express Edition, Send Mail Top

hi!

thanks for your help, but unfortunately, it didn't work. it continues the same error.


 
 
nobugz





PostPosted: Visual Basic Express Edition, Send Mail Top

Pray tell, what line of code generates the exception


 
 
Eder Pardeiro .





PostPosted: Visual Basic Express Edition, Send Mail Top

Thank you,

This link resolv my problem. I found the solution.



 
 
LynnOoi





PostPosted: Visual Basic Express Edition, Send Mail Top

hi,

i still have problems regarding it. i have add the "New" keyword already but it show another error message, saying that "could not access 'CDO.Message'" object.



 
 
mkrtchyan.arsen





PostPosted: Visual Basic Express Edition, Send Mail Top

127.0.0.1 is in my relay list. I must do anything else


 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Send Mail Top

I don't know if this makes a difference but remove the arrow brackets <> from the email addresses.

http://msdn2.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

would be nice if you could tell us where the code throws the error!



 
 
SJWhiteley





PostPosted: Visual Basic Express Edition, Send Mail Top

You aren't allowed to relay mail through that server; this is a pretty common issue. Here's a useful link to learn about sending mail using the Mail namespace:

http://www.systemnetmail.com/default.aspx



 
 
Sven De Bont





PostPosted: Visual Basic Express Edition, Send Mail Top

Can you post the full exeption please. The innerexception will most likely contain usefull information about the error.



 
 
LynnOoi





PostPosted: Visual Basic Express Edition, Send Mail Top

ok.. here is the error.. (is this the error exception u asked me to post if not, where can i get the full exception ) i'm sorry as i'm new in this field.

'DefaultDomain': Loaded 'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.

'TestingMail': Loaded 'C:\Documents and Settings\lichioo\My Documents\Visual Studio Projects\TestingMail\bin\TestingMail.exe', Symbols loaded.

'TestingMail.exe': Loaded 'c:\windows\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll', No symbols loaded.

'TestingMail.exe': Loaded 'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No symbols loaded.

'TestingMail.exe': Loaded 'c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll', No symbols loaded.

'TestingMail.exe': Loaded 'c:\windows\assembly\gac\system.web\1.0.5000.0__b03f5f7f11d50a3a\system.web.dll', No symbols loaded.

An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll

Additional information: Could not access 'CDO.Message' object.

'TestingMail.exe': Loaded 'c:\windows\assembly\gac\microsoft.visualbasic\7.0.5000.0__b03f5f7f11d50a3a\microsoft.visualbasic.dll', No symbols loaded.

> TestingMail.exe!TestingMail.Form1.Button1_Click(Object sender = {System.Windows.Forms.Button}, System.EventArgs e = {System.EventArgs}) Line 153 + 0x8 bytes Basic

TestingMail.exe!TestingMail.Form1.Main() Line 5 + 0x1d bytes Basic



 
 
OmegaMan





PostPosted: Visual Basic Express Edition, Send Mail Top

Take a look at this post and recreate the code in a console application. If it does not have the same problems as the original, you may want to redo the methodology.


 
 
mkrtchyan.arsen





PostPosted: Visual Basic Express Edition, Send Mail Top

SmartHost is empty can is it true


 
 
Ashish Basran





PostPosted: Visual Basic Express Edition, Send Mail Top

Check if your SMTP server accepts connections from your application and allows to relay mails.

You might need to give rights at your SMTP server to allow accepting connections from the machine where your application is running and need to give rights to relay mails.

hope this helps you!!!!



 
 
Ashish Basran





PostPosted: Visual Basic Express Edition, Send Mail Top

Yes, you got it right. You need to provide value for smartHost.

then that server will relay your mails



 
 
Sven De Bont





PostPosted: Visual Basic Express Edition, Send Mail Top

Most likely, that exception has some nested 'inner exceptions', to access those, you will have to catch the error and enumerate the innerexeptions

Try the code bellow:

Try

SmtpMail.Send(mail)

Catch ex As Exception

While Not (ex.InnerException Is Nothing)

MessageBox.Show(ex.InnerException.ToString())

ex = ex.InnerException

End While

End Try



 
 
LynnOoi





PostPosted: Visual Basic Express Edition, Send Mail Top

the inner exception:

system.reflextion.TargerInvocationException: Exception has been thrown by target of an invocation. --> System.Runtime.InteropServices.COMException (0x80040213) : The transport failed to connect to the server.

--- End of inner exception stack trace ---

* is this indicating the i enter wrong smtp server name or my connection to the LAN is improper



 
 
mkrtchyan.arsen





PostPosted: Visual Basic Express Edition, Send Mail Top

Now i receive message to my yahoo account but can't receive to my local mail in the Virtual Machine Can anyone help


 
 
daniel testa





PostPosted: Visual Basic Express Edition, Send Mail Top

Hi. Are you using VS 2005 if yes then use SmtpClient class instead of SmtpMail which is now obsolete in .Net framework 2.0

SmtpMail was more like a wrapper for CDO (Collaboration Data Objects) which is COM.  

If you have, for example, authentication problems you'll receive the same error message if you use SmtpMail. Check things like if you have access to that server on port 25 or if you have to authenticate to the server.

SmtpClient is not a warpper and it doesn't use CDO.

 


 
 
Ashish Basran





PostPosted: Visual Basic Express Edition, Send Mail Top

If you are receiving mail in your Yahoo! account, that means the system is able to relay the mail successfully. Now if you want to get your message in local mail, you need to find if your system is able to recieve mail and your SMTP is able to relay to your local mail.....



 
 
LynnOoi





PostPosted: Visual Basic Express Edition, Send Mail Top

i'm using VS2003 only.

must i add the port number in the smtpserver address



 
 
Sven De Bont





PostPosted: Visual Basic Express Edition, Send Mail Top

If it's any other than port 25, you should add the port numer. The error message is very clear, stating that it can not connect to the server.

I tried connecting to that server myself (web-proxy.mys.agilent.com) and my telnet session timed out.

It's also possible that your (windows) firewall is blocking you application



 
 
LynnOoi





PostPosted: Visual Basic Express Edition, Send Mail Top

how can i include the port number in the smtpserver address

by the way, really thanks a lot for helping...



 
 
LynnOoi





PostPosted: Visual Basic Express Edition, Send Mail Top

hi,

before i continue tracking the error, can you please tell me where can i get the smtpServer address i jus wana confirm that i have inserted the correct 1.



 
 
Sven De Bont





PostPosted: Visual Basic Express Edition, Send Mail Top

The smtp server is usually given to you by your provider. When you are in a company, they usaully have their own smtp server (like a Microsoft Exchange Server). If you are a home user, this is the same server as you specify in the 'outgoing mail server' of you mail client (like Outlook).

Depending on where the application is used, the smtp server address will vary, so it's a good idea to store it in a configuration file so that it can be changed without having to recompile the application.

As for specifying a different port number. I've been looking at it, and apparently there is no standard way to specify the port number in the SmtpMailSmtpServer. Normally you won't need to do this as almost every mail server will operate on port 25.



 
 
LynnOoi





PostPosted: Visual Basic Express Edition, Send Mail Top

ok..thanks a lot.

 
 
LynnOoi





PostPosted: Visual Basic Express Edition, Send Mail Top

Hi,

i would like to ask whether how am i going to add attachment in my mails from my program.