Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

how do i send an email to a user via code

id like to send an email of usersemail and password from my program (UsersEmail,Password) how do i send an email of lets say;

------------ begin email --------------
This email is so you may keep a record of your email and password


E-Mail: UsersEmail
Password: Password

Please save this message for your records in case you lose your logon information

----------------- end email ----------------

how do i send this via my program with haveing UsersEmail and Password populated with the info.
UsersEmail is the email im going to send to as well

thx
Johnny
aka Pern
Avatar of tusharashah
tusharashah

Hi Pern,
Which Language you'll be using?

Following is code in C# & VB.Net for this
---------------------------------------------------------------------------------------------------------------------
C#
-------
public void SendEmail()
{
      MailMessage msg = new MailMessage();
      msg.From = "YourEmail@Email.com";
      msg.Subject = "Your Subject";
      msg.Body = "Your Body";
      msg.To = "Email@yahoo.com";
      msg.Bcc = "IfAny@anything.com";
      SmtpMail.SmtpServer = "localhost";   // your SMTP server
      SmtpMail.Send(msg);
}

VB.Net
--------
Public Sub SendEmail()
 Dim msg As MailMessage = New MailMessage ()
 msg.From = "YourEmail@Email.com"
 msg.Subject = "Your Subject"
 msg.Body = "Your Body"
 msg.To = "Email@yahoo.com"
 msg.Bcc = "IfAny@anything.com"
 SmtpMail.SmtpServer = "localhost"
 SmtpMail.Send(msg)
End Sub
---------------------------------------------------------------------------------------------------------------------

-tushar
Avatar of Johnny

ASKER

VB.Net
Well that was irrilavent question from me.. realize that I was in VB.Net section.. the code in Vb.Net is right there.. also check out following property of MailMessage class that you can use..

You'll need to use using System.Web.Mail namespace.


Property Description:
Attachments             Used for sending e-mails with attachments
From                       Sender's e-mail address
To                           Recipient's e-mail address
Cc                           Recipient's e-mail address (Carbon Copy)
Bcc                          Recipient's e-mail address (Blind Carbon Copy)
Body                        Text of the e-mail message
BodyFormat              Specifies the format of an e-mail message (Possible Values: Text, Html)
Priority                     Specifies the priority of an e-mail message (Possible Values: High, Low, and Normal)
Subject                    Denotes the subject of an e-mail message
Headers                   Denotes a collection of acceptable headers (Example: Reply-To)
BodyEncoding           Specifies the method of encoding an e-mail message (Possible Values: Base64 and UUEncode)

-tushar
Avatar of Johnny

ASKER

great for sending the email the sub...thx

i get how just do

msg.Body = "Your Body" & UsersEmail & "more msg body"

for sending my info

thanks that wroks for me

Johnny
aka Pern
Avatar of Johnny

ASKER

dang it it says
      Error      1            Type 'MailMessage' is not defined.

      Error      2            Name 'SmtpMail' is not declared.

i hate whenit does that... thats a include thing right ??

                  
                  
Avatar of Johnny

ASKER

thx for the nifty property discrip too
I mentioned that in my lastpost:

You'll need

System.Web.Mail

-tushar
Avatar of Johnny

ASKER

my fault sorry
Oh dont worry, that happens.. post back if you got it working :>
Avatar of Johnny

ASKER

     Error      1            'Mail' is not a member of 'Web'.


