Добрый день.
При отправке email через TcpClient столкнулся со следующей проблемой:
Если через TcpClient - NetworkStream посылать строку длиннее, чем 1023 символа, почта просто не доходит. Если меньше сообщения проходят успешно.
Подскажите, как отправить по почте содержимое текстового файла, в котором, например 5000 символов.
'http://abstractvb.com/code.asp?A=1085
Imports System.IO
Imports System.Net.Sockets
Imports System.Text
Module SocketMail
Dim ns As NetworkStream
Dim client As TcpClient
Public Function SendMail(ByVal sFrom As String, ByVal sTo As String, ByVal sFilePath As String, ByVal sServer As String, ByVal iMaxLength As Integer, ByRef iWasSend As Integer) As Boolean
Dim sB As New StringBuilder
Dim iPort As Integer = 25
Dim strMessage As String
Dim sSubject As String = "Testing message"
Try
'Open
client = New TcpClient
client.Connect(sServer, iPort)
ns = client.GetStream()
strMessage = "HELO TEST" & ControlChars.CrLf
Call SendCommand(aStr:=strMessage)
sB.Length = 0
sB.Append("MAIL FROM:" & sFrom & ControlChars.CrLf)
sB.Append("RCPT TO:" & sTo & ControlChars.CrLf)
sB.Append("DATA" & ControlChars.CrLf)
sB.Append("date:" & ControlChars.CrLf) '_date.ToString & ControlChars.CrLf)
sB.Append("from:" & sFrom & ControlChars.CrLf)
sB.Append("to:" & sTo & ControlChars.CrLf)
sB.Append("cc:" & ControlChars.CrLf)
sB.Append("bcc:" & ControlChars.CrLf)
sB.Append("subject:" & sSubject & ControlChars.CrLf)
'текст письма
Dim sRd As StreamReader
Try
sRd = New StreamReader(sFilePath, Encoding.GetEncoding(866))
While Not (sRd.Peek = -1)
sB.Append(sRd.ReadLine & ControlChars.CrLf)
End While '(sRd.Peek = -1)
Catch ex As Exception
Throw New Exception("Ошибка при отправке тела письма" & ControlChars.CrLf & ex.ToString)
Finally
sRd.Close()
End Try
'ограничим тело письма размером iMaxLength
'(чтобы определить максимально допустимый размер)
If sB.Length > iMaxLength Then
sB.Length = iMaxLength
sB.Append(ControlChars.CrLf)
End If
sB.Append("." & ControlChars.CrLf)
iWasSend = sB.Length 'передать обратно к-во отправленных символов
Call SendCommand(aStr:=sB.ToString)
strMessage = "QUIT" & ControlChars.CrLf
Call SendCommand(aStr:=strMessage)
Catch ex As Exception
Throw New Exception("Ошибка при отправке тела письма" & ControlChars.CrLf & ex.ToString)
Return False
Finally
client.Close()
client = Nothing
ns = Nothing
End Try
Return True
End Function
Private Sub SendCommand(ByVal aStr As String)
Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(aStr)
ns.Write(sendBytes, 0, sendBytes.Length)
End Sub
End Module
Вызываю отправку файла по почте:
Private Sub btnSendMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendMail.Click
Dim sAddr() As String = {"krasovskiy@gmail.com", "salve0@marm.com.ua", "textbox@inbox.ru"}
Dim sSMTP() As String = {"smtp.gmail.com", "marm.com.ua", "smtp.inbox.ru"}
Dim iAddr As Integer = 1
Dim sFrom As String = sAddr(iAddr)
Dim sTo As String = sAddr(iAddr)
Dim sFilePath As String = "D:\AK\AK\Projects\Test\OnlyTest\OnlyTest_Jun_NET\Data\Z5018.TXT"
Dim sServer As String = sSMTP(iAddr)
Dim iMaxLength As Integer
Dim iWasSend As Integer
Try
iMaxLength = CInt(txtBoxMaxSize.Text)
Catch ex As Exception
iMaxLength = 3000
End Try
Call SendMail(sFrom:=sFrom, sTo:=sTo, sFilePath:=sFilePath, sServer:=sServer, iMaxLength:=iMaxLength, iWasSend:=iWasSend)
MsgBox(iWasSend.ToString)
End Sub
Ответить
|