Results 1 to 16 of 16

Thread: Rich Text Emails and Attachments

  1. #1

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Rich Text Emails and Attachments

    I have written a DLL to connect to a microsoft ESMTP server.

    What I have not been able to figure out (thus far) or find on the net is how to send 1) emails in rich text format or html and 2) how to send attachments.

    I tried sending rich text format and just placing the rich text where the body normally would go, however this showed up as plain text displaying the RTF code. Is there a different server command that must be sent for these things?

  2. #2
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Thanks to James Stanich, I used this program you can get on planetsourcecode, wich has a HTML control, allowing you to send both text and HTML.




    Unfortunatly, I'm not being able to put it to work completely. This is weird, because if I add a breakpoint, and cycle line by line [with F8] I'm able to send an e-mail, while if I run the program [with F5/Ctrl+F5] I get an SMTP connection time out!

    Can anyone correct the code in the program I pointed? I really need to include something alike in my program, and help would be really appreciated!

  3. #3

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222
    In the DLL I wrote I am setting a timeout value - basically, once a command is sent, it loops for the specified number of milliseconds awaiting a response. If the response times out, it goes to the next line of code.

    Something like that COULD help, however you may also want to try using a couple of DoEvents statements. It sounds like the app is simply executing too fast, whereas when you step through, it is holding each command until you release it.

    Thanks for the post - I will check out this app.

  4. #4

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222
    Looking at his code, when it is calling winsock.GetResponse, it is making a single call instead of looping and awaiting a response.

    Try replacing

    Code:
        smtp.GetData Response
    with

    Code:
        Dim strTemp As String
        
        strTemp = ""
        
        Do While strTemp = ""
            sock.GetData strTemp
            DoEvents
        Loop

  5. #5
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Thanks for the help, but no luck.

    Try using the code yourself. Maybe you can see what I can't.

  6. #6
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Did you have any luck, Finn?

  7. #7

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222
    I didn't look at it any more - It didn't really do anything that mine doesn't do already, so I didn't spend any time looking at it.

    On another note.....

    I am still looking for the smtp commands to send data in rich text/html format however. I have noticed that in the mail header of most rich text/html emails, there is a content type attribute. Does this have to be set somewhere through SMTP?

    I also am looking for a page or documents that outline MIME encoding of attachments and how it is done through SMTP. Through trial and error I have gotten uuencode attachments to work, however would like to support MIME as well.

    Any help is greatly appreciated as I have been searching the web for days with not much luck.

  8. #8
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    Originally posted by finn0013
    I didn't look at it any more - It didn't really do anything that mine doesn't do already, so I didn't spend any time looking at it.
    Could I please have a look at it, please? Based on the program I pointed above I'm not being able to send mails successfully. Could you please help?

  9. #9

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222
    Here is a tutorial page on how to do it. This is almost identical to the way I did it in my control. Let me know if this helps or not.

    It wouldn't let me upload an *.htm file (which is what this is). Just download then rename to *.htm.
    Attached Files Attached Files

  10. #10
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    It's... empty!

  11. #11

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222
    Weird.....

    Here ya go:

    Part II: Creating the E-Mail Component
    Step 1
    Using Windows Explorer, create a new directory where you can build the ActiveX E-Mail component for your e-mail information system. Name the directory \VBMAIL.

    Start a new Visual Basic 5.0 ActiveX DLL project. Access the properties for your new project by choosing Project1 Properties from the Project menu. On the General tab in the Project Properties dialog box, change the project name to VBMail and change the project description to Active Server Pages E-Mail Component. Click OK.

    When the project is first opened, a default class named CLASS1 is created. Change the name of this default class to Connector.

    Save the project in the directory \VBMAIL.

    Step 2
    You can create the e-mail connector by using Winsock, an ActiveX control that ships with Visual Basic 5.0 and allows a Visual Basic component to access the Internet. With Winsock, you can automatically send e-mail.

    Because Winsock is an ActiveX control, you need to add a form to your project to host the control, even though the component will not have a user interface. To add this form, choose Add Form from the Project menu and name the form frmVBMail.

    Choose Components from the Project menu. In the Components dialog box, select Microsoft Winsock Control 5.0 and click OK.

    Add the Winsock control to your newly created form by double-clicking the control in the Visual Basic Toolbox. Change the name of the control to sockVBMail. Save your project.

    Step 3
    In Active Server Pages code, the Connector class will be the primary interface for e-mail. This class must include properties that allow you to designate the sender and receiver of the mail, as well as the mail content. Add the following code to the Connector class to create the necessary properties for the E-Mail component:

    ` Variables
    Private m_Sender As String
    Private m_Recipient As String
    Private m_SenderName As String
    Private m_RecipientName As String
    Private m_Subject As String
    Private m_Body As String
    Private m_Server As String

    ` Property procedures
    Public Property Get Sender() As String
    Sender = m_Sender
    End Property

    Public Property Let Sender(strSender As String)
    m_Sender = strSender
    End Property

    Public Property Get Recipient() As String
    Recipient = m_Recipient
    End Property

    Public Property Let Recipient(strRecipient As String)
    m_Recipient = strRecipient
    End Property

    Public Property Get SenderName() As String
    SenderName = m_SenderName
    End Property

    Public Property Let SenderName(strSenderName As String)
    m_SenderName = strSenderName
    End Property

    Public Property Get RecipientName() As String
    RecipientName = m_RecipientName
    End Property

    Public Property Let RecipientName(strRecipientName As String)
    m_RecipientName = strRecipientName
    End Property

    Public Property Get Subject() As String
    Subject = m_Subject
    End Property

    Public Property Let Subject(strSubject As String)
    m_Subject = strSubject
    End Property

    Public Property Get Body() As String
    Body = m_Body
    End Property

    Public Property Let Body(strBody As String)
    m_Body = strBody
    End Property

    Public Property Get Server() As String
    Server = m_Server
    End Property

    Public Property Let Server(strServer As String)
    m_Server = strServer
    End Property

    Step 4
    After all the information is provided to the component through the properties, you can call the Send method of the Connector class to send e-mail. To create the Send method, choose Add Procedure from the Tools menu. In the Add Procedure dialog box, set the following attributes:

    Name: Send
    Type: Function
    Scope: Public

    After the Send function is added, designate the return data type as Boolean. The complete function definition should appear as follows:

    Public Function Send() As Boolean
    End Function

    You will complete the Send method shortly, but first add some supporting functionality to the component.

    Step 5
    Because many of the steps involved in sending e-mail require a response from the e-mail server, you need to create a routine that causes the E-Mail component to wait for the server's response. This can be accomplished with a private routine named GetResponse. Add the routine to the Connector class by choosing Insert Procedure from the Tools menu. In the Insert Procedure dialog box, set the following attributes:

    Name: GetResponse
    Type: Sub
    Scope: Private

    After the GetResponse routine is created, add the following code to force a wait for the server's response:

    Dim strTemp As String

    strTemp = ""
    Do While strTemp = ""
    frmVBMail.sockVBMail.GetData strTemp
    DoEvents
    Loop

    Step 6
    In this step, you will create the code necessary to use SMTP, the protocol that allows communication between the e-mail server and the client. Although this project was originally designed to use Microsoft Exchange Server 5.0, you can use any e-mail server that supports SMTP.

    Return to the Send method, and add the following code to connect to the SMTP mail server:

    With frmVBMail.sockVBMail
    Send = True

    ` Connect to SMTP server
    If .State <> sckConnected And .State <> sckOpen Then
    .Connect Server, 25
    Do While .State <> sckConnected
    DoEvents
    Loop
    End If

    Step 7
    Once the E-Mail component is successfully connected to the e-mail server, you can construct and send a message using SMTP. The message is sent to the server in parts until each necessary piece—sender, recipient, body—is received. Then the server sends the message to the recipient. Add the following code to the Send method to build and send an e-mail message:

    On Error GoTo SendErr

    With frmVBMail.sockVBMail
    Send = True

    ` Connect to SMTP server
    If .State <> sckConnected And .State <> sckOpen Then
    .Connect Server, 25
    Do While .State <> sckConnected
    DoEvents
    Loop
    End If

    ` Create the message
    Dim strMessage As String

    ` The sender
    strMessage = "MAIL FROM:" & Sender & vbCrLf
    .SendData strMessage
    GetResponse

    ` The recipient
    strMessage = "RCPT TO:" & Recipient & vbCrLf
    .SendData strMessage
    GetResponse

    ` The body of the message
    strMessage = "DATA" & vbCrLf
    .SendData strMessage
    GetResponse

    strMessage = "DATE:" & Format$(Now, "dd/mm/yy") & vbCrLf
    strMessage = strMessage & "FROM:" & SenderName & vbCrLf
    strMessage = strMessage & "TO:" & RecipientName & vbCrLf
    strMessage = strMessage & "SUBJECT:" & Subject & vbCrLf & vbCrLf
    strMessage = strMessage & Body & vbCrLf
    strMessage = strMessage & "." & vbCrLf
    .SendData strMessage
    GetResponse

    Step 8
    After the message is sent, you must close the connection to the e-mail server. Use the Close method of the Winsock control to close the connection and complete the Send method code:

    ` Close connection
    .Close
    Do While .State <> sckClosed
    DoEvents
    Loop
    End With

    SendExit:
    Exit Function

    SendErr:
    Send = False
    Resume SendExit

    Save your project.

    Step 9
    Before you can use the E-Mail component in an ASP page, you must compile the DLL by choosing Make VBMail.dll from the File menu. Then save your work, and exit Visual Basic.

  12. #12
    Junior Member
    Join Date
    Nov 2004
    Posts
    22

    Question send html with DM Easy Mail Sender

    With DM Easy Mail Sender is possible to insert image in the HTML text but when you send this text, image not is send but there is red "x". How can i do?

    I want insert image in the body of email send.

  13. #13

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222
    Wow - been a long time since I originally posted this. In order to get this to work you will have to use MIME encoding and send the email in either rich text format or HTML format. I am not very familiar with RTF formatting, but if you use HTML you can just build tables and format just like a web page.

    If the control you are using supports HTML formatted body, just write the html and stick it in and it should work.

    I am sure there is a way to send an email with pictures as attachments then include those pictures in the body, but that is a lot of overhead. Your best bet for this is direct your image tag to point to an image on your/a web server.

  14. #14

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222
    Alternatively if you want to write your own control:

    RFC 821 - SMTP
    RFC 2045 - MIME Part 1
    RFC 2046 - MIME Part 2


    There are a ton more on that site depending on what you want to do. Just search for SMTP.

  15. #15
    Junior Member
    Join Date
    Nov 2004
    Posts
    22

    Question dhtml Editor

    I would want to capture from present control DHTML Editor in the form of visual basic project, the selected text and save it in one string constant. How I can make it?

  16. #16

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222
    Are you talking about grabbing text from another application's textbox? If so you will need to use API.

    Do a search on FindWindowEx and it will bring up tons of examples.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width