Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 923
  • Last Modified:

ASP.NET - Any ideas why I can't send email from my code?

I found and changed the following code:

 Dim strfrom As String = "myemailaddress@email.com"
        Dim strto As String = "anotheremailaddress@email.com"
        Dim emailsub As String = "a subject"
        Dim bod As String

        bod = "body text"


        Dim mysmtpclient As New System.Net.Mail.SmtpClient("MyExchangeServer.MyCompaniesDomain.plc.uk")
        mysmtpclient.Send(strfrom, strto, emailsub, bod)

I am using visual web developer 2005 express using VB.

I get an error on the last line saying smtpexception was unhandled by user code.

I should also mention I have put the library Imports system.net.mail at the top of the page and am running this from a command button

I am using this on my client machine using VB's own web server while connected to my corporate network

Any help would be brill !
0
2326ac
Asked:
2326ac
  • 9
  • 4
  • 2
  • +4
1 Solution
 
prajapati84Commented:
Try this one...

        Dim email As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()
        email.From = "myemailaddress@email.com"
        email.To = "anotheremailaddress@email.com"
        email.Subject = "A Subject"
        email.BodyFormat = MailFormat.Html
        email.Body = "body text"
        SmtpMail.SmtpServer = ""
        SmtpMail.Send(email)

If not working, configure first of all SMTP on ur web server.

Regards,
Mukesh
0
 
OMC2000Commented:
if your mail server requires authentication, you should provide user name and password

if (!email.Fields.Contains("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"))
{
  email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "Basic"); // Anonimous, Basic, NTLM
  email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_account_name");
  email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "my_password");
}
0
 
sandip132Commented:
In earlier versions of ASP.NET (1.0 and 1.1), we used the System.Web.Mail class to send e-mail messages.  ASP.NET 2.0 replaced the System.Web.Mail class with the new and improved System.Net.Mail class.

The System.Web.Mail class uses the System.Web.Mail.SmtpMail class to handle the actual delivery of the e-mail message.  This has also been replaced by a new class (System.Net.Mail.SmtpClient) for the System.Net.Mail class.  

