Solved

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

Posted on 2006-11-14
20
904 Views
Last Modified: 2011-08-18
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
Comment
Question by:2326ac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 2
  • +4
20 Comments
 
LVL 5

Expert Comment

by:prajapati84
ID: 17945062
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
 
LVL 15

Expert Comment

by:OMC2000
ID: 17945091
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
 
LVL 12

Expert Comment

by:sandip132
ID: 17945095
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
Industry Leaders: 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!

 
LVL 7

Expert Comment

by:mjmarlow
ID: 17945112
What is the precise error message?
0
 

Author Comment

by:2326ac
ID: 17945116
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
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 17945155
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
 

Author Comment

by:2326ac
ID: 17945197
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
 
LVL 27

Expert Comment

by:Sammy
ID: 17945250
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
 

Author Comment

by:2326ac
ID: 17945290
I am not too sure to be honest what class should I use?
0
 
LVL 7

Expert Comment

by:mjmarlow
ID: 17945327
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
 

Author Comment

by:2326ac
ID: 17945339
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
 

Author Comment

by:2326ac
ID: 17945376
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
 
LVL 5

Expert Comment

by:prajapati84
ID: 17945387
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
 
LVL 12

Expert Comment

by:sandip132
ID: 17945394
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
 
LVL 12

Expert Comment

by:sandip132
ID: 17945433
just relace SmtpMail.Host with
SmtpMail.SmtpServer="<server name>"
0
 

Author Comment

by:2326ac
ID: 17945463
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
 

Author Comment

by:2326ac
ID: 17945484
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
 
LVL 12

Accepted Solution

by:
sandip132 earned 500 total points
ID: 17945487
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
 

Author Comment

by:2326ac
ID: 17945586
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
 

Author Comment

by:2326ac
ID: 17946386
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
looking for an ASP.net report designer 4 41
Json and ajax 1 19
ASP.net Moving Visual Studio 2015 project to 2017 2 36
asp.net, radiobuttonlist, c# 2 26
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question