pdf arrives damaged on when I send it via email - vb.net

I want to send a pdf file via email, the file arrives but it damaged, any idea why?
Dim buffer As Byte() = New Byte(6499) {}
Dim bytesRead As Integer = 0
bytesRead = file.FileByteStream.Read(buffer, 0, buffer.Length)
Dim ms As New MemoryStream(bytesRead)
ms.Seek(0, SeekOrigin.Begin)
mail.Attachments.Add(New Attachment(ms, "test.pdf", "application/pdf"))
mail.IsBodyHtml = False
mail.From = New System.Net.Mail.MailAddress("xxxxx#gmail.com")
mail.To.Add("yyyyyy#gmail.com")
mail.Subject = "test subject"
mail.Body = "test body"
mail.Priority = System.Net.Mail.MailPriority.Normal
Dim smtp As New System.Net.Mail.SmtpClient
smtp.Host = "smtp.gmail.com"
smtp.Credentials = New System.Net.NetworkCredential("mymail#gmail.com", "pass")
smtp.Port = 587
smtp.EnableSsl = True
smtp.Send(mail)enter code here

I'm not sure from your question if you're reading the attachment in from the file system, but try changing this line:
Dim ms As New MemoryStream(bytesRead)
to this:
Dim ms = File.OpenRead("my file path")
and discard everything above it.

Related

can't send Email from Webservice written in vb.net

I wanna send an Email from my Webservice, written in vb.net, but I get an Timeout.
What is wrong?
Dim toaddress As MailAddress = New MailAddress("xxx")
Dim fromaddress As MailAddress = New MailAddress("yyy")
' The structure for MailMessage(from, to)
Dim message As MailMessage = New MailMessage(fromaddress, toaddress)
message.Subject = "I have sent you a message from a program!"
message.Body = "Hello World!"
Dim messanger As SmtpClient = New SmtpClient("smtpxxx", 995)
messanger.Credentials = New NetworkCredential("user", "password")
messanger.EnableSsl = True
messanger.Send(message)
You are doing many things wrong and some of the information you have given are not clear enough. host name and port names have to give properly. it chooses which mail service you are using, from your code it is not correctly provided, so i will give a snippet that uses gmail to send mail. please go through this and make changes as per your SmtpHost.
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("email", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtemail.Text)
e_mail.To.Add(txtemail.Text)
e_mail.Subject = "Email Sending"
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
This link help you to find the smtp.Server and Port numbers, you can use this thread for check the limitations of smtp.servers

Email not send using Gmail smtp on host

I've used this code to sending emails. When I test this code in LOCALHOST, it works correctly, but in host, I get this error:
Failure sending mail.few
My code is:
Dim MailMsg As New MailMessage(New Net.Mail.MailAddress("MYUSERNAME#gmail.com"), New Net.Mail.MailAddress("myname#gmail.com"))
MailMsg.Subject = "test"
MailMsg.Body = "test"
MailMsg.IsBodyHtml = True
Dim SmtpMail As New Net.Mail.SmtpClient
Dim SMTPUserInfo As New Net.NetworkCredential()
SMTPUserInfo.UserName = "MYUSERNAME"
SMTPUserInfo.Password = "MYPASSWORD"
SmtpMail.UseDefaultCredentials = False
SmtpMail.Credentials = SMTPUserInfo
SmtpMail.Host = "smtp.gmail.com"
SmtpMail.Port = 587
SmtpMail.EnableSsl = True
SmtpMail.Send(MailMsg)
'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()
'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("USERNAME#gmail.com")
'To is a collection of types
MyMailMessage.To.Add("DESTINATION#EXAMPLE.COM")
MyMailMessage.Subject = "XXXXXXX" & x
MyMailMessage.Body = "XXXXXXXXXXXXXXXXXXX "
Dim attach As New Attachment(fileName)
MyMailMessage.Attachments.Add(attach)
'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("USERNAME#gmail.com", "PASSWD")
SMTPServer.EnableSsl = True
SMTPServer.Send(MyMailMessage)
SMTPServer.Dispose()
MyMailMessage.Dispose()

Use Progress bar for upload attachment

