电子邮件正文vb.net的文本文件内容

时间:2014-05-24 07:31:13

标签: vb.net email

我想复制文本文件的所有内容并将其粘贴到电子邮件正文中。我使用下面的代码,但身体不包含任何东西。有人可以帮我吗?

    Dim lines As List(Of String) = File.ReadAllLines("C:\sendD.txt").ToList
    Dim sender As String = "sender@yahoo.com"
    Dim recipient As String = "receiver@yahoo.com"
    Dim emailSubject = "Test!"
    Dim oMail As New SmtpMail("TryIt")
    Dim oSmtp As New SmtpClient()

    oMail.From = sender
    oMail.To = recipient
    oMail.Subject = emailSubject
    For Each line In lines
        oMail.TextBody = line
    Next

1 个答案:

答案 0 :(得分:0)

Using inputReader As New IO.StreamReader("C:\sendD.txt")
    oMail.Body = inputReader.ReadToEnd
End Using

For Each line In lines
    oMail.Body = oMail.Body & VbCrLf & line
Next

或使用StringBuilder,但这都需要iList的问题。没有必要使用StreamReader。

或者,使用您的示例(删除For Each循环):

Dim lines As String = File.ReadAllText("C:\sendD.txt")
oMail.Body = lines

另外,您应该使用System.Net.Mail,而不是.Web.Mail

请参阅:http://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail%28v=vs.110%29.aspx

和:http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient%28v=vs.110%29.aspx

另外,我找不到这个'.TextBody'属性,我想你的意思是.Body