-
October 23rd, 2013, 12:54 AM
#1
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!!!
-
October 23rd, 2013, 01:52 AM
#2
Re: Adavnced Email App
Try this:
Code:
Public Sub InitializeOpenFileDialog()
' Set the file dialog to filter for graphics files.
Me.OpenFileDialog1.Filter = _
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + _
"All files (*.*)|*.*"
' Allow the user to select multiple images.
Me.OpenFileDialog1.Multiselect = True
Me.OpenFileDialog1.Title = "My Image Browser"
End Sub
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
-
October 23rd, 2013, 02:05 AM
#3
Re: Adavnced Email App
The easiest method is to create a collection or list of attachments... example code below..
Code:
Public Class attachments
Private _ext As String
Private _path As String
Public Property ext() As String
Get
Return _ext
End Get
Set(ByVal value As String)
_ext = value
End Set
End Property
Public Property Path() As String
Get
Return _path
End Get
Set(ByVal value As String)
_path = value
End Set
End Property
End Class
now in your code...
Code:
Public Class Form1
'Dim picturePath As String
'Dim pictureExt As String
Dim Picture as new list of attachments
'Dim filePath As String = ""
and to attach the image to the mail...
Code:
If (openDlg.ShowDialog() = DialogResult.OK) Then
Dim TmpAttachment as attachments
TmpAttachment.Path = openDlg.FileName
tmp = TmpAttachment.Path.Split(".")
TmpAttachment.ext = tmp(1)
Picture.add(TmpAttachment)
img = Image.FromFile(picturePath)
orgData = Clipboard.GetDataObject
Clipboard.SetImage(img)
txtBody.Paste()
End If
and you can use the same method for standard file attachments too...
lastly to send... you need to loop over each attachment...
Code:
avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, MediaTypeNames.Text.Html)
For Each ThisPicture in Picture
Select Case ThisPicture.ext.ToUpper
Case "JPG", "JPEG"
pic1 = New LinkedResource(ThisPicture.Path, MediaTypeNames.Image.Jpeg)
Case "GIF"
pic1 = New LinkedResource(ThisPicture.Path, MediaTypeNames.Image.Gif)
Case "TIFF", "TIF"
pic1 = New LinkedResource(ThisPicture.Path, MediaTypeNames.Image.Tiff)
End Select
pic1.ContentId = "Pic1"
avHtml.LinkedResources.Add(pic1)
Next
Of course you will have to make proper provisions for other file types too..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
October 23rd, 2013, 02:07 AM
#4
Re: Adavnced Email App
Fanx for your reply
But my problem is not choosing multiple files
But rather once he has already chosen multiple files how to convert the rtf to html?
-
October 23rd, 2013, 12:48 PM
#5
Re: Adavnced Email App
Originally Posted by Dhan17
Fanx for your reply
But my problem is not choosing multiple files
But rather once he has already chosen multiple files how to convert the rtf to html?
Actually, If you look closely at the code you posted, it only ever stores the LAST selected file, and while it may show all the selected files in the msg composition window, only the LAST selected one is attached to the sent msg...
checkout the code I posted for helpful hints..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|