I have a xml file as an attachment. I searched on the internet all of them were for uploading by ftp I couldn't figure out how to use them for sending mail so how can I use progress bar to show while it is uploading to the mail?
here is my code for sending email
Dim smtpServer As New SmtpClient
Dim mail As New MailMessage()
Dim mailAttachment As Attachment = New Attachment("XMLFile")
smtpServer.UseDefaultCredentials = False
smtpServer.Credentials = New Net.NetworkCredential("a#ymail.com", "12345")
smtpServer.Port = 465
smtpServer.EnableSsl = True
smtpServer.Host = "smtp.mail.yahoo.com"
mail = New MailMessage()
mail.From = New MailAddress("a#ymail.com")
mail.To.Add("a#ymail.com")
mail.Subject = "110BackUp"
mail.IsBodyHtml = False
mail.Body = "XML FILE"
mail.Attachments.Add(mailAttachment)
smtpServer.Send(mail)
If the problem is only have the progress bar while sending the email maybe you can find useful this SO post

How to send Image as email attachment using Restful service

Below is the vb.net code that i'm using to receive an image from jquery ajax call..
Public Function MailKpiChart(ByVal img As String) As GenericResponse(Of Boolean) Implements IKPI.MailKpiChart
Dim SmtpServer As New Net.Mail.SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("xyz#gmail.com", "password")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Dim blStatus As Boolean
Try
Dim mailMessage As New MailMessage()
mailMessage.From = New MailAddress("from#gmail.com", ".Net Devoloper")
mailMessage.To.Add(New MailAddress("to#gmail.com"))
mailMessage.Subject = "test mail"
mailMessage.Body = "hello world"
Dim imgStream As New MemoryStream()
Dim Image As System.Drawing.Bitmap = getImagefromBase64(img)
Dim filename As String = "sample image"
Image.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png)
mailMessage.Attachments.Add(New Attachment(imgStream, filename, System.Net.Mime.MediaTypeNames.Image.Jpeg))
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.Send(mailMessage)
blStatus = True
Return ServiceUtility.BuildResponse(Of Boolean)(blStatus, String.Empty, String.Empty, 0)
Catch ex As Exception
Return ServiceUtility.BuildResponse(Of Boolean)(False, "",
ex.Message, AppConstants.ErrorSeverityCodes.HIGH)
End Try
End Function
and this is the function where i'm converting base64 string to an image
Public Function getImagefromBase64(ByVal uploadedImage As String) As Image
'get a temp image from bytes, instead of loading from disk
'data:image/gif;base64,
'this image is a single pixel (black)
Dim bytes As Byte() = Convert.FromBase64String(uploadedImage)
Dim image__1 As Image
Using ms As New MemoryStream(bytes)
ms.Write(bytes, 0, bytes.Length)
image__1 = Image.FromStream(ms)
End Using
Return image__1
End Function
and when i see my gmail inbox i c a mail with an attachment but i dont see the actual image which i've sent..i just c a blank image
Help Needed
Regards
I had the same issue and solved it by just adding the following line just before attaching the stream to the message.
imgStream.Position = 0
Regards,
Jonathan.
add your url - points to another web site
Dim bodys As String = "<html><head><title> example </title></head><body><table width='100%'>"
bodys += "<p><img width=135 height=77 src='http://www.example.com/image.gif'></p>"
bodys += "</table></body></html>"
Dim mails As New System.Net.Mail.MailMessage()
SmtpServer.Host = "127.0.0.1"
SmtpServer.Port = 25
mails = New System.Net.Mail.MailMessage()
mails.From = New System.Net.Mail.MailAddress("example#example.com")
mails.To.Add(txtEmail.Text)
mails.Subject = ""
mails.BodyEncoding = Encoding.UTF8
mails.IsBodyHtml = True
mails.Body = bodys.ToString()
SmtpServer.Send(mails)
A relative URL - points to a file within a web site
src="image.gif"
http://www.w3schools.com/tags/att_img_src.asp

Sending Mail in Vb.net using vb.net

Am using the below code to send a mail in vb.net, this code works fine with gmail but not works with rediffmail
Mail.Subject = "test email"
Mail.To.Add(dgr.Cells("to#xyz.com")
Mail.From = New MailAddress("from#abc.com")
Mail.Body = "Hello"
Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment("AttachPath")
Mail.Attachments.Add(attachment)
Dim SMTP As New SmtpClient("smtp.rediffmailpro")
SMTP.EnableSsl = false
SMTP.Credentials = New System.Net.NetworkCredential("xyz#.abc.com",
"password")
SMTP.Port = 25
SMTP.Send(Mail)
any reason? or solution, so that I can sent mail using any Email ID
Try
Dim SMTP As New SmtpClient("smtp.rediffmailpro.com")