Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dim myString as String = "This String here should be send as Mail Attachment content (as a virtual File called HALLO.txt or without any extension)to the Mail Receiver without having to be saved on a file respectively on the HardDisk, since an attachment requieres a file from Harddisc or elsewhere"

VB
Dim SmtpServer As New SmtpClient()
           Dim mail As New MailMessage()
           SmtpServer.Credentials = New Net.NetworkCredential("JOHN.DOE@gmx.com", "PASSWORD")
           'SmtpServer.Port = 587
           SmtpServer.Host = "mail.gmx.com"
           mail = New MailMessage()
           mail.From = New MailAddress("John.Doe@gmx.com")
           mail.To.Add("to_someone@live.com")
           mail.Subject = "Dear Santa"
           mail.Body = "Whats up"
           Dim oAttch As Net.Mail.Attachment = New Net.Mail.Attachment(myString)
           mail.Attachments.Add(oAttch)
           SmtpServer.Send(mail)
           mail.Dispose()


This is a very complicated question and not really of great use but if you have any idea please reply because i need this solution, thanks in advance:
Posted
Updated 8-May-12 10:36am
v5

Try converting the string to a Stream:
VB
Dim myString as String = "This String here should be send as Mail Attachment as (HALLO.txt or without any extension) File to the Mail Receiver without having to be saved on a file respectively on the HardDisk"
Dim ms As New MemoryStream(System.Text.Encoding.ASCII.GetBytes(myString))
Dim ct As New ContentType(MediaTypeNames.Text.Plain)
Dim oAttch As Net.Mail.Attachment = New Net.Mail.Attachment(ms, ct)
mail.Attachments.Add(oAttch)


[edit]Forgot the ContentType - OrignalGriff[/edit]
 
Share this answer
 
v2
Comments
[no name] 7-May-12 17:01pm    
Dude you have forgotten something because i get an exception saying;
"Value of Type 'System.IO.Memory.Stream' cannot be converted to String"
[no name] 8-May-12 8:50am    
Hi again. Your code works but the string wasnt sent as Attachment, it was sent as Mail Body. If you come up with something new please let me know, sorry for the inconveinence, Begi
Maciej Los 8-May-12 10:14am    
You need to read carefully the answer. OriginalGriff wrote: "File to mail (...)" should be saved and get from "HardDisk".
Save data into txt file and use something like this:
Dim oAttch As Net.Mail.Attachment = New Net.Mail.Attachment(PathToFile, ContentType)
More at MSDN: Attachment Contructor, ContentType with examples
[no name] 8-May-12 11:25am    
I already know how to send a mail containing an attachment, but my question was how to make for example a string "hello world" to send as attachment meant as virtual File, because i dont want always to write some string to "c:\myAttachment.txt". I want directly to send my string as mail attachment, on which the user(receiver), will be able to download that attachment as a file, containing my string on it understand?
thanks in advance, Begi
Maciej Los 8-May-12 12:15pm    
Yes, now i do understand you ;)
While researching i found out from a website what i was searching for!
I want to thank 'OriginalGriff' and 'losmac' for their effort.
Here below is the website where i obtained some code, alright.
http://msdn.microsoft.com/en-us/library/system.net.mime.contentdisposition.aspx#Y0[^]

VB
Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New Net.NetworkCredential("JOHN.DOE@gmx.com", "PASSWORD")
            'SmtpServer.Port = 587
            SmtpServer.Host = "mail.gmx.com"
            mail = New MailMessage()
            mail.From = New MailAddress("John.Doe@gmx.com")
            mail.To.Add("to_someone@live.com")
            mail.Subject = "Dear Santa"
            mail.Body = "Whats up

VB
Dim myString As String = "This String here as content(body) to the new Attachment(not to the Mail Body)"
        Dim ms As New MemoryStream(System.Text.Encoding.ASCII.GetBytes(myString))
        Dim oAttch As Net.Mail.Attachment = New Net.Mail.Attachment(ms, MediaTypeNames.Text.Plain)


VB
'Here is the magic;

VB
Dim disposition As ContentDisposition = oAttch.ContentDisposition
        disposition.FileName = "MyPersonalAttachment.txt" 'Sets this name for the new attachment as a new file containing myString on it
        mail.Attachments.Add(oAttch)
        SmtpServer.Send(mail)
        mail.Dispose()
 
Share this answer
 
v4
Comments
Maciej Los 10-May-12 2:07am    
Great Begi21, 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900