email attachment issue in ASP.Net - asp.net

Hi I am trying to send a picture attachment to some email via ASP.NET. The program works without any problem, but I cannot visualize the attached picture in some email programs, such as iPhone's email program for Yahoo email. I suspect I am not attaching correctly the picture.
It works ok on my desktop pc
When the email is loaded I cannot see the picture.
Dim mail As New MailMessage()
mail.From = New MailAddress("xxxxxxxx", "xxxxxxx")
mail.To.Add(DirectCast(RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Email"), TextBox).Text)
mail.Subject = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
mail.Body = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Dim htmlBody As String = "<html><body><DIV style=""background-color:#5B37AE"">"
htmlBody += "<img height=""70px"" width=""150px"" src=cid:HDIImage /></DIV></body></html>"
Dim htmlView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, "text/html")
Dim imageResource As New System.Net.Mail.LinkedResource(Server.MapPath("~/Images/Site/background_main2.png"))
imageResource.ContentId = "HDIImage"
htmlView.LinkedResources.Add(imageResource)
mail.AlternateViews.Add(htmlView)
Dim j As New SmtpClient
j.Host = "hostingxxxxx"
j.EnableSsl = False
j.Credentials = New System.Net.NetworkCredential("usernam", "passw")
j.Send(mail)
Thanks

It was not necessary to attach via linkedresoruce, adding src="http://wwww.mysite.com/myimage.jpg" works ok in every (modern) browser

Related

Prevent LinkedResource image getting added as attachment

On a page that sends emails using System.Net.Mail I have this to embed an image into a html formatted email.
string logoPath = "W:\\WebSites\\logo.jpg";
LinkedResource imagelink = new LinkedResource(logoPath, "image/jpg");
imagelink.ContentId = "imageId";
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(MessageHeader + Message.ToString() + MessageFooter, null, "text/html");
htmlView.LinkedResources.Add(imagelink);
In the code that creates the email, the image is embedded like this.
MessageHeader += "<img alt=\"Company Logo\" hspace=0 src=\"cid:imageId\" align=baseline border=0>";
This all works okay. The logo appears in the html formatted email where it should be. But, when users receive the email there is also an attached image - in Outlook you see an attachment always called ATT00001 - which, if you download, is the logo image.
How can I prevent the logo getting added as an attachment as well as being embedded? It looks unprofessional to have the logo in the message but also attached waiting to be downloaded. Users complain they think there is an attachment - but in fact it is just the logo.
I figured out the solution for this question. When you add the LinkedResource object, you have to define the correct MIME type, and if you do so, the email client won't offer the embedded images as attachments.
Here is a sample code grabbed from my scenario (vb.net code):
'Your email
Dim _htmlSource As New StringBuilder("<html> ... YOUR HTML EMAIL ... </html>")
Dim _linkedResources As New List(Of LinkedResource)
Dim _href As String = "http://your.image.url/image.png"
'Adding images
Dim _imageStream As MemoryStream
Dim _mimeType As String
Using _wc = New WebClient
_imageStream = New MemoryStream(_wc.DownloadData(_href))
_mimeType = _wc.ResponseHeaders("content-type")
End Using
Dim _contentId As String = Guid.NewGuid.ToString
_htmlSource.Replace(_href, "cid:" & _contentId)
_linkedResources.Add(New LinkedResource(_imageStream, New ContentType(_mimeType)) With {.ContentId = _contentId})
'Compile mail message
Dim mail As New MailMessage
mail.IsBodyHtml = True
Dim _htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(_htmlSource.ToString(), Nothing, MediaTypeNames.Text.Html)
_htmlView.TransferEncoding = TransferEncoding.EightBit
_linkedResources.ForEach(Sub(lr) _htmlView.LinkedResources.Add(lr))

Sending e-mail using VB on ASP.NET through Gmail

I am trying to send an email from an ASP page using gmail, but when I try to send it, i just get "Error sending mail", with no description other than that.
SMTP is enabled on the gmail box I'm using and also working from mobile devices.
If anyone could give me a hand with this, I'll appreciate it.
Thanks.-
Friend Shared Sub Send_Mail(Destinatario As String)
Dim Message As New MailMessage()
Dim SmtpC = New SmtpClient("smtp.gmail.com")
Message.From = New MailAddress("xxxx#gmail.com")
Message.To.Add(Destinatario)
Message.Subject = "Some Subject"
Message.Body = "Message body"
SmtpC.Port = 465
SmtpC.Credentials = New System.Net.NetworkCredential("xxxx#gmail.com", "somepassword")
SmtpC.EnableSsl = True
Try
SmtpC.Send(Message)
Catch ex As Exception
End Try
End Sub

Sending to external email fail

Here is the codes:
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Dim attachment As System.Net.Mail.Attachment
SmtpServer.Credentials = New _
Net.NetworkCredential("administrator#company.com", "1234")
SmtpServer.Port = 25
SmtpServer.Host = "SmtpServer"
mail = New MailMessage()
mail.From = New MailAddress("user#company.com.my")
mail.To.Add("recipient#external.com")
mail.CC.Add("user1#company.com")
mail.CC.Add("user2#company.com")
mail.Headers.Add("Disposition-Notification-To", "user1#company.com") 'Read receipt
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure 'Fail delivery notification
mail.Subject = "Sending Documents"
mail.Body = "To Whom May Concern," & vbCrLf _
& vbCrLf & "Please refer to the attachment for the documents." & vbCrLf & _
"NOTE : This is an automatically generated email and will be sent daily."
For Each path As String In attch
attachment = New System.Net.Mail.Attachment(path)
mail.Attachments.Add(attachment)
Next
Try
SmtpServer.Send(mail)
SmtpServer = Nothing
Catch ex As Exception
Response.Write(ex.ToString)
Exit Sub
End Try
Problem is only internal email receiving, not external email.
No errors shown during the code execution.
Any idea on how to solve this?
Or do I need to configure something at the Microsoft Exchange Server?
Also the server using the MailMarshal to do the filtering.
An advanced thanks for the contributing feedback.
Make sure that "mail.From" value is in the list allowed by SMTP server. It could be that relaying is not allowed.

Email confirmation asp.net

This my working method to send a email
Sub emailConfirm()
Dim email As String = txtMail.Text
Dim name As String = txtPrenom.Text + txtNom.Text
Dim mail As MailMessage = New MailMessage()
'mail.To.Add("francois.smtp#gmail.com")
mail.To.Add(email)
mail.From = New MailAddress("sumerman#gmail.com.com")
mail.Subject = "Email using Gmail"
Dim Body As String = "Hi " + name + ", this mail is to confirm your registration" + "Click on the link to confirm please" + " Link(check note) "
mail.Body = Body
mail.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.Port = 587
smtp.Credentials = New System.Net.NetworkCredential("sumerman#gmail.com", "12345")
smtp.EnableSsl = True
smtp.Send(mail)
End Sub
note:I want to send Link contains a unique identifier and verify the unique identifier in the login.aspx
I don't know when to do this...
I found nothing interesting on the net ...
This is in Vb.net
I will add security later
Thank's for your helping!
Well, first off don't put the username and password in the email itself. That's a bit sloppy.
Second, most sites now do the following:
User creates account
Email sent to user. Link in email contains a unique identifier, not the username and password.
User clicks on link. At this point the page tests to see if the unique id is valid. If so, it tells them Thank You for confirming your account. It then displays the login form.
User enters their login credentials.
Note that the email did NOT send the actual username/pw info.

embedding image not displaying in emaill using vb.net

I am trying to display an embed an image within the body of an email. The is sent, however without the image.
Below is the code:
Dim mail As New MailMessage()
mail.[To].Add("siu07aj#reading.ac.uk")
mail.From = New MailAddress("atiqisthebest#hotmail.com")
mail.Subject = "Test Email"
Dim Body As String = "<b>Welcome to codedigest.com!!</b><br><BR>Online resource for .net articles.<BR><img alt="""" hspace=0 src=""cid:imageId"" align=baseline border=0 >"
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(Body, Nothing, "text/html")
Dim imagelink As New LinkedResource(Server.MapPath(".") & "\uploads\CIMG1443.JPG", "image/jpg")
imagelink.ContentId = "imageId"
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64
htmlView.LinkedResources.Add(imagelink)
mail.AlternateViews.Add(htmlView)
Dim smtp As New SmtpClient()
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
smtp.Host = ConfigurationManager.AppSettings("SMTP")
smtp.Port = 587
'smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential(ConfigurationManager.AppSettings("FROMEMAIL"), ConfigurationManager.AppSettings("FROMPWD"))
smtp.Send(mail)
In the body of the email only the following is display:
Welcome to CodeDigest.Com!!
Any idea how I can get the CIMG1443.JPG displaying?
Thanks
you could try to inline the image, by converting it to a base64-string with the following method:
Public Function ImageToBase64(image As Image, format As System.Drawing.Imaging.ImageFormat) As String
If image Is Nothing Then Return ""
Using ms As New MemoryStream()
' Convert Image to byte[]
image.Save(ms, format)
Dim imageBytes As Byte() = ms.ToArray()
' Convert byte[] to Base64 String
Dim base64String As String = Convert.ToBase64String(imageBytes)
Return base64String
End Using
End Function
Then you add the image to your HTML using something like this (where yourImage is an instance of the Image-class):
dim imageString = "<img src=""data:image/png;base64," + ImageToBase64(yourImage, ImageFormat.Png) + "="" />"
That way you would get around adding the image as resource. This worked for me in several places, even though I must admit that I haven't tryed it on hmlt emails.
Sascha

Resources