电子邮件文件附件和VB.net [英] Email file attachments and VB.net

查看:122
本文介绍了电子邮件文件附件和VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码将文档成功附加到电子邮件并将其发送给最终用户,有两个问题:



1.如果没有文件存在对于用户,我怎么能跳过它,所以寻找关于如何查询有问题的表的想法,如果没有条目则跳到发送电子邮件。



2.如何附加多个文档?我是否需要将表格读入数组?



检索(文档)
Dim fileName 作为 字符串 = 字符串 .Empty
Dim con As SqlConnection(ConfigurationManager.ConnectionStrings( TESTConnectionString)。ConnectionCtring)
Dim cmd 作为 SqlCommand( SELECT DocName,DocData FROM Docs WHERE Ext_Ref =& strID,con)
con .Open()
Dim dReader As SqlDataReader = cmd.Exec uteReader()

while dReader.Read()
fileName = dReader( DocName)。ToString()
Dim documentBinary 作为 字节()= DirectCast (dReader( DocData),字节())
Dim fStream As FileStream(服务器) .MapPath( 〜\Docs)& \& fileName,FileMode.Create)
fStream.Write(documentBinary, 0 ,documentBinary.Length)
fStream.Close()
fStream .Dispose()
结束 while
con.Close()
mail.Attachments.Add(附件( 〜 Docs \& fileName))

解决方案

在循环内部,只需编写要附加的代码,如下所示。 ..

  while  dReader.Read()
fileName = dReader( DocName)。ToString()
Dim documentBinary 作为 字节()= DirectCast (d Reader( DocData), Byte ())
Dim fStream As New FileStream(Server.MapPath( 〜\Docs)& \& fileName,FileMode.Create)
fStream.Write(documentBinary, 0 ,documentBinary.Length)
fStream.Close()
fStream .Dispose()

' 如果文件存在则附加。
If !String.IsNullOrEmpty(fileName)然后
mail.Attachments.Add(< span class =code-keyword>新附件( ~Docs \ & fileName))
结束



所以,



  1. 这里首先检查FileName是否为空或空白。
  2. 编写代码将文件附加到循环中。因此,所有文件将逐个附加。


检查..

http://social。 msdn.microsoft.com/Forums/en-US/9a0e883c-1e22-4206-ad22-a753f6401e61/attach-multiple-files-in-a-directory-to-an-email [ ^ ]

http://www.aspsnippets.com/Articles/Upload-and-attach-multiple-files-as-attachments-to-email-in-ASPNet.aspx [ ^ ]


尝试
Dim SmtpClient 作为 SmtpClient( smtp .google.com' smtp服务器地址
Dim MailAddressCollection As MailAddressCollection()
< span class =code-keyword> Dim 消息作为 MailMessage()
message.From = MailAddress( test @ test.com 测试电子邮件
message.Body = 在此处键入主体
message.Subject = 在此处输入主题
message。[ To ]。添加( tested@golf.com 收件人电子邮件
< span class =code-comment>' Dim att As New Attachment(New MemoryStream(bytes),name)
Dim 数据作为 附件( C:\ Aututose Extracts \document& _
DateTime.Today.ToString( yyyyMMdd)& 。zip
message.Attachments.Add(data)
SmtpClient .Port = 25
' SmtpClient.EnableSsl = True
SmtpClient.Send(message)
Catch ex As 例外

结束 尝试


I have the following code which sucessfully attaches a document to an email and fires it to an end user, got two issues:

1. If no documents exist for the user, how can I skip it, so looking for ideas on how I can look up the table in question, if there are no entries then skip to just sending the email.

2. How do I attach multiple docs ? Do I need to read the table into an array ?

Retrieve(Document)
        Dim fileName As String = String.Empty
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("TESTConnectionString").ConnectionString)
        Dim cmd As New SqlCommand("SELECT DocName,DocData FROM Docs WHERE Ext_Ref = " & strID, con)
        con.Open()
        Dim dReader As SqlDataReader = cmd.ExecuteReader()

        While dReader.Read()
            fileName = dReader("DocName").ToString()
            Dim documentBinary As Byte() = DirectCast(dReader("DocData"), Byte())
            Dim fStream As New FileStream(Server.MapPath("~\Docs") & "\" & fileName, FileMode.Create)
            fStream.Write(documentBinary, 0, documentBinary.Length)
            fStream.Close()
            fStream.Dispose()
        End While
        con.Close()
        mail.Attachments.Add(New Attachment("~Docs\" & fileName))

解决方案

Inside the loop, just write the code to attach like below...

While dReader.Read()
    fileName = dReader("DocName").ToString()
    Dim documentBinary As Byte() = DirectCast(dReader("DocData"), Byte())
    Dim fStream As New FileStream(Server.MapPath("~\Docs") & "\" & fileName, FileMode.Create)
    fStream.Write(documentBinary, 0, documentBinary.Length)
    fStream.Close()
    fStream.Dispose()

    ' If the file exists then attach.
    If !String.IsNullOrEmpty(fileName) Then 
        mail.Attachments.Add(New Attachment("~Docs\" & fileName))
End While


So,

  1. Here you first check whether FileName is not null or blank.
  2. Written the code to attach the file in the loop. So, all the files will be attached one by one.


Check..
http://social.msdn.microsoft.com/Forums/en-US/9a0e883c-1e22-4206-ad22-a753f6401e61/attach-multiple-files-in-a-directory-to-an-email[^]
http://www.aspsnippets.com/Articles/Upload-and-attach-multiple-files-as-attachments-to-email-in-ASPNet.aspx[^]


Try
    Dim SmtpClient As New SmtpClient("smtp.google.com") 'smtp server address
    Dim MailAddressCollection As New MailAddressCollection()
    Dim message As New MailMessage()
    message.From = New MailAddress("test@test.com", "test emailer")
    message.Body = "type body here"
    message.Subject = "type subject here"
    message.[To].Add("tested@golf.com") 'recipients email
    '    Dim att As New Attachment(New MemoryStream(bytes), name)
    Dim data As New Attachment("C:\Automated Extracts\document" & _
         DateTime.Today.ToString("yyyyMMdd") & ".zip")
    message.Attachments.Add(data)
    SmtpClient.Port = 25
    'SmtpClient.EnableSsl = True
    SmtpClient.Send(message)
Catch ex As Exception

End Try


这篇关于电子邮件文件附件和VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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