i put this in my modual so i can (at some point pass the values to it and reuse it...(if i can figure that out)

im still learning this so please bare with me

heres my function
 Public Function SendEmail()
        Using System.Web.Mail


            ''    Property Description()
            ''Attachments             Used for sending e-mails with attachments
            ''       From(Sender) 's e-mail address
            ''To                           Recipient's e-mail address
            ''        Cc(Recipient) 's e-mail address (Carbon Copy)
            ''        Bcc(Recipient) 's e-mail address (Blind Carbon Copy)
            ''Body                        Text of the e-mail message
            ''BodyFormat              Specifies the format of an e-mail message (Possible Values: Text, Html)
            ''Subject                    Denotes the subject of an e-mail message
            ''BodyEncoding           Specifies the method of encoding an e-mail message (Possible Values: Base64 and UUEncode)

            Dim msg As MailMessage = New MailMessage()
            msg.From = "YourEmail@Email.com"
            msg.Subject = "Your Subject"
            msg.Body = "Your Body"
            msg.To = "Email@yahoo.com"
            msg.Bcc = "IfAny@anything.com"
            SmtpMail.SmtpServer = "localhost"
            SmtpMail.Send(msg)
        End Using
    End Function

sorry for aksing all these dum questions..
2 Correction:

1) Replace
-->> Using System.Web.Mail
with --> Imports System.Web.Mail

2) And you'll put this with other Import on the top
(eg. Imports System.Data  )


-tushar

Avatar of Johnny

ASKER

     Error      1            Namespace or type specified in the Imports 'System.Web.Mail' cannot be found.      

LOL ok now its really broken (smile)

i put  it out side the Module Module1 very top line
                  
Avatar of Johnny

ASKER

how about we make this easy lets say its the only think in a modual and i want to pass all this info to it how do i do that...
and whats the line to fire it all??

maybe i wont get any errors then

thx
hmmm.. damn..

Replace:
         Dim msg As MailMessage = New MailMessage()
With:
        Dim msg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage

-tushar
Avatar of Johnny

ASKER

still have same other errors and new one
      Error      1            Namespace or type specified in the Imports 'System.Web.Mail' cannot be found.

ok heres my complete code in modual

----------- code -------

