VB.NET邮件群发纯htlm二维码

前端之家收集整理的这篇文章主要介绍了VB.NET邮件群发纯htlm二维码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

发送邮件采用了frame的System.Net.Mail

Dim msg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
msg.To.Add(mailToStr)
msg.From = New MailAddress("liuc@qq.com","Liuc",System.Text.Encoding.UTF8)
msg.Subject = "报名确认函"
msg.SubjectEncoding = System.Text.Encoding.UTF8
msg.Body = "报名确认函"
msg.BodyEncoding = System.Text.Encoding.UTF8
msg.IsBodyHtml = True '是否是HTML邮件    
msg.Priority = MailPriority.High
Dim View As AlternateView = AlternateView.CreateAlternateViewFromString(body,System.Text.Encoding.UTF8,"text/html")

'在html中插入本地图片
Dim body As String = ""
body = body + "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN' >"
body = body + "<HTML><HEAD><Meta http-equiv=Content-Type content='text/html; charset=utf-8'></HEAD><BODY>"
body = body + "<DIV><img src='cid:qrCode'></DIV>"
body = body + "</BODY></HTML>"
Dim View As AlternateView = AlternateView.CreateAlternateViewFromString(body,"text/html")

'Dim MS As MemoryStream = New MemoryStream()
'MS = bitmapToGifMemoryStream(img)
Dim imgPathStr As String = "D:\123456.bmp"

Dim lrImage As LinkedResource = New LinkedResource(imgPathStr,"image/gif")
lrImage.ContentId = "qrCode"
View.LinkedResources.Add(lrImage)
msg.AlternateViews.Add(View)
'上传本地图片为附件
msg.Attachments.Add(New Attachment(imgPathStr,"image/gif"))

Dim client As SmtpClient = New SmtpClient()
client.UseDefaultCredentials = True
client.Credentials = New System.Net.NetworkCredential("liuc@qq.com","mima123")
'SMTP服务器和端口号
'因为本实例中用来发送邮件的是qq企业邮箱,所以接受服务器用的是smtp.exmail.qq.com
client.Host = "smtp.exmail.qq.com"
client.Port = 25
Try
	client.Send(msg)
	MessageBox.Show("发送成功")
Catch ex As Exception
	MessageBox.Show(ex.Message,"发送邮件异常")
End Try

以上代码实现了带图片及附件的邮件发送。

但是实际应用中发现,QQ邮箱为了保证安全,他的默认配置是读取纯文本格式的邮件内容中的图片未被显示,需要在邮件中点击【显示图片】或者【信任发件人的图片】,邮件内容中的图片才会显示出来。

因为我发送邮件的场景是想发送一个带二维码邮件,如果图片无法显示,则达不到我想要的效果

因此我考虑,是否可以用<table>标签,拼出一个二维码来,这样就可以解决QQ邮箱无法显示图片的问题了。


生成二维码我采用的是Google的ZXing

生成二维码选用的是QR码。Qr码有40种版本,也就是大小从21*21到177*177。

我要生成二维码二维码格式基本都是byx123456。经测试,发现生成二维码的size是25*25.

所以我要先生成一个25*25的二维码。每一个像素都表示一个有效的信息点。

'生成二维码
Dim Writer As BarcodeWriter = New BarcodeWriter()
Writer.Format = BarcodeFormat.QR_CODE
Dim options As ZXing.QrCode.QrCodeEncodingOptions = New ZXing.QrCode.QrCodeEncodingOptions()
'禁用ECI编码段: use UTF-8 encoding and the ECI segment is omitted
options.DisableECI = True
'设置内容编码
options.CharacterSet = "UTF-8"
'设置二维码的宽度和高度
options.Width = 25
options.Height = 25
'设置二维码的边距,单位不是固定像素
options.Margin = 1
options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H
Writer.Options = options
Dim Map As Bitmap = Writer.Write("byx123456")
Map.Save("D:\byx123456.bmp")




生成二维码如上图

想要将二维码转化为纯html格式的,我只需遍历这个图片,根据像素点的颜色,设置<td></td>标签的背景色即可。

遍历图片我用的是EmguCV。当然啦,其实根本没必要用Emgu。

'二维码转为纯html
Dim emguImg As Image(Of Gray,Byte) = New Image(Of Gray,Byte)("D:\byx123456.bmp")
Dim qrCodeHtmlStr As String = ""
qrCodeHtmlStr = qrCodeHtmlStr + "<tr>"
qrCodeHtmlStr = qrCodeHtmlStr + "<td><table style='border-collapse:collapse;'>"
For i = 0 To emguImg.Width - 1
	qrCodeHtmlStr = qrCodeHtmlStr + "<tr>"
	For j = 0 To emguImg.Height - 1
		If emguImg.Data(i,j,0) = 0 Then
			qrCodeHtmlStr = qrCodeHtmlStr + "<td width='13' height='13' border='0' padding='0' style='background-color:black;padding:0px;'></td>"
		Else
			qrCodeHtmlStr = qrCodeHtmlStr + "<td width='13' height='13' border='0' padding='0' style='padding:0px;'></td>"
		End If
	Next
	qrCodeHtmlStr = qrCodeHtmlStr + "</tr>"
Next
qrCodeHtmlStr = qrCodeHtmlStr + "</table></td></tr>"        
body = body + qrCodeHtmlStr



再将生成的html插入到邮件中,就可以得到一个QQ邮箱也可以查看的纯html的二维码 原文链接:https://www.f2er.com/vb/256769.html

猜你在找的VB相关文章