How to embed images in an exchange email message? Using Microsoft.Exchange.Webservices

  • Thread starter Thread starter GoneToTheDogs
  • Start date Start date
G

GoneToTheDogs

Guest
I'm moving from using SmtpClient to Exchange.WebServices due to emails being blocked by the receiving server. I thought by doing this I could use our company's exchange server, which would be recognized as safe by the receiving server.

The issue I'm running into is that I had embedded images and I don't know how to embed images using Exchange.WebServices. So, my question is: How do you send an exchange email with embedded images? (see attached Images)

Here is the HTML results. Notice the image tags.:

<meta
http-equiv="Content-Type" content="text/html;
charset=us-ascii"><p><img src="cid:pic1"><br><br></p>
<h3>Thank you for your request!</h3><br><br><img
src="cid:pic2">
<br><br><br>
<p>Dear Tester Test,<br><br>Your request is important to me
and I am working to resolve it as quickly as possible.<br>I've included
the details for your request as well as my contact information
below.<br><br>Ticket Number:&nbsp;
2983<br>Inquiry:&nbsp; General Request -
Test<br>Status:&nbsp; Assigned to Owner<br><br>I'll be
contacting you shortly with further information.<br><br>Sincerely,<br>Tester<br>tester@test.com<br>123-123-1234<br>Other
Info<br><br><br><br></p>
1305711.jpg

This screenshot shows what it should look like (and works using SMTP, unless it gets blocked)


1305712.jpg

Here is what I'm getting now using the code below. Images aren't shown because I don't know how to attach and relate them to the embedded image tags (noted in the html displayed above).

Here is the vb.net code that I am working on now:

Note: I am sending the Html without the embedded images because I can't find any method to do this with the Exchange.WebServices class. If you look at htmlView variable it is of type AlternateView, which contains the images but doesn't work with this class, so I had to leave it out until I found something else that worked.

Private sub SendCustomerEmailNotificationExchange(ByVal CompanyCode As String, ByVal TktId As String, ByVal TktType As String, ByVal TktStatus As eStatusKey)

Dim service As New ExchangeService(ExchangeVersion.Exchange2013)
service.UseDefaultCredentials = true
service.AutodiscoverUrl(HubertUser.Email)

Dim emailaddress As String = ""
Dim emailSubject As String = ""
Dim emailBody As String = ""
Dim plainBody As String = ""
Dim CustEmailAddress As String = ""
Dim UserEmail As String = ""

Dim mail As New EmailMessage(service)

Dim esc As Boolean = False
Dim EmailCount As Integer = 0

Dim Complete_emailSubject As String = ""
Dim Complete_emailBody As String = ""

Dim resp As DialogResult = Windows.Forms.DialogResult.No

emailSubject = BuildCustomerEmailSubject(CompanyCode, GetTicketTypeID(TktType), TktId, TktStatus)
emailBody = BuildCustomerEmailBody(CompanyCode, GetTicketTypeID(TktType), TktId, TktStatus)


'Get Ticket Owner's email address

If Not IsNothing(emailBody) Then

Try
'---------------------------------------------------
'Build plaintext (modify emailBody)

plainBody = crumsUtilities.StripHTML(emailBody)


'---------------------------------------------------
'Set up customer email address

