Send newsletter HTML Email with pictures as body - vb.net

I'm new to this group and i have one question. how can i attach pictures on the HTML newsletter that i have. when i send the newsletter everything goes well but the pictures are not showing on the email. please help. this is the code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Body As String = GetWebPageContent(Sender_email, Message)
Dim Mail As New MailMessage
Mail.Subject = "Test HTML"
Mail.To.Add(to_email)
Mail.From = New MailAddress(Sender_email)
Mail.Body = Body
Mail.IsBodyHtml = True
Mail.Priority = MailPriority.Normal
Dim SMTP As New SmtpClient(SMTP_Confg)
SMTP.EnableSsl = False
SMTP.Credentials = New System.Net.NetworkCredential(Sender_email, Password)
SMTP.Port = Port_Num
Try
SMTP.Send(Mail)
MsgBox("Successfully Sent!!!")
Catch ex As Exception
MessageBox.Show(" - your confermation email is not sent! " & vbNewLine & " Please contact your Administrator!")
End Try
End Sub
Private Function GetWebPageContent(ByVal recipient As String, ByVal customMsg As String) As String
Dim objStreamReader As New StreamReader("C:\Users\alex.GFH\Desktop\Email\beefree-s09uuvnlono.HTML")
'read html template file'
Dim bodyMsg As String = objStreamReader.ReadToEnd()
Return bodyMsg
End Function

It looks like you're copying the raw HTML from this file:
"C:\Users\alex.GFH\Desktop\Email\beefree-s09uuvnlono.HTML"
As the message body of your email.
My psychic powers tell me that you've got <img> tags inside your HTML that are pointing to relative locations. e.g. <img src="picture.jpg"/>. As such when the HTML email is opened in someone else's browser or mail app, the renderer has no idea where to fetch "picture.jpg" from.
And even if it did have the full URL, most mail apps won't fetch URL images by default until the user acknowledges the privacy risk.
I think if you want your images to just show up when the mail is opened, inline the image bytes directly into the <img> tag. You base 64-encode the image and stick on a header (e.g. data:image/png;base64, or data:image/jpeg;base64,. That becomes the src attribute.
E.g:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
More details here: https://www.rfc-editor.org/rfc/rfc2397

Related

Sending ICS Calendar File by Email Vb.net

When I send a calendar file by email, it appears correctly on recipient's email. When I send same file programmatically, it does not. here is what I do programmatically
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Mail As New MailMessage
Mail.Subject = "test email"
Mail.To.Add("someuser#gmail.com")
'Mail.To.Add("somesender#gmail.com")
Mail.From = New MailAddress("somesender#gmail.com")
Mail.Body = "This is a sample email using VB.NET"
Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment("C:\Users\x\Downloads\testCalendar.ics")
Mail.Attachments.Add(attachment)
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.EnableSsl = True
'SMTP.DeliveryMethod = SMTP.DeliveryMethod.SpecifiedPickupDirectory
SMTP.Credentials = New System.Net.NetworkCredential("somesender#gmail.com", "password")
SMTP.Port = "587"
SMTP.Send(Mail)
End Sub
here is the content of the test ics file
BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
DTSTART:20160930T110000
DTEND:20160930T120000
SUMMARY:Bastille Day Party
LOCATION:123 Main Street, Anahiem CA
DESCRIPTION:THIS IS A TEST DESCRIPTION
END:VEVENT
END:VCALENDAR
Any ideas?
thanks
There is absolutely no way to do this using System.Net.Mail as the resulting MIME message content will have the wrong structure. The VCalendar information needs to be contained within a specific MIME part but it will be placed in a separate one and treated as a normal attachment.
I wrote my own SMTP client and MIME message generator specifically to overcome this issue by creating the MIME content exactly as is required. The issue with doing this was that I needed to cater for different security protocols, such as NTLM, which was a pain to code.
You may be able to find a third-party SMTP client that supports the behaviour you require in the nuget gallery.

VB Authenticate User In Outlook Web App

I currently have a mail system using Microsoft's exchange server (OWA). I am trying to authenticate a user and send an email using pure Visual Basic code.
I have been trying to use a library called Aspose, however; I have no idea if I'm on the right track. I can not get it to work and I am not sure (since this is a company mail server) whether it's the server or it's my code.
Currently I have,
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create instance of ExchangeClient class by giving credentials
Dim client As Aspose.Email.Exchange.ExchangeClient = New Aspose.Email.Exchange.ExchangeClient("https://MAILSERVER.com/username/", "username", "password", "https://MAILSERVER.com/")
' Create instance of type MailMessage
Dim msg As Aspose.Email.Mail.MailMessage = New Aspose.Email.Mail.MailMessage()
msg.From = "username#MAILSERVER.com"
msg.To = "receivingemail#gmail.com"
msg.Subject = "test"
msg.HtmlBody = "test"
' Send the message
Try
client.Send(msg)
Console.WriteLine("made it")
Catch ex As Exception
Console.WriteLine("failed")
End Try
End Sub
I have obviously changed the username, password, and server name fields to generic ones but with (what I think is the right credentials) the output is always failed.
Can anybody help me out please?
Here is what I use:
Public Shared Function SendEMail(MailMessage As System.Net.Mail.MailMessage) As String
ErrorMess = ""
' Default the from address, just in case is was left out.
If MailMessage.From.Address = "" Then
MailMessage.From = New Net.Mail.MailAddress("donotreply#MAILSERVER.com")
End If
' Check for at least one address
If MailMessage.To.Count = 0 AndAlso MailMessage.CC.Count = 0 AndAlso MailMessage.Bcc.Count = 0 Then
ErrorMess = "No Addresses Specified"
Return ErrorMess
End If
' Create a SMTP connedction to the exchange 2010 load balancer.
Dim SMTPClient As New System.Net.Mail.SmtpClient("MAILSERVER.com")
Try
Dim ValidUserCredential As New System.Net.NetworkCredential
ValidUserCredential.Domain = "MAILSERVER.com"
ValidUserCredential.UserName = My.Resources.EmailUserName
ValidUserCredential.Password = My.Resources.EmailPassword
SMTPClient.UseDefaultCredentials = False
SMTPClient.Credentials = ValidUserCredential
SMTPClient.Send(MailMessage)
Return "Mail Sent"
Catch ex As Exception
ErrorMess = ex.Message & " " & ex.InnerException.ToString
Return ErrorMess
End Try
End Function
The ExchangeClient class is used to connect to Exchange server using the WebDav protocol and is used with Exchange Server 2003 and 2007. For OWA, you need to use the IEWSClient interface as shown in the following sample code. It has an Office365 test account that you can use to send a test email (the test account is solely for testing purpose and is not property of Aspose. I just created it for assisting you in testing the functionality). Please try it and if you face any problem, you may share the porblem on Aspose.Email forum for further assistance.
' Create instance of IEWSClient class by giving credentials
Dim client As IEWSClient = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "UserTwo#ASE1984.onmicrosoft.com", "Aspose1234", "")
' Create instance of type MailMessage
Dim msg As New MailMessage()
msg.From = "UserTwo#ASE1984.onmicrosoft.com"
msg.[To] = "receiver#gmail.com"
msg.Subject = "Sending message from exchange server"
msg.IsBodyHtml = True
msg.HtmlBody = "<h3>sending message from exchange server</h3>"
' Send the message
client.Send(msg)
I work with Aspose as Developer evangelist.

