Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AJesani
AJesani

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);

}
Avatar of dba123

ASKER

ok, thanks.  So I have to use the local server then and setup smtp on that I assume.
Avatar of dba123

ASKER

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
Avatar of dba123

ASKER

the "relay server" in this case is our own internal exchange server...emails are only being sent to internal exchange mailboxes.
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

aki4u, just wondering, what is wrong with mine?
Avatar of dba123

ASKER

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.
Avatar of dba123

ASKER

nauman_ahmed, awesome links....thanks!  I had forgotten to setup smtp on my server
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
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

Avatar of dba123

ASKER

I want to know why mine wasn't working!  my last post.
Avatar of dba123

ASKER

aki4u, I had the correct namespace at the top of my page, why didn't your first example work in my .net page?
Avatar of dba123

ASKER

I am workign in a console project, not an aspx
Avatar of dba123

ASKER

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?
Avatar of dba123

ASKER

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.
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.
Avatar of dba123

ASKER

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!
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.
Did you also include imports statement for System.Net.Mail?

-Nauman.
Avatar of dba123

ASKER

of course, I said I was using Imports System.Net.Mail  am I missing another import?
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.
Avatar of dba123

ASKER

I forget how to find the version I'm using....
Avatar of dba123

ASKER

on the server .net 2.0.50727 which is where my code lies.  On mylocal PC, not sure...
Console.WriteLine(System.Environment.Version.ToString())

-Nauman.
Avatar of dba123

ASKER

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?
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")

Avatar of dba123

ASKER

you have to typecast the To addresses now...see article above...
Avatar of dba123

ASKER

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