Solved

how do i send an email to a user via code

Posted on 2004-10-23
202 Views
Last Modified: 2012-05-05
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
0
Question by:Johnny
    34 Comments
     
    LVL 18

    Expert Comment

    by: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
    0
     

    Author Comment

    by:Johnny
    VB.Net
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
    0
     

    Author Comment

    by:Johnny
    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
    0
     

    Author Comment

    by:Johnny
    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 ??

                      
                      
    0
     

    Author Comment

    by:Johnny
    thx for the nifty property discrip too
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    I mentioned that in my lastpost:

    You'll need

    System.Web.Mail

    -tushar
    0
     

    Author Comment

    by:Johnny
    my fault sorry
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    Oh dont worry, that happens.. post back if you got it working :>
    0
     

    Author Comment

    by:Johnny
         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..
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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

    0
     

    Author Comment

    by:Johnny
         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
                      
    0
     

    Author Comment

    by:Johnny
    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
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    hmmm.. damn..

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

    -tushar
    0
     

    Author Comment

    by:Johnny
    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
                      
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
    0
     

    Author Comment

    by:Johnny
         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!
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
    0
     

    Author Comment

    by:Johnny
         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
    0
     

    Author Comment

    by:Johnny
    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)
    0
     

    Author Comment

    by:Johnny
    nope no IIS
    0
     
    LVL 18

    Accepted Solution

    by:
    I want to get this solved as much as you..

    Your new SendEmail function will look like following:

    ----------------------------------------------------------------------------------------------------
    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"
            System.Web.Mail.SmtpMail.SmtpServer = "Localhsot"
            System.Web.Mail.SmtpMail.Send(msg)

        End Function
    ----------------------------------------------------------------------------------------------------

    Tell me your development environment..

    -tushar
    0
     

    Author Comment

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

    and thx for your persistance im inpressed
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
    0
     
    LVL 38

    Assisted Solution

    by:PaulHews
    Go into references for your project and add a reference to System.Web.DLL

    Dont give me any points, just helping out on this one.
    0
     

    Author Comment

    by:Johnny
         
    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
                      
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    System.Web.Mail.SmtpMail.Send()

    Requires MailMessage object as argumnet

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

    -tushar
    0
     

    Author Comment

    by:Johnny
    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)
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    :)

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

    i've got my points

    -tushar
    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    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...
    0
     

    Author Comment

    by:Johnny
    min was 20 thx alot again
    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    So thanks anyway... :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now