VB script and chrome

I'm having a lot of trouble modifying an existing website written in VB.NET.
Can someone please explain to me the basics to of the VB.NET-chrome relationship?
The specific problem I have is with sending mail through the website, I have no problem to add the relevant code, I just feel like I need to understand more before I start looking for bugs.
In the website there's an option to send an e-mail to a list of people. This option works in IE but doesn't work in firefox and chrome. I basically have a form tag which holds a table with a list of people with a check button next to every name. When you click send there's a function defined like this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sTo As String, sFrom As String, sSubject As String, sBody As String
Dim sCc As String, sBcc As String
Dim MyMail As MailMessage = New MailMessage
sFrom = "Laboratory Mail system sent from " & Session.Contents("UserNameEng") & " <dbsystem#mscc.huji.ac.il>"
sTo = Trim(Request.Form("EmailTo")) & txtTo.Text
sCc = Trim(Request.Form("EmailCc")) & txtCc.Text
sSubject = Trim(txtSubject.Text)
sBody = Trim(txtBody.Text)
sBody = sBody.Replace(vbCrLf, "<br />") 'new
MyMail.Headers.Add("Reply-To", Session.Contents("UserEmail"))
MyMail.From = sFrom
MyMail.To = sTo
MyMail.Subject = sSubject
MyMail.BodyFormat = MailFormat.Html 'new
'MyMail.Body = sBody
Select Case optDirection.SelectedValue.ToString
Case "BodyRtl"
MyMail.Body = "<style> body {direction: rtl} </style>" & sBody
Case "BodyLtr"
MyMail.Body = "<style> body {direction: ltr} </style>" & sBody
End Select
' MyMail.Body = "<style> body {direction: rtl} </style>" & sBody 'new
' MyMail.BodyEncoding = System.Text.Encoding.GetEncoding("iso-8859-8-i")
MyMail.Cc = sCc
If chkCopyForMe.Checked Then
MyMail.Bcc = Session.Contents("UserEmail")
End If
'MyMail.BodyFormat = MailFormat.Text
Select Case optPriority.SelectedValue.ToString
Case "Normal"
MyMail.Priority = MailPriority.Normal
Case "High"
MyMail.Priority = MailPriority.High
Case "Low"
MyMail.Priority = MailPriority.Low
End Select
If Trim(UploadFile.Value) <> vbNullString Then
Dim myAttachment As New MailAttachment(GetAttachment(Trim(UploadFile.Value))) '(Trim(UploadFile.Value))
MyMail.Attachments.Add(myAttachment)
End If
SmtpMail.SmtpServer = "pluto.mscc.huji.ac.il"
Try
SmtpMail.Send(MyMail)
Response.Redirect("SentMessage.aspx?m=1")
Catch ex As Exception
lblComment.Text = "Problem With Sending Mail<br />" & ex.Message
'Response.Redirect("SentMessage.aspx?m=2")
End Try
End Sub
which is suppose to send the mail to the selected boxes. In IE the mail arrives, in chrome it doesn't.
VB.NET is a server side technology - it outputs HTML to the browser.
The browser interacts with this HTML and may send responses to the server (clicking links, posting forms and such) - the VB.NET code can interpret these and respond with HTML.
This works the same way regardless of what browser (Chrome, IE, Firefox, Opera or any other).
This code has nothing to do with your problem. The browser never sees it, only the server.
You've made a mistake somewhere in your .aspx file: IE is guessing correctly what you really want, and Chrome is not.
What happens with Firefox?