Imports System.Web.Mail
Module Module1
    Public Function AppPath(Optional ByVal WithSlash As Boolean = False) As String
        'Dim a As String = AppPath(True) & "somepic.jpg"
        'Dim b As String = AppPath(False) & "\somepic.jpg"
        'Dim c As String = Application.StartupPath
        'Dim d As String = Environment.CurrentDirectory
        'Dim e As String = Application.ExecutablePath

        'MsgBox(a & vbCrLf & b & vbCrLf & c & vbCrLf & d & vbCrLf & e)

        Dim Result As String = Application.StartupPath
        If WithSlash Then
            If Result.EndsWith("\") = False Then
                Result &= "\"
            End If
        Else
            If Result.EndsWith("\") Then
                Result = Strings.Left(Result, Len(Result) - 1)
            End If
        End If
        Return Result
    End Function

    Public Function deleteFile(ByVal fileName As String)
        If System.IO.File.Exists(fileName) Then
            Try
                System.IO.File.Delete(fileName)
            Catch ex As Exception
                MsgBox(fileName & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Unable to delete file")
            End Try
        Else
            MsgBox(fileName, MsgBoxStyle.Critical, "File does not exist")
        End If

    End Function
    Public Function DownloadFile(ByVal uri As String, ByVal destFile As String, _
        Optional ByVal username As String = Nothing, Optional ByVal pwd As String = _
        Nothing)
        Dim wc As New System.Net.WebClient
        ''Dim myWebClient As New WebClient
        If Not username Is Nothing AndAlso Not pwd Is Nothing Then
            wc.Credentials = New System.Net.NetworkCredential(username, pwd)
        End If

        Try

            wc.DownloadFile(uri, destFile)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function

    Public Function SendEmail()

        ''    Property Description:
        ''Attachments   Used for sending e-mails with attachments
        ''From          Sender's e-mail address
        ''To            Recipient's e-mail address
        ''Cc            Recipient's e-mail address (Carbon Copy)
        ''Bcc           Recipient's e-mail address (Blind Carbon Copy)
        ''Body          Text of the e-mail message
        ''BodyFormat    Specifies the format of an e-mail message (Possible Values: Text, Html)
        ''Subject       Denotes the subject of an e-mail message
        ''BodyEncoding  Specifies the method of encoding an e-mail message (Possible Values: Base64 and UUEncode)

        Dim msg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage

        msg.From = "YourEmail@Email.com"
        msg.Subject = "Your Subject"
        msg.Body = "Your Body"
        msg.To = "Email@yahoo.com"
        msg.Bcc = "IfAny@anything.com"
        SmtpMail.SmtpServer = "localhost"
        SmtpMail.Send(msg)

    End Function

End Module
                  
When you replace with following line you dont need Imports..

Dim msg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage


& with Imports:
 make sure Imports is the top most line and outside your Class

-tushar
Avatar of Johnny

ASKER

     Error      1            Type 'System.Web.Mail.MailMessage' is not defined.
      Error      2            Name 'SmtpMail' is not declared.
      Error      3            Name 'SmtpMail' is not declared.

took out
Imports System.Web.Mail

left
Module Module1

still no joy!
I'm feeling the joy..

You also need to replace:
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg)

with:
        System.Web.Mail.SmtpMail.SmtpServer = "localhost"
        System.Web.Mail.SmtpMail.Send()

-tushar
Also, since you're getting all short of errors.. if IN CASE you get any errors regarding RELAY then do following:

Go to:
 Administrator Tools --> IIS --> Default SMTP Server --> Properties --> Access --> Relay
& Make sure you have selected 'all except the list below'

-tushar
Avatar of Johnny

ASKER

     Error      1            Type 'System.Web.Mail.MailMessage' is not defined.
      Error      2            'Mail' is not a member of 'Web'.
      Error      3            'Mail' is not a member of 'Web'.

replaced as said.. and JOY went and left the bulding (smile)

thx for helping btw and being understanding
Avatar of Johnny

ASKER

i dont use IIs.. im useing argosoft's email server...with xp pro.. and apache/php/mysql as web space..

im also programming this in vb 2005 express in case that makes a diffrence

ill look that up and tell u on next volly what it is
i asume its bult into xp and uses it regardless if IIS is installed(or default install)
Avatar of Johnny

ASKER

nope no IIS
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

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 Johnny

ASKER

it look as you said... and did that in l;ast message

and thx for your persistance im inpressed
Just in case I am missing something take a look at following link from microsoft:

http://support.microsoft.com/default.aspx?scid=kb;en-us;314201

-tushar
This could be the case: (taken from above mentioned link)

Add a reference to System.Web.dll. To do this, follow these steps:
a.  On the Project menu, click Add Reference.
b.  On the .NET tab, locate System.Web.dll, and then click Select.  
c.  Click OK in the Add References dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries you selected, click Yes.

Though I do not need to add it in my Application somehow..

-tushar
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 Johnny

ASKER

     
it does not like line

System.Web.Mail.SmtpMail.Send()

Error      1            Overload resolution failed because no accessible 'Send' accepts this number of arguments.

we where missing the
Add a reference to System.Web.dll. To do this, follow these steps:a.  On the Project menu, click Add Reference.
b.  On the .NET tab, locate System.Web.dll, and then click Select.  
c.  Click OK in the Add References dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries you selected, click Yes.


now we only have the one error
getting closer
                  
System.Web.Mail.SmtpMail.Send()

Requires MailMessage object as argumnet

System.Web.Mail.SmtpMail.Send( msg )

-tushar
Avatar of Johnny

ASKER

works now

thx alot...

tusharashah would you mind if give 5 points to PaulHews as PaulHews was right about the refrence

i know PaulHews said no points but im fair..

thx again

and thats reall good looking girl JOY came back in the bulding..and kissed you!! (smile)
:)

oh yes.. i dont mind even if you give all points to him..

i've got my points

-tushar
I only posted because it looked like you were having problems figuring out the reference, but even then tushar posted that solution before me, so really no points necessary here.  Tushar stuck with you from the beginning...
Avatar of Johnny

ASKER

min was 20 thx alot again
So thanks anyway... :)