Try This:

  Dim msg As New System.Net.Mail.MailMessage()
  Dim smtp As New System.Net.Mail.SmtpClient("127.0.0.1")    ' Add Your SMTP Client server url or ip
  Dim fromadx As New System.Net.Mail.MailAddress("sample@yourdomain.com", "Term
  Paper Mailer")   'From Email ID
  Dim toadx As New System.Net.Mail.MailAddress("recipient@yourdomain.com",
  "Professor Plumb") ' To Email ID
  msg.From = fromadx
  msg.To.Add(toadx)
  msg.Subject = "SUBJECT" 'Subject line
  msg.Body = "Body text goes here..."
  smtp.Send(msg)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mjmarlowCommented:
What is the precise error message?
0
 
2326acAuthor Commented:
Thanks I have pasted your code in but I get an error on the first line telling me the following:

'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage.

I have changed 'System.Web.Mail.MailMessage' to System.Net.Mail.MailMessage which resolves that error but then further down I get errors

from here onwards saying

email.From = "myemailaddress@email.com"

Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'.

Any more ideas would be great
0
 
Ajay SharmaCommented:
sometimes asp.net application need smtp server to be specified , depending upon the hosting server settings, and sometimes not. so try to send mail using both (with smtp , and without).

if u are running code locally then dont specify a smtp server.
if u uploaded the code to the server and running it then do this
 
                Dim mail As New MailMessage
                mail.To = "abc@yourdomain.com"
                mail.From = "you@yourdomain.com"
                mail.BodyFormat = MailFormat.Html
                mail.Subject = "subject here"
                mail.Body = "your message text here"
               SmtpMail.Send(mail)

some servers dont let u send mail with bogus names, so please use a valid email-id in the "from" field, like if u uploaded the application to "www.bgsolution.com" then use "yourname@bgsolution.com" in the "mail.from" field.

0
 
2326acAuthor Commented:
Thanks

but even pasting your code:


        Dim mail As New MailMessage
        mail.To = "abc@yourdomain.com"
        mail.From = "you@yourdomain.com"
        mail.BodyFormat = MailFormat.Html
        mail.Subject = "subject here"
        mail.Body = "your message text here"
        SmtpMail.Send(mail)

tells me that the mail.to property is readonly !?
I am not using any fake mail address's etc I am also not even getting to the point of sending mail as it errors before the code is finished.
I will post my exact code below and if any one can recreate that would be good

----------------------------

Imports system.net.mail
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
         Dim strfrom As String = "acoulston@mycompname.com"
        Dim strto As String = "acoulston@mycompname.com"
        Dim emailsub As String = "subject"
        Dim bod As String

        bod = "dddd"


        Dim mysmtpclient As New System.Net.Mail.SmtpClient("EQ-EXNODE01.companyname.plc.uk")

        mysmtpclient.Send(strfrom, strto, emailsub, bod)
0
 
Sammy AgeilCommented:
2326ac,
you are using the correct class in your first post and the code should work fine if your smtp server doesnt require authentication
a few things may cause this error
1-virus scanners "Mainly McAffee" --> if you happen to have it, disable McAffee Scan On-Access
2-smtp doesnt allow relay

make sure you are using valid email addresses to be in the safe side

HTH
0
 
2326acAuthor Commented:
I am not too sure to be honest what class should I use?
0
 
mjmarlowCommented:
To resolve this problem:
email.From = "myemailaddress@email.com"
Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'.

You need to use something like this.  Sorry I dont know it in VB.
email.From = new System.Net.Mail.MailAddress("myemailaddress@email.com")
0
 
2326acAuthor Commented:
Thanks sandpid 132

I changed your code for mine see below but it errors with the same original error:

Imports system.net.mail
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim msg As New System.Net.Mail.MailMessage()
        Dim smtp As New System.Net.Mail.SmtpClient("10.0.0.7")    ' Add Your SMTP Client server url or ip
        Dim fromadx As New System.Net.Mail.MailAddress("acouls@lsg.com", "ddd")
        Dim toadx As New System.Net.Mail.MailAddress("acouls@lsg.com", "dd")
        msg.From = fromadx
        msg.To.Add(toadx)
        msg.Subject = "SUBJECT" 'Subject line
        msg.Body = "Body text goes here..."
        smtp.Send(msg)
    End Sub
End Class

0
 
2326acAuthor Commented:
Thanks mjmarlow I have changed your code to:

        Dim smtpmail As New System.Net.Mail.SmtpClient
        Dim email As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
        email.From = New System.Net.Mail.MailAddress("acoulton@liang.com")
        email.To = New System.Net.Mail.MailAddress("acoulston@liang.com")
        email.Subject = "A Subject"
        email.BodyFormat = MailFormat.Html
        email.Body = "body text"
        SmtpMail.SmtpServer = "10.0.0.7"
        SmtpMail.Send(email)

And I have listed the errors I get

Line 4 =  email.To = New System.Net.Mail.MailAddress("acoulston@liang.com")
Error      1      Property 'To' is 'ReadOnly'.      

Line 6 = email.BodyFormat = MailFormat.Html
Error      2      'BodyFormat' is not a member of 'System.Net.Mail.MailMessage'.      

Line 6  =email.BodyFormat = MailFormat.Html  
Error      3      Name 'MailFormat' is not declared.

Line 8 = SmtpMail.SmtpServer = "10.0.0.7"      
Error      4      'SmtpServer' is not a member of 'System.Net.Mail.SmtpClient'.

0
 
prajapati84Commented:
Can u try the same code again? Because I have tried this in my application and it worked. I know below 'System.Web.Mail.MailMessage' a green line will be displayed. But build this code. Before that pls check ur mail ids that r they valid or not?

        Dim email As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()
        email.From = "myemailaddress@email.com"
        email.To = "anotheremailaddress@email.com"
        email.Subject = "A Subject"
        email.BodyFormat = MailFormat.Html
        email.Body = "body text"
        SmtpMail.SmtpServer = ""
        SmtpMail.Send(email)

Regards,
Mukesh
0
 
sandip132Commented:
Here is working code from my application:

Dim fromAddress As New MailAddress("me@work.com", "me@ work")
Dim toAddress As New MailAddress("me@work.com", "me @ work")
Dim msg As New MailMessage(fromAddress, toAddress)
msg.Body = "<b>Testing new email</b>"
msg.Subject = "Testing new email, sent at " & DateTime.Now.ToString()
msg.IsBodyHtml = True
Dim mailSender As New System.Net.Mail.SmtpClient()
mailSender.Host = "(hostname)"  '''''ADD SMTP SERVER HERE.
Try    
  mailSender.Send(msg)    
  Label1.Text = "Message sent."
Catch ex As Exception    
  Label1.Text = ex.MessageEnd
Try
0
 
sandip132Commented:
just relace SmtpMail.Host with
SmtpMail.SmtpServer="<server name>"
0
 
2326acAuthor Commented:
sandip132  - many thanks your code builds and seems to work except the word ex.messageend.  It complies but I have not recieved the mail and nothing is in my sent items - i have put the ip address of my exchange server instead of host name - do you have any ideas about this?

thanks so much for your help

0
 
2326acAuthor Commented:
Saying that sandip132 - my code fails and the message does not send it fails with the original issue smtpexception was unhandles by user code.

the code builds but i get the message when i click the button on the line mailsender.send(msg)
I will publish the site to my local iis5 server on xp machine to see if that helps.
0
 
sandip132Commented:
this is a very working code... might be problem with SMTP server!!!

have you tried this:

relace SmtpMail.Host with
SmtpMail.SmtpServer="<server name>"
0
 
2326acAuthor Commented:
Possibly I will try to investigat on another machine how ever I would like to give this code a go if you can help:

       Dim place As String
        Dim exception_msg As String

        place = ""
        exception_msg = ""

        Dim message As New MailMessage
        Dim fromAddress As New MailAddress("md@wow.com", "Mike")
        Dim toAddress As New MailAddress("md@wow.com", "Mike")
        Dim msessage As New MailMessage(fromAddress, toAddress)

        message.From = fromAddress
        message.To  = md@wow.com
        message.Subject = " Exception"
        message.IsBodyHtml = True
        message.Body = "Error happened in this area" & place & "Error information: " & exception_msg

        Dim smtp As New SmtpClient("10.0.0.7")
        smtp.Send(message)

this code fails on the line message.To  = md@wow.com
it says the message.to property is read only
0
 
2326acAuthor Commented:
Thanks eveone for your help, it was a exchange as my pc's ip address was not permitted to use exchange as a smtp relay
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 4
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now