How to put a hyperlink into the email body using vb.net

What im trying to do is add a hyperlink to the body of an email in vb.net. What im getting when i send the email is the link is text. Here is what I doing so far below. Any help would be very much appreciated.
'Accepts two parameters - the username and password for the email client
Dim credentials As New System.Net.NetworkCredential("test#test.net", "test")
smtpClient.Credentials = credentials
Dim body, link As String
link = "http://localhost:" & partUrl & "/test.aspx?autoNum=" & autoNum
body = "Test email." & "<br/><br/>"
body += link
Dim email As New MailMessage
email.From = New MailAddress("test#test.net")
email.Body = body
email.Subject = "test Change/Request Password"
email.To.Add(New MailAddress(toAddress))
smtpClient.Send(email)
You will need to enclose it in a tags.
link = "Click here"
And you need to set
email.IsBodyHtml = true
Try this:
link = "Link"
body = "Test email." & "<br/><br/>"
body += link
The idea (I can't test it now, sorry) is you have to add not the url itself, but the HTML code used to create link.
Remember to set mail body to html with email.IsBodyHtml = True
I believe you need to set
IsBodyHtml = True
Then you can use plain HTML in the body of the e-mail. It's still up to the mail client to display it correctly. I've had a few cases where valid HTML that looked create in my browser was messy in my e-mail.
You haven't identified the body as HTML.
Add:
email.IsBodyHtml = true

Create Individualized PDFs with VB.Net and Crystal Reports

I'd like to be able to create, name and store individualized reports (school report cards, actually) with VB.Net and Crystal Reports using data from our SQL database.
It would be even better to be able to automatically generate individualized e-mails using e-mail addresses stored in the database, attaching the aforementioned PDF reports and sending them off.
Has anyone attempted anything like this before?
TIA for any help/pointers!
1. Create PDF programatically
Depending on the version of crystal, the export function will look similar to this
Dim objApp As CRAXDRT.Application
Dim objRpt As CRAXDRT.Report
Dim Path As String = "MyReport.rpt"
objApp = new CRAXDRT.Application
objRpt = objApp.OpenReport(Path)
With objRpt
.ExportOptions.FormatType = crEFTPortableDocFormat
.ExportOptions.DestinationType = crEDTDiskFile
.ExportOptions.DiskFileName = "MyReport.PDF"
.ExportOptions.PDFExportAllPages = True
.Export( False )
End With
2. Send email with a PDF attachment
The "send" part will look like this:
Dim email As New MailMessage()
''//set the reply to address and the to address
email.To.Add(New MailAddress("student#domain.com", "Studen Name"))
email.ReplyTo = New MailAddress("youremail#domain.com", "Your name")
''//Assign the MailMessage's properties
email.Subject = "Your scorecard file"
email.Body = "Attached is the file you asked<br />Regards!"
email.IsBodyHtml = True
''//attach the file
email.Attachments.Add(New Attachment("c:\temp\myreport.pdf"))
Dim smtp As New SmtpClient
Try
smtp.Send(email)
Catch ex As Exception
messageBox("cant send")
End Try