如何在下面的VB .NET代码中删除图像附件 [英] How to remove image attachement in the below VB .NET code

查看:65
本文介绍了如何在下面的VB .NET代码中删除图像附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我搜索过网页,发送包含邮件正文中嵌入图片的邮件。

我找到了下面的代码,我已经整合了我的窗体应用程序在vb .net中,它与我的应用程序运行良好。



但问题是附加图像并且它在身体中显示为嵌入图像。



我不希望图像显示为附件。



任何人都可以帮助我修改下面的代码。



我尝试过:



Hi,
I have searched web for sending the mail with image embedded in the mail body.
I found the below code and i have integrated with my windows forms application in vb .net and it is working well with my application.

But the problem is it is attaching the image and it is displaying as embedded image in the body.

I don't want the image to be displayed as attachment.

can anyone help me to modify the below code.

What I have tried:

Public Sub SendEmail()
 
        'CREATE MAIL MESSAGE
        Using myMailMessage As New MailMessage
 
            myMailMessage.To.Add("recipent@domain.com")
            myMailMessage.From = New MailAddress("me@mydomain.com")
            myMailMessage.Subject = "This is the email subject"
            myMailMessage.Body = "This is the default text body"
            myMailMessage.IsBodyHtml = True 'THIS WILL MAKE THE MESSAGE USE THE ALT HTML VIEW
 
            'CREATE ALT HTML BODY THAT WILL INCLUDE EMBEDDED IMAGE
            'NOTE THE IMG SRC IS CID:ThePictureID
            Dim myMailHTMLBody = "<html><head></head><body>This is a test and should include a picture: <img src=cid:ThePictureID></body></html>"
 
            'BYTES ARRAY OF IMAGE SO WE CAN PUT IN MEMORY STREAM
            Dim myImageData() As Byte = Nothing
 
            'GRAB IMAGE FROM FILE AND PUT IN MEMORY STREAM
            Using myImage = Image.FromFile("C:\directory\image.jpg")
                Dim IC As New ImageConverter
                myImageData = DirectCast(IC.ConvertTo(myImage, GetType(Byte())), Byte())
            End Using
 
            Using myStream As New MemoryStream(myImageData)
                'CREATE ALT VIEW
                Dim myAltView As AlternateView = AlternateView.CreateAlternateViewFromString(myMailHTMLBody, New System.Net.Mime.ContentType("text/html"))
                'CREATE LINKED RESOURCE FOR ALT VIEW
                Dim myLinkedResouce = New LinkedResource(myStream, "image/jpeg")
                'SET CONTENTID SO HTML CAN REFERENCE CORRECTLY
                myLinkedResouce.ContentId = "ThePictureID" 'this must match in the HTML of the message body
 
                'ADD LINKED RESOURCE TO ALT VIEW, AND ADD ALT VIEW TO MESSAGE
                myAltView.LinkedResources.Add(myLinkedResouce)
                myMailMessage.AlternateViews.Add(myAltView)
 
                'SEND EMAIL
                Using mySMTP As New SmtpClient
                    mySMTP.Host = "smtp.yourdomain.com"
                    mySMTP.Credentials = New System.Net.NetworkCredential("you@domain.com", "password")
                    mySMTP.Send(myMailMessage)
                End Using
 
            End Using
        End Using
    End Sub

推荐答案

如果您不想在HTML中显示图像,为什么要包括电子邮件正文中的img标签显示它?

If you didn't want the image displayed in the HTML, why did you include an img tag in the email body to show it?
<img src=cid:ThePictureID>


这篇关于如何在下面的VB .NET代码中删除图像附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