Failure Sending Mail / smtp.Send(message)

for some reason for this simple function I created, the portion smtp.Send(message) is throwing an error;
I get the error "Failure Sending Mail".  I don't get anything much other than that in the error message...unles there's another place I should be looking.

This is ASP.NET 2.0 code using the System.Net.mail namespace:

    Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)

        Dim message As New System.Net.Mail.MailMessage("myemail@whatever.com", "myemail@whatever.com")
        message.Subject = "Exception"
        message.Body = "Error happened in this area" & place & "Error information: " & exception_msg

        Dim smtp As New System.Net.Mail.SmtpClient("Exchange servername")
        smtp.Send(message)  <-------------- Error happens here

    End Function

The servername I'm puting in there is one I've used on other servers as an smtp client.  I'm not sure if this just can't talk to external servers or not or just that I have a syntax problem.
LVL 1
dba123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
Hi dba123,

see http://www.systemnetmail.com/

Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AJesaniCommented:
Yes, smtp will not send email to external domains, unless you authenticate thru your smtp server.

 static void Authenticate()
{
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");

//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("username", "secret");
smtp.Send(mail);

}
0
dba123Author Commented:
ok, thanks.  So I have to use the local server then and setup smtp on that I assume.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

dba123Author Commented:
this is not sending email to external domains.  What I'm saying is I'm tryign to reference an internal mail server...and use that as the IP for the smtpClient
0
dba123Author Commented:
the "relay server" in this case is our own internal exchange server...emails are only being sent to internal exchange mailboxes.
0
aki4uCommented:
Try this:

Imports System.Web.Mail


    Private Sub SendEmail(ByVal exception_msg As String, ByVal place As String)
        Dim email As SmtpMail
        Dim msg As New MailMessage

        email.SmtpServer = "Exchange servername"

        msg.Priority = MailPriority.Normal
        msg.From = "myemail@whatever.com"
        msg.To = "myemail@whatever.com"
        msg.Subject = "Exception"
        msg.Body = "Error happened in this area" & place & vbCrLf & "Error information: " & exception_msg
        msg.BodyFormat = MailFormat.Html

        email.Send(msg)
    End Sub
0
nauman_ahmedCommented:
You should check the Event Log since it may contains the detailed information about the failure. Are you sure the Exchange server you are trying to connect allow connections from outside? Why dont you use the IIS SMTP server for mail distribution? If you would like to learn more about Smtp Server setup please refere to the following aricle:

Begining ASP.NET Development - Part 1
http://www.techinet.com/GetContent.aspx?ContentID=14&SectionID=1&SectionItemID=2

http://www.systemwebmail.com/ is also an excellent site on the mailing features in .NET.

-Nauman.
0
dba123Author Commented:
aki4u, just wondering, what is wrong with mine?
0
dba123Author Commented:
aki4u, yours is not quite there, remember, this is .NET 2.0 and VS 2005 I'm using:

Dim email As SmtpMail
Error      3      Type 'SmtpMail' is not defined.

msg.From = "myemail@whatever.com"
Error      4      Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'.

msg.To = "myemail@whatever.com"
Error      5      Property 'To' is 'ReadOnly'.

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

msg.BodyFormat = MailFormat.Html
Error      7      Name 'MailFormat' is not declared.
0
dba123Author Commented:
nauman_ahmed, awesome links....thanks!  I had forgotten to setup smtp on my server
0
aki4uCommented:
Create a new page and try this:

Imports System.Web.Mail

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call SendEmail("whatever", "whatever")
    End Sub

    Private Sub SendEmail(ByVal exception_msg As String, ByVal place As String)
        Dim email As SmtpMail
        Dim msg As New MailMessage

        email.SmtpServer = "Exchange servername"
        With msg
            .Priority = MailPriority.Normal
            .From = "myemail@whatever.com"
            .To = "myemail@whatever.com"
            .Subject = "Exception"
            .Body = "Error happened in this area" & place & vbCrLf & "Error information: " & exception_msg
            .BodyFormat = MailFormat.Html
        End With
        email.Send(msg)
    End Sub
End Class
0
nauman_ahmedCommented:
I have converted a MSDN sample on this topic that also sends attachment with the mail.  The code is translated using the utility at http://carlosag.net/Tools/CodeTranslator/Default.aspx:

Public Shared Sub CreateMessageWithAttachment(ByVal server As String)
        ' Specify the file to be attached and sent.
        ' This example assumes that a file named Data.xls exists in the
        ' current working directory.
        Dim file As String = "data.xls"
        ' Create a message and set up the recipients.
        Dim message As MailMessage = New MailMessage("jane@contoso.com", "ben@contoso.com", "Quarterly data report.", "See the attached spreadsheet.")
        ' Create  the file attachment for this e-mail message.
        Dim data As Attachment = New Attachment(file, MediaTypeNames.Application.Octet)
        ' Add time stamp information for the file.
        Dim disposition As ContentDisposition = data.ContentDisposition
        disposition.CreationDate = System.IO.File.GetCreationTime(file)
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file)
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file)
        ' Add the file attachment to this e-mail message.
        message.Attachments.Add(data)
        'Send the message.
        Dim client As SmtpClient = New SmtpClient(server)
        ' Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials
        client.Send(message)
        ' Display the values in the ContentDisposition for the attachment.
        Dim cd As ContentDisposition = data.ContentDisposition
        Console.WriteLine("Content disposition")
        Console.WriteLine(cd.ToString)
        Console.WriteLine("File {0}", cd.FileName)
        Console.WriteLine("Size {0}", cd.Size)
        Console.WriteLine("Creation {0}", cd.CreationDate)
        Console.WriteLine("Modification {0}", cd.ModificationDate)
        Console.WriteLine("Read {0}", cd.ReadDate)
        Console.WriteLine("Inline {0}", cd.Inline)
        Console.WriteLine("Parameters: {0}", cd.Parameters.Count)
        For Each d As DictionaryEntry In cd.Parameters
            Console.WriteLine("{0} = {1}", d.Key, d.Value)
        Next
        data.Dispose
    End Sub