If TestRun Then
CustEmailAddress = UserEmail
'CustEmailAddress = GetCustomerEmailAddress()
Else
Dim sCreateDate As String = ""
Dim dCreateDate As Date = "1/1/1800"
Try
sCreateDate = OConv(recTicketHeader.ReadV(TktId, "TICKET", Account, Ticket.CreateDate).ToString, "D4/")
Date.TryParse(sCreateDate, dCreateDate)
Catch ex As Exception
'error, so use an old create date which will not send the notification
resp = MessageBox.Show("Error during launch date check. Okay to send the customer notification?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If resp = Windows.Forms.DialogResult.No Then
Exit Sub
End If
End Try

If DateTime.Compare(dCreateDate, gCustNotifLaunchDate) >= 0 Then 'LaunchDate stored in Crums_Control with ID='Cust_Notif_LaunchDate'
CustEmailAddress = GetCustomerEmailAddress().ToLower
Else
'Dont Send
Exit Sub
End If
End If

'---------------------------------------------------


mail.From = UserEmail 'logged in user's company email address : Environment.UserName.ToLower & "@company.com"
mail.From = tktOwnerEmail
mail.ToRecipients.Add(CustEmailAddress) 'Send-to email address
mail.Subject = emailSubject


'mail.ReplyToList.Add(New MailAddress(tktOwnerEmail))
'mail.Headers.Add("Disposition-Notification-To", tktOwnerEmail)

'mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess
'mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

Dim alinkedImages As New ArrayList
Dim linkedImageCounter As Integer = 1
Dim imgSrcTag As String = ""
Dim imgSrcContent As String = ""
Dim Pos As Integer = 1
Dim endPos As Integer = 0
Dim nextTag As Integer = 0
Dim ImgTypePos As Integer = 0
Dim ImgType As String = ""

While Pos <> 0
Pos = Instr(Pos, emailBody, "<img src=")
If Pos > 0 Then
endPos = Instr(Pos, emailBody, "/>") + 1

'grab the img tag
imgSrcTag = Strings.Mid(emailBody, Pos, endPos - Pos + 1)
ImgTypePos = endPos - 1


imgSrcContent = Strings.Mid(imgSrcTag, 11, imgSrcTag.Length - 14)

'replace with mail tag and ID
emailBody = Strings.Replace(emailBody, imgSrcTag, "<img src=""cid:pic" & linkedImageCounter & """/>", 1, 1)

Dim linkedImage As New LinkedResource(imgSrcContent)
linkedImage.ContentId = "pic" & linkedImageCounter
ImgType = Strings.Right(imgSrcContent, 3)
Select Case ImgType.ToLower
Case "jpg"
linkedImage.ContentType = New ContentType(MediaTypeNames.Image.Jpeg)
Case "gif"
linkedImage.ContentType = New ContentType(MediaTypeNames.Image.Gif)
Case "iff"
linkedImage.ContentType = New ContentType(MediaTypeNames.Image.Tiff)
Case Else
'Image type not supported!
MessageBox.Show("Error during Email processing. Image Type not supported!" & vbCrLf & "Image Type: " & ImgType.ToLower & " (" & imgSrcContent & ")" & vbCrLf & "Using PlainText instead", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
End Select

alinkedImages.Add(linkedImage)

linkedImageCounter += 1
Else
Exit While
End If
Pos = endPos
End While


Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(emailBody, Nothing, "text/html")
Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(plainBody, Nothing, "text/plain")

For Each link In alinkedImages
htmlView.LinkedResources.Add(link)
Next

'mail.AlternateViews.Add(plainView)
'mail.AlternateViews.Add(htmlView)

mail.Body = emailBody
Try
mail.Send()
Catch ex As Exception
MsgBox(ex.Message)
End Try


'Dim recContro1 As New classControl1

'Dim smtpServer1 As String = ""
'Dim smtpServer2 As String = SMTP_FAILOVER

'Try
' smtpServer1 = recContro1.ReadV("CRUMS.SMTP", "CONTROL1", HubertAccount, 1)
'Catch ex As Exception
' smtpServer1 = SMTP_PRIMARY
'End Try

'Me.Cursor = Cursors.WaitCursor

'If crumsUtilities.IsClientAHubertIPAddress() Then
' 'User is in the office
' Try
' Dim smtpServer As New SmtpClient(smtpServer1)
' smtpServer.Send(mail)
' Catch ex1 As Exception
' Try
' Dim smtpServer As New SmtpClient(smtpServer2)
' smtpServer.Send(mail)
' Catch ex2 As Exception
' HubertMessageBox.Show("Attempting to send notification email to customer failed." & vbCrLf & ex2.Message & "Contact IS", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
' End Try
' End Try

'Else 'User is remote
' Try
' Dim smtpServer As New SmtpClient(smtpServer2)
' smtpServer.Send(mail)
' Catch ex1 As Exception
' Try
' Dim smtpServer As New SmtpClient(smtpServer1)
' smtpServer.Send(mail)
' Catch ex2 As Exception
' HubertMessageBox.Show("Attempting to send notification email to customer failed." & vbCrLf & ex2.Message & "Contact IS", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
' End Try
' End Try
'End If


Catch ex As Exception
MessageBox.Show("Error while attempting to send notification to customer at email address, " & CustEmailAddress & vbCrLf & ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
'Send it to the ticket owner
'mail.To.Add(tktOwnerEmail)
Finally
Me.Cursor = Cursors.Default
End Try

End If

End sub





~GoneToTheDogs

Continue reading...
 
Back
Top