本文介绍了使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("[email protected]", "PASSWORD")
           'SmtpServer.Port = 587
           SmtpServer.Host = "mail.gmx.com"
           mail = New MailMessage()
           mail.From = New MailAddress("[email protected]")
           mail.To.Add("[email protected]")
           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:

推荐答案

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]


Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "PASSWORD")
            'SmtpServer.Port = 587
            SmtpServer.Host = "mail.gmx.com"
            mail = New MailMessage()
            mail.From = New MailAddress("[email protected]")
            mail.To.Add("[email protected]")
            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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:05