Другие языки программирования и технологии
Как программно отправить письмо на E-mail с помощью Visual Basic?
Как программно отправить письмо на E-mail с помощью Visual Basic зная порт, адрес SMTP сервера? Можно ли использовать не существующий mail отправителя?
Примерно так
Sub send_mail_CDO()
Dim MSG As New CDO.Message
MSG.to = “user@corp.net” ‘-> адрес получателя
MSG.From = “boss@corp.net” ‘-> от кого
MSG.Subject = “С днём рождения” ‘-> Тема
MSG.textbody = “С днём рождения, желаю счастья и здоровья” ‘->тело письма
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2 ‘->тип отправки, в данном случае SMTP
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = “smtp.corp.net” ‘->Адрес SMTP Сервера
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = “1″ ‘->Способ аунтификации (0 – нет, 1 – простой, 2 – NTLM аунтификация)
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendusername”) = “boss” ‘->имя пользователя
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “123qwe” ‘->пароль пользователя
MSG.Configuration.Fields.Item(“urn:schemas:mailheader:content-language”) = “windows-1251″ ‘->кодировка
MSG.Send ‘-> команда отправить
End Sub
E-mail отправителя должен существовать.
Sub send_mail_CDO()
Dim MSG As New CDO.Message
MSG.to = “user@corp.net” ‘-> адрес получателя
MSG.From = “boss@corp.net” ‘-> от кого
MSG.Subject = “С днём рождения” ‘-> Тема
MSG.textbody = “С днём рождения, желаю счастья и здоровья” ‘->тело письма
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2 ‘->тип отправки, в данном случае SMTP
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = “smtp.corp.net” ‘->Адрес SMTP Сервера
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = “1″ ‘->Способ аунтификации (0 – нет, 1 – простой, 2 – NTLM аунтификация)
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendusername”) = “boss” ‘->имя пользователя
MSG.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “123qwe” ‘->пароль пользователя
MSG.Configuration.Fields.Item(“urn:schemas:mailheader:content-language”) = “windows-1251″ ‘->кодировка
MSG.Send ‘-> команда отправить
End Sub
E-mail отправителя должен существовать.
У Visual Basic 6.0 есть элемент WinSock, вот документация по протоколу SMTP:
http://ru.wikipedia.org/wiki/SMTP
VB.NET:
Смотрите также тут http : // www . emailarchitect . net / easendmail / kb / vb.aspx (уберите пробелы)
И тут http : // www . codeproject . com / Articles / 2987 / Send-SMTP-mail-using-VB-NET (уберите пробелы)
Imports System.Web.Mail
Sub Main()
Dim strMSG As String
Dim strArgs() As String = Command.Split(",")
Dim blnSMTP As Boolean = False
Dim blnCC As Boolean = False
Dim blnAttachments As Boolean = False
'get the product name, version and description from the assembly
strMSG = vbCrLf + vbCrLf + _
System.Diagnostics.FileVersionInfo.GetVersionInfo( _
System.Reflection.Assembly.GetExecutingAssembly.Location).ProductName _
+ " v" + System.Diagnostics.FileVersionInfo.GetVersionInfo( _
System.Reflection.Assembly.GetExecutingAssembly.Location _
).ProductVersion + vbCrLf + _
System.Diagnostics.FileVersionInfo.GetVersionInfo( _
System.Reflection.Assembly.GetExecutingAssembly.Location _
).Comments + vbCrLf + vbCrLf
If UBound(strArgs) < 3 Then
strMSG = strMSG + "Usage: EPSendMail from@email.com, " + _
"to@email.com, subject, message, [smtp Server]," + _
"[cc1@email.com;cc2@email.com;...], [attachment1;" + _
"attachment2;...]" + vbCrLf
Console.Write(strMSG)
Exit Sub
End If
strMSG = strMSG + "Sending email message" + vbCrLf + _
" From --> " + Trim(strArgs(0)) + vbCrLf + _
" To --> " + Trim(strArgs(1)) + vbCrLf + _
" Subject --> " + Trim(strArgs(2)) + vbCrLf + _
" Message --> " + Trim(strArgs(3)) + vbCrLf
If UBound(strArgs) >= 4 Then
If Len(Trim(strArgs(4))) > 0 Then
blnSMTP = True
strMSG = strMSG + " SMTP Server --> " + Trim(strArgs(4)) + _
vbCrLf
End If
End If
If UBound(strArgs) >= 5 Then
If Len(Trim(strArgs(5))) > 0 Then
blnCC = True
strMSG = strMSG + " CC --> " + Trim(strArgs(5)) + _
vbCrLf
End If
End If
If UBound(strArgs) >= 6 Then
If Len(Trim(strArgs(6))) > 0 Then
blnAttachments = True
strMSG = strMSG + " Attachments --> " + Trim(strArgs(6)) + _
vbCrLf
End If
End If
Console.Write(strMSG)
'send the email
Try
Dim insMail As New MailMessage()
With insMail
.From = Trim(strArgs(0))
.To = Trim(strArgs(1))
.Subject = Trim(strArgs(2))
.Body = Trim(strArgs(3))
If blnCC Then .Cc = Trim(strArgs(5))
If blnAttachments Then
Dim strFile As String
Dim strAttach() As String = Split(strArgs(6), ";")
For Each strFile In strAttach
.Attachments.Add(New MailAttachment(Trim(strFile)))
Next
End If
End With
If blnSMTP Then SmtpMail.SmtpServer = Trim(strArgs(4))
SmtpMail.Send(insMail)
Console.WriteLine("Successfully sent email message" + vbCrLf)
Catch err As Exception
Console.WriteLine("EXCEPTION " + err.Message + vbCrLf)
End Try
End Sub
P.S. Я бы на вашем месте VB6 похоронил, и перешел бы на VB.NET или на C#
Потому-что ваше приложение небудит работать на Windows 8!
http://ru.wikipedia.org/wiki/SMTP
VB.NET:
Смотрите также тут http : // www . emailarchitect . net / easendmail / kb / vb.aspx (уберите пробелы)
И тут http : // www . codeproject . com / Articles / 2987 / Send-SMTP-mail-using-VB-NET (уберите пробелы)
Imports System.Web.Mail
Sub Main()
Dim strMSG As String
Dim strArgs() As String = Command.Split(",")
Dim blnSMTP As Boolean = False
Dim blnCC As Boolean = False
Dim blnAttachments As Boolean = False
'get the product name, version and description from the assembly
strMSG = vbCrLf + vbCrLf + _
System.Diagnostics.FileVersionInfo.GetVersionInfo( _
System.Reflection.Assembly.GetExecutingAssembly.Location).ProductName _
+ " v" + System.Diagnostics.FileVersionInfo.GetVersionInfo( _
System.Reflection.Assembly.GetExecutingAssembly.Location _
).ProductVersion + vbCrLf + _
System.Diagnostics.FileVersionInfo.GetVersionInfo( _
System.Reflection.Assembly.GetExecutingAssembly.Location _
).Comments + vbCrLf + vbCrLf
If UBound(strArgs) < 3 Then
strMSG = strMSG + "Usage: EPSendMail from@email.com, " + _
"to@email.com, subject, message, [smtp Server]," + _
"[cc1@email.com;cc2@email.com;...], [attachment1;" + _
"attachment2;...]" + vbCrLf
Console.Write(strMSG)
Exit Sub
End If
strMSG = strMSG + "Sending email message" + vbCrLf + _
" From --> " + Trim(strArgs(0)) + vbCrLf + _
" To --> " + Trim(strArgs(1)) + vbCrLf + _
" Subject --> " + Trim(strArgs(2)) + vbCrLf + _
" Message --> " + Trim(strArgs(3)) + vbCrLf
If UBound(strArgs) >= 4 Then
If Len(Trim(strArgs(4))) > 0 Then
blnSMTP = True
strMSG = strMSG + " SMTP Server --> " + Trim(strArgs(4)) + _
vbCrLf
End If
End If
If UBound(strArgs) >= 5 Then
If Len(Trim(strArgs(5))) > 0 Then
blnCC = True
strMSG = strMSG + " CC --> " + Trim(strArgs(5)) + _
vbCrLf
End If
End If
If UBound(strArgs) >= 6 Then
If Len(Trim(strArgs(6))) > 0 Then
blnAttachments = True
strMSG = strMSG + " Attachments --> " + Trim(strArgs(6)) + _
vbCrLf
End If
End If
Console.Write(strMSG)
'send the email
Try
Dim insMail As New MailMessage()
With insMail
.From = Trim(strArgs(0))
.To = Trim(strArgs(1))
.Subject = Trim(strArgs(2))
.Body = Trim(strArgs(3))
If blnCC Then .Cc = Trim(strArgs(5))
If blnAttachments Then
Dim strFile As String
Dim strAttach() As String = Split(strArgs(6), ";")
For Each strFile In strAttach
.Attachments.Add(New MailAttachment(Trim(strFile)))
Next
End If
End With
If blnSMTP Then SmtpMail.SmtpServer = Trim(strArgs(4))
SmtpMail.Send(insMail)
Console.WriteLine("Successfully sent email message" + vbCrLf)
Catch err As Exception
Console.WriteLine("EXCEPTION " + err.Message + vbCrLf)
End Try
End Sub
P.S. Я бы на вашем месте VB6 похоронил, и перешел бы на VB.NET или на C#
Потому-что ваше приложение небудит работать на Windows 8!
Сам ищу - валид не могу найти уже неделю...
Похожие вопросы
- как можно удалить темп файлы с помощью visual basic. . как можно удалить темп файлы с помощью visual basic
- у меня вопроскакого вида программы можно прописать с помощью visual basic 6.0&
- Продаю программу с исходниками на Visual Basic 2008 за 50 руб.
- Нужна помощь по Visual Basic
- Подойдет ли самоучитель по Visual Basic .NET для освоения языка Basic, человеку не понимающему в программировании?
- Visual Basic или C#
- visual basic 6
- visual basic 2008
- Программа на Microsoft Visual Basic 2008
- как в visual basic определить методом Монте-Карло площадь треугольника?