使String()就像要发送的真实文件一样(作为Net.Mail.Attachment) [英] Make String() acting like a real File to be sent (as Net.Mail.Attachment)

查看:60
本文介绍了使String()就像要发送的真实文件一样(作为Net.Mail.Attachment)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim myString as String =此字符串应作为邮件附件的内容(作为称为HALLO.txt的虚拟文件或没有任何扩展名)发送到邮件接收器,而不必分别保存在硬盘上的文件中,因为附件需要来自Harddisc或其他地方的文件"

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"

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:

推荐答案

尝试转换字符串到流:
Try converting the string to a Stream:
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]忘记了ContentType-OrignalGriff [/edit]



[edit]Forgot the ContentType - OrignalGriff[/edit]


在进行调查时,我从网站上发现了我要搜索的内容!
我要感谢"OriginalGriff"和"losmac"的努力.
下面是我在其中获得一些代码的网站.
http://msdn.microsoft.com/en-us/library/system.net.mime.contentdisposition.aspx#Y0 [ ^ ]

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[^]

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 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)





'Here is the magic;



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()


这篇关于使String()就像要发送的真实文件一样(作为Net.Mail.Attachment)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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