CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Oct 2013
    Posts
    2

    Question Adavnced Email App

    Hi all,

    I need to develop a small but rather advanced email app using vb.net
    Up to now everything has been going smoothly until my lecturer asked me to include pictures in the email
    I successfully managed to include the picture as attachment but he wanted the picture to be included in the email body itself and not as attachment
    Now for 1 image I managed to do it by converting the email into html format and sending it
    But now the real problem is when the user wishes to include several pictures in the email
    How am i to detect the number of pictures and rewrite the equivalent email html??
    I am using a richtextbox for the input of text and pictures

    What I managed to do up till now is as follows

    Code:
    Imports System.Net.Mail
    Imports System.Net.Mime
    
    Public Class Form1
        Dim picturePath As String
        Dim pictureExt As String
        Dim filePath As String = ""
    
        Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
            Dim smtp As New SmtpClient
            Dim avHtml As AlternateView
            Dim mail As MailMessage = New MailMessage
            Dim htmlBody As String
            Dim pic1 As LinkedResource
            Dim x() As String
    
            x = txtBody.Text.Split(" ")
    
            smtp.UseDefaultCredentials = False
            smtp.Credentials = New Net.NetworkCredential("***************", "**************")
            smtp.Port = "25"
            smtp.EnableSsl = True
            smtp.Host = "***********"
    
            Try
                htmlBody = "<html> "
                htmlBody = htmlBody & "<body> "
    
                If Not x(0) = Nothing Then htmlBody = htmlBody & x(0)
    
                htmlBody = htmlBody & "<br /> "
                htmlBody = htmlBody & "<img src='cid:Pic1'> "
                htmlBody = htmlBody & "<br /> "
    
                If Not x(1) = Nothing Then htmlBody = htmlBody & x(1)
    
                htmlBody = htmlBody & "</body> "
                htmlBody = htmlBody & "</html>"
    
                avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, MediaTypeNames.Text.Html)
    
                Select Case pictureExt
                    Case "jpg", "JPG", "jpeg", "JPEG"
                        pic1 = New LinkedResource(picturePath, MediaTypeNames.Image.Jpeg)
                    Case "gif", "GIF"
                        pic1 = New LinkedResource(picturePath, MediaTypeNames.Image.Gif)
                    Case "tiff", "tif", "TIFF", "TIF"
                        pic1 = New LinkedResource(picturePath, MediaTypeNames.Image.Tiff)
                End Select
    
                pic1.ContentId = "Pic1"
                avHtml.LinkedResources.Add(pic1)
    
                mail.From = New MailAddress("dhanishb@live.com")
                mail.To.Add(txtTo.Text)
                mail.Subject = txtSubject.Text
                mail.IsBodyHtml = True
                mail.Body = htmlBody
                mail.AlternateViews.Add(avHtml)
    
                If Not filePath = "" Then mail.Attachments.Add(New System.Net.Mail.Attachment(filePath))
    
                smtp.Send(mail)
    
                MsgBox("Email sent!")
    
                txtBody.Text = ""
                txtSubject.Text = ""
                txtTo.Text = ""
                picturePath = ""
                pictureExt = ""
                filePath = ""
            Catch ex As Exception
                MsgBox(ex.ToString)
            Finally
    
            End Try
        End Sub
    
        Private Sub btnAddImage_Click(sender As System.Object, e As System.EventArgs) Handles btnAddImage.Click
            Dim openDlg As OpenFileDialog = New OpenFileDialog
            Dim filter As String = openDlg.Filter
            Dim tmp() As String
            Dim img As Image
            Dim orgData
    
            openDlg.Filter = "Image File (*.tiff;*.jpg;*.gif)|*.tiff;*.jpg;*.gif"
            openDlg.Title = "Open an image file"
    
            If (openDlg.ShowDialog() = DialogResult.OK) Then
                picturePath = openDlg.FileName
    
                tmp = picturePath.Split(".")
    
                pictureExt = tmp(1)
    
                img = Image.FromFile(picturePath)
    
                orgData = Clipboard.GetDataObject
    
                Clipboard.SetImage(img)
    
                txtBody.Paste()
            End If
        End Sub
    
        Private Sub btnAddAttachement_Click(sender As System.Object, e As System.EventArgs) Handles btnAddAttachement.Click
            Dim openDlg As OpenFileDialog = New OpenFileDialog
    
            If (openDlg.ShowDialog() = DialogResult.OK) Then
                filePath = openDlg.FileName
            End If
        End Sub
    End Class
    Last edited by GremlinSA; October 23rd, 2013 at 01:41 AM. Reason: removed Credentials from code!!!

Tags for this Thread

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

Featured