Email sender

Язык Visual Basic на платформе .NET.

Модераторы: Ramzes, Sebas

Uce
Начинающий
Начинающий
 
Сообщения: 3
Зарегистрирован: 16.09.2009 (Ср) 22:22

Email sender

Сообщение Uce » 16.09.2009 (Ср) 22:29

Начнем с того что я нуб)
Скоко не пытался по различным примерам сделать Прогу для отправки сообщений на мыло.
Всегда что-то работало не так.
Кто может написать\дать исходнк простого отправщика почты.

С сайта не катит, не хотят оптимизироваться в Visual studio 2008
буду очень признателен. :alien:

Uce
Начинающий
Начинающий
 
Сообщения: 3
Зарегистрирован: 16.09.2009 (Ср) 22:22

Re: Email sender

Сообщение Uce » 16.09.2009 (Ср) 22:41

Например пробовал вот это
Код: Выделить всё
Imports System.Net.Mail
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim smtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        smtpServer.Credentials = New Net.NetworkCredential("alex******@gmail.com", "******")
        'using gmail
        smtpServer.Port = 587
        smtpServer.Host = "smtp.gmail.com"
        smtpServer.EnableSsl = True
        mail = New MailMessage()
        mail.From = New MailAddress("alex*******@gmail.com")
        mail.To.Add("alexr*******@gmail.com")
        mail.Subject = ("blablabla")
        mail.Body = TextBox1.Text
        smtpServer.Send(mail)
        MsgBox("Mail Sent Succesfull!")
    End Sub
End Class


ругается на smtpServer.Send(mail)

System.Net.Mail.SmtpException was unhandled
Message="Сбой при отправке сообщения электронной почты."
Source="System"
StackTrace:
в System.Net.Mail.SmtpClient.Send(MailMessage message) в WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) в C:\Users\Ecu\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb:строка 17 в System.Windows.Forms.Control.OnClick(EventArgs e) в System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) в System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) в System.Windows.Forms.Control.WndProc(Message& m) в System.Windows.Forms.ButtonBase.WndProc(Message& m) в System.Windows.Forms.Button.WndProc(Message& m) в System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) в System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) в System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) в System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) в System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) в System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) в Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() в Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() в Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) в WindowsApplication1.My.MyApplication.Main(String[] Args) в 17d14f5c-a337-4978-8281-53493378c1071.vb:строка 81 в System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) в System.Threading.ThreadHelper.ThreadStart()
InnerException: System.FormatException
Message="Недопустимый знак в заголовке электронной почты."
Source="System"
StackTrace:
в System.Net.BufferBuilder.Append(String value, Int32 offset, Int32 count) в System.Net.Mail.EHelloCommand.PrepareCommand(SmtpConnection conn, String domain) в System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) в System.Net.Mail.SmtpClient.Send(MailMessage message)
InnerException:

Williams
Гуру
Гуру
Аватара пользователя
 
Сообщения: 1280
Зарегистрирован: 06.05.2008 (Вт) 18:35
Откуда: System.Reflection.Williams (увидел себя в зеркале :))

Re: Email sender

Сообщение Williams » 16.09.2009 (Ср) 23:45

Синхронно:

Код: Выделить всё
        Dim client As New SmtpClient

        With client
            .Host = "smtp.gmail.com"
            .Port = 465
            .Credentials = New NetworkCredential("your_username", "your_password")
            .EnableSsl = True
            .Timeout = 50000
        End With

        Dim message As New MailMessage("myemail@gmail.com", "bobby@yahoo.com")

        With message
            .Subject = "Greetings!"
            .BodyEncoding = System.Text.Encoding.UTF8
            .SubjectEncoding = System.Text.Encoding.UTF8
            .Priority = MailPriority.Normal
            .IsBodyHtml = False
        End With



        With message.Attachments
            .Add(New Attachment("C:\picture1.jpg"))
            .Add(New Attachment("C:\picture2.jpg"))
        End With

        Try
            client.Send(message)
        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try


Асинхронно:

Код: Выделить всё
        Dim client As New SmtpClient

        With client
            .Host = "smtp.gmail.com"
            .Port = 465
            .Credentials = New NetworkCredential("your_username", "your_password")
            .EnableSsl = True
            .Timeout = 50000
        End With

        Dim message As New MailMessage("myemail@gmail.com", "receiver@yahoo.com")

        With message
            .Subject = "Hi!"
            .BodyEncoding = System.Text.Encoding.UTF8
            .SubjectEncoding = System.Text.Encoding.UTF8
            .Priority = MailPriority.Normal
            .IsBodyHtml = False
        End With



        With message.Attachments
            .Add(New Attachment("C:\picture1.jpg"))
            .Add(New Attachment("C:\picture2.jpg"))
        End With
        AddHandler client.SendCompleted, AddressOf SendMessageCompleted


        Try
            client.SendAsync(message, Nothing)
        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try


    Private Sub SendMessageCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

        MessageBox.Show("Sending Complete!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub
И вы думаете, что вас оставят в живых после прочтения этого поста?


Вернуться в Visual Basic .NET

Кто сейчас на конференции

Сейчас этот форум просматривают: Google-бот и гости: 24

    TopList