0
dba123Author Commented:
I want to know why mine wasn't working!  my last post.
0
dba123Author Commented:
aki4u, I had the correct namespace at the top of my page, why didn't your first example work in my .net page?
0
dba123Author Commented:
I am workign in a console project, not an aspx
0
dba123Author Commented:
Look, smtpMail doesn't exist in the namespace I'm using:  see here:

http://www.webfound.net/smtpMail.jpg

where is it?  I'm assuming that's asp.net 1.1 or 1.0 which is why...different namespace that 2.0 doesn't use in your example?
0
dba123Author Commented:
Ok folks.  I'm using System.Net.Mail class so let's not focus on System.Web.Mail here...the syntax I've seen so far is System.Web.Mail.  Mine is correct when using System.Net.Mail and that's what I'm sticking to.
0
nauman_ahmedCommented:
dba123,

Which .NET 2.0 version are you using? The final version of .NET 2.0 do not have any parameters in the MailMessage constructor. The following simple code work in VS.NET 2005 without any errors:

Dim msg As MailMessage = New MailMessage
msg.To = "toemails@site.com"
msg.From = "fromemail@site.com"
msg.Subject = "Subject"
msg.Body = "Body"
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg)

-Nauman.
0
dba123Author Commented:
so far nauman_ahmed  is on target...I'm still testing my code and getting rid of errors and will get back to you....  ASP.NET 2.0!!! not 1.0 or 1.1 here so therefore I'm using System.Net.Mail!
0
nauman_ahmedCommented:
woops dba123, I didn know about System.Net.Mail was introduced in 2.0. Thanks for the info. I tried compiling the following code and didn recieve any error:

Dim msg As MailMessage = New MailMessage("myemail@whatever.com", "myemail@whatever.com")
msg.Subject = "Exception"
msg.Body = "Body"
Dim client As SmtpClient = New SmtpClient("localhost")
client.Send(msg)

Can you please tell if you recieve the error in compilation or running the application? If so what is the error code?

-Nauman.
0
nauman_ahmedCommented:
Did you also include imports statement for System.Net.Mail?

-Nauman.
0
dba123Author Commented:
of course, I said I was using Imports System.Net.Mail  am I missing another import?
0
nauman_ahmedCommented:
Imports System.Net.Mail
Module Module1

    Sub Main()
        Dim msg As MailMessage = New MailMessage("myemail@whatever.com", "myemail@whatever.com")
        msg.Subject = "Exception"
        msg.Body = "Body"
        Dim client As SmtpClient = New SmtpClient("localhost")
        client.Send(msg)
    End Sub

End Module

Tried the above code and it was compiled.

-Nauman.
0
dba123Author Commented:
I forget how to find the version I'm using....
0
dba123Author Commented:
on the server .net 2.0.50727 which is where my code lies.  On mylocal PC, not sure...
0
nauman_ahmedCommented:
Console.WriteLine(System.Environment.Version.ToString())

-Nauman.
0
dba123Author Commented:
Errors with my code below:

message.From = "someemailaddress"
Error      3      Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'.

message.To = "myemailaddress"
Error      4      Property 'To' is 'ReadOnly'.

Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)

        Dim message As New MailMessage
        message.From = "someemailaddress"
        message.To = "myemailaddress"
        message.Subject = "HEDI 2 Exception"
        message.IsBodyHtml = True
        message.Body = "Error happened in this area" & place & "Error information: " & exception_msg

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

    End Function


I'm not sure why I get the read-only error, is there some sort of property I need to add?
0
aki4uCommented:
dba123,

your code also works, but message it's not instantly sent, like in my example.
Code I sent you works for 1.1/2.0 framework.

the simplest code I can think of is this(also works in 2.0):

        Dim smtp As New System.Net.Mail.SmtpClient
        smtp.Host = "Exchange servername"
        smtp.Send("myemail@whatever.com", "myemail@whatever.com", "Subject", "Test")

0
dba123Author Commented:
you have to typecast the To addresses now...see article above...
0
dba123Author Commented:
that article I found tells it all folks.  Thanks for all your help though!!!

    Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)

        Dim message As New MailMessage
        Dim fromAddress As New MailAddress("mike@elsewhere.com", "Mike")
        Dim toAddress As New MailAddress("mike@work.com", "Mike")
        Dim msessage As New MailMessage(fromAddress, toAddress)

        message.Subject = " Exception"
        message.IsBodyHtml = True
        message.Body = "Error happened in this area" & place & "Error information: " & exception_msg

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

    End Function
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.