VB表单在文本文件中发送带有表单字段的电子邮件作为附件

时间:2013-01-17 16:36:39

标签: .net vb.net winforms email email-attachments

我是VB形式的新手,但如果可能的话需要一些建议。我想通过电子邮件将表单字段作为一行管道分隔的文本文件附件。因此,当您打开发送的电子邮件时,它将附加* .txt文件,第一行将如下所示

textbox1_value|textbox2_value|textbox3_value

这是我的电子邮件代码:

Imports System.Net.Mail

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Using message As New MailMessage()
            'set to the from, to and subject fields
            message.From = New MailAddress(TextBox2.Text.ToString())
            message.[To].Add(New MailAddress("steve@steve.com"))
            message.Subject = "Certificate alert"
            'code the message body
            Dim MsgBody As String
            MsgBody = TextBox2.Text.ToString() & vbCr & _
                      TextBox3.Text.ToString() & vbCr & _
                      TextBox4.Text.ToString()
            message.Body = MsgBody
            Dim client As New SmtpClient()
            client.Host = "mailhost"
            client.Send(message)
        End Using
        'display submitted box
        MessageBox.Show("Your Incident has been submitted!", "Congratulations!")
        'close form
        Me.Close()
    End Sub
End Class

非常感谢任何想法

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以将附件中的文本保存到文本文件中,然后:

message.Attachments.Add(New Attachment("filename"))
system.io.file.delete("filename") ' delete the file

答案 1 :(得分:0)

在磁盘上创建文件

Dim filename as String = "C:\Somefile.txt"
My.Computer.FileSystem.WriteAllText(filename, "value1|value2|value3")

然后附加到您的邮件消息:

Dim att as New System.Net.Mail.Attachment(filename)
message.Attachments.Add(att)

不要忘记之后删除你的文件