Send e-mail from a windows application

To send mail from our windows application in visual basic. net System.Net.Mail.MailMessage use the class.

For the following example we will create a form (Form1), a text box to type the message (TextBox1) and a button (Button1) to send text to send the e-mail.


 Dim As New System.Net.Mail.MailMessage mymail

 miCorreo.IsBodyHtml = False
 miCorreo.From = "info@solovb.net" 'mail where you send
 correo.To.Add ("usuario@hotmail.com") 'Mail Recipient
 correo.Subject = "Test message from windows application [solovb.net]" 'Case

 miCorreo.Body = TextBox1.Text 'Message Body

 miCorreo.Priority = System.Net.Mail.MailPriority.Normal 'Priority

 Dim smtp As New System.Net.Mail.SmtpClient

 smtp.Host = "smtp.solovb.net"
 'I pass the credentials for the account and password from which you sent
 smtp.Credentials = New System.Net.NetworkCredential ("info@solovb.net" "clave123456"))

 Try
       smtp.Send (mymail)
       MsgBox ("Message sent." MsgBoxStyle.OKOnly, "solovb.net")
 Catch ex As Exception
       MsgBox ("ERROR:" & ex.Message, MsgBoxStyle.OKOnly, "Error!")
 End Try

The fact that you can add much functionality to this example as text boxes for sender, recipient and SMTP settings.

But for an example (as I like) the simpler the better you get ... there will be time to complicate it, haha.

If we IsBodyHtml property to True, we fill the body with HTML code so that our message got a better look.

If you served, leave a comment.

50 Responses to "Send e-mail from a windows application"

  1. Information Bitacoras.com ...

    If you wish, you can click to rate this post Bitacoras.com. Thanks ....

  2. Jose says:

    Good.

    One question, in the following line:

    miCorreo.From = "josem.flores @ sfb.es" 'mail where you send

    It indicates that a value of type "String" can not be converted to 'System.Net.Mail.MailAdress'

    Let's see if I can help out please. Since I've changed but I can not fix that error.

    A greeting and thanks

    • Elisha says:

      you can use the following line to avoid error

      New System.Net.Mail.MailAddress miCorreo.From = ("josem.flores @ sfb.es")

      Elisha

  3. Jose says:

    Thank you very much.

    But I have a problem, I paste my code and tell you the problem:

    Dim As New System.Net.Mail.MailMessage _Message ()

    Dim As New System.Net.Mail.SmtpClient _SMTP

    'STMP SETTINGS

    = New System.Net.NetworkCredential _SMTP.Credentials ("mail account", "password")

    _SMTP.Host = "Smtp.gmail.com"

    _SMTP.Port = 465

    _SMTP.EnableSsl = True

    'CONFIGURATION MESSAGE

    _Message. [To]. Add ("mail account") 'mail account to which you want to send the e-mail

    _Message.From = New System.Net.Mail.MailAddress ("mail account", "name", System.Text.Encoding.UTF8) 'The sender

    _Message.Subject = "Test" 'Subject of the e-mail

    _Message.SubjectEncoding = System.Text.Encoding.UTF8 'Coding

    _Message.Body = "This is a test" 'content of the mail

    _Message.BodyEncoding = System.Text.Encoding.UTF8

    '_Message.Priority = System.Net.Mail.MailPriority.Normal

    _Message.IsBodyHtml = False

    'ADDING ATTACHMENTS

    'Dim _file As String = My.Application.Info.DirectoryPath & "file"' file you want to attach

    'Dim As New System.Net.Mail.Attachment _Attachment (_file, System.Net.Mime.MediaTypeNames.Application.Octet)

    '_Message.Attachments.Add (_Attachment)

    'SHIPPING

    Try

    _SMTP.Send (_Message)

    Catch ex As System.Net.Mail.SmtpException

    'MessageBox.Show (ex.ToString)

    End Try

    This code is ok.

    Now, when I send mail I get the following error:

    It exceeded the wait time of application.

    Making a telnet smtp.gmail.com 465 connect perfectly, yet it does not send the e ...

    I would appreciate if you can clear you to give me a solution because I can not get it ...

    A greeting and thank you very much in advance.

  4. Elisha says:

    The code is good, in fact I use it in apliaciones and running. Apparently the issue is with some gmail settings that are missing. With gmail I get an error. If I make it work with gmail, I will post the solution.

  5. foogeobre says:

    Thank you!

  6. Jose says:

    Ok Elisha.

    I change the port as the 587 would not let me get through telnet, but still not working ...

    I will keep looking and if I tell him something. And I hope your solution if it still fails. : D

    Thank you very much and best regards.

  7. Jose says:

    By the way.

    Can you tell me any SMTP server that works correctly with your're running for example, need to implement it as soon as possible and I can not find any server that works ...

    See if you can tell me with whom you do it and I try it and see if so if I work. ;)

    Thank you very much again.

    A greeting.

  8. Jose says:

    Good.

    Now everything is settled, it was problem of Antivirus programs that cut worm conducting a mass mailing ... must be entered in the exceptions process and working properly.

    Thank you very much.

    A greeting.

  9. kyha says:

    Good morning.

    I have a code very, very similar to suyo.El Problem is that I do not send mail until you close the application, or other mail delivery entionces latter is not sent. Would you know why or how I can fix it?
    Thanks

  10. Carlos says:

    This is a very good function! thanks for sharing this with us : D

  11. Marino says:

    Thank you for sharing this code ... It was driving me crazy and nothing I went out the mail. Thanks to yours solved the error. Keep it up ... :)

  12. Pharsat says:

    hellos, for those who can not find an smtp server that works I give a configuration that is used with windows live hotmail, is 100% sure I leave my code to observe.

    Please do not modify its program code related to the configuration of the server to avoid errors.

    Dim As New System.Net.Mail.MailMessage mymail

    miCorreo.IsBodyHtml = False
    miCorreo.From = New System.Net.Mail.MailAddress ("ejemplo@hotmail.com") 'send mail from where
    miCorreo.To.Add ("ejemplo@hotmail.com") 'Mail Recipient
    miCorreo.Subject = "example" 'Case
    miCorreo.Body = "you can put a string of text or html code if expesificas it will include the message"
    miCorreo.Priority = System.Net.Mail.MailPriority.Normal 'Priority
    'Does not modify the following lines qye and correspond to the SMTP server coniguracion of microsoft (windows live)
    Dim smtp As New System.Net.Mail.SmtpClient

    smtp.Host = "smtp.live.com"
    smtp.Port = "25"
    smtp.EnableSsl = True

    'I pass the credentials for the account and password from which you sent
    = New System.Net.NetworkCredential smtp.Credentials ("ejemplo@hotmail.com", "password")

    Try
    smtp.Send (mymail)
    MsgBox ("Message sent." MsgBoxStyle.OKOnly)
    Catch ex As Exception
    MsgBox ("ERROR:")
    End Try

    and last agredezco to the creator of the item served me too. thanks

  13. oscar says:

    PARTNERS thank you very much are a few bosses truth that served me much information.

  14. Gonzalo says:

    Hello, you either andubo? , Was able to send any mail?
    I get my error when trying to send an email : (
    the credentials ok?
    or where I can get another?

    Greetings.

  15. alejandro says:

    Good for everyone, this post has helped me a lot, but I have a small problem when I attach a file, I get the following error: NotSupportedException was unhandled
    Unsupported format provided in the path.

    Code:
    Dim file As String = My.Application.Info.DirectoryPath & "c: \ arcchivo.rar"

    __________________inicio bug where brand
    Dim attachment As New System.Net.Mail.Attachment (file, System.Net.Mime.MediaTypeNames.Application.Pdf)
    msg.Attachments.Add (attached)
    __________________fin bug where brand

    if anyone can tell me I'm doing wrong I thank you in advance.

    Keep PCs are so helpful.

  16. Gianpiere Bernuy Ramos says:

    ta hola q tal chebre your post but for q works for you can change the
    _SMTP.Port = 465
    by
    _SMTP.Port = 587
    to me that I run.
    q luck God bless

  17. jose says:

    I tested this code and it worked perfect, I made the changes to the SMTP server recomendades Pharsat and annotation by Elisha to miCorreo.From = New System.Net.Mail.MailAddress ("josem.flores @ sfb.es") and was great, thank you very much.

  18. Charly says:

    Thanks for the code

  19. dosmilcero says:

    Hey.

    I have developed a program to send via SMTP accessing an exchange server that requires authentication with SSL. The program works perfectly from any computer outside the network where the exchange server, but if I install it on a PC network exchange server does not work. Always give server not found error.

    Using PING to the server from the LAN to the DNS, IP public, private, telnet, etc. .. provided no response. On the Exchange server log says nothing, it is clear that no one knows. Antivirus and firewall disconnected Any ideas?

  20. Fredy says:

    q das hi Parsat mefunciona code of marabillas but I have a problem is I have a form where q APODER will put any mail to send the message, as I can in the script espesificar q see the form fields to know q will send e-aq the message, the matter
    agradeseria you much if mepudes ayuadar

  21. arturo says:

    With Windows 7 Professional does not work, why??

  22. Builes Julian says:

    Excellent code brother

    many thanks

  23. Cristian says:

    Hello everyone and try the original code, the modification of parshat with the other company and disable the antivirus and firewall and still not sending me the code to copy and paste with the fields obvious someone can give me another solution ?

  24. Cristian says:

    I again if someone does not try out the port 587 in hotmail

  25. john says:

    Was very good thanks this helped me a great

  26. Jesus says:

    Hey. These are my first steps in vb.net, (so far into with vb6). The code to email server me well, is very good example. Now I want to complicate the acknowledgments, attachments, etc.. I wanted to ask a little thing Could you send me an example to download mail? Thank you very much for everything.

  27. lester says:

    ami a query and send me emails the problem is I need envviar an image in the email body not as attachment (type advertising) but did not let me send .. I have in a RichTextBox the image but does not send anything ... if anyone can help me I would greatly appreciate

    my email is lesterelpepecalderon@hotmail.com

  28. Eigthy says:

    I put a little more than adding beauty to the form's textbox and a button and a RichTextBox so that more paresca a web form. For this we will create:

    Textbox1 (Case)
    Textbox2 (receiver)
    Textbox3 (your email)
    Textbox4 (passwor - password)
    RichTextBox1 (message)
    Button1 (submit button)

    is configured to hotmail, let the code below:

    button1 click:

    Dim As New System.Net.Mail.MailMessage mymail

    miCorreo.IsBodyHtml = False
    miCorreo.From = New System.Net.Mail.MailAddress (TextBox2.Text) 'sends mail address where
    miCorreo.To.Add (TextBox3.Text) 'Mail Recipient
    miCorreo.Subject = TextBox1.Text 'subject
    miCorreo.Body = RichTextBox1.Text
    miCorreo.Priority = System.Net.Mail.MailPriority.Normal 'Priority

    'Does not modify the following lines as they relate to the SMTP server coniguracion of microsoft (windows live)
    Dim smtp As New System.Net.Mail.SmtpClient

    smtp.Host = "smtp.live.com"
    smtp.Port = 25
    smtp.EnableSsl = True

    'I pass the credentials for the account and password from which you sent
    = New System.Net.NetworkCredential smtp.Credentials (TextBox3.Text, TextBox4.Text)

    Try
    smtp.Send (mymail)
    MsgBox ("Message sent." MsgBoxStyle.OKOnly)
    Catch ex As Exception
    MsgBox ("ERROR:")
    End Try

    as they see follow the original code but the code did Marine SMTP settings for hotmail I just say more beauty to the form as shown in this image: http://i46.tinypic.com/34ir2c0.jpg

    I hope and serve them greetings and thanks SoloVB

    I say goodbye!

  29. sagilman says:

    Hi I get the following error and not because they put all the code as it is.
    the error is:
    Failure sending message.

    someone who can help me please.

  30. juan miguel says:

    thanks for the contributions muchisisisimo served me seriously :)

  31. Renan Cardona says:

    With me it worked wonderfully

  32. sonia says:

    Hello, I send a link automatically giving a button where the data should I bring a select in oracle data base, the only thing is a must have for; subject: anger within the text and the link should be sent but not how could help me!

  33. Fernando says:

    hey friends I do not mistake me worked mm

    so I have the codigolo

    Public Class Form1

    Enviarcorreo Public Sub ()

    Dim As New System.Net.Mail.MailMessage mymail
    miCorreo.IsBodyHtml = False
    miCorreo.From = New System.Net.Mail.MailAddress ("luisfernandop.s @ hotmail.com") 'send mail from where
    miCorreo.To.Add ("luisfernandop.s @ hotmail.com") 'Mail Recipient
    miCorreo.Subject = "test fer" 'Case
    miCorreo.Body = "none"
    miCorreo.Priority = System.Net.Mail.MailPriority.Normal 'Priority
    'Does not modify the following lines qye and correspond to the SMTP server coniguracion of microsoft (windows live)
    Dim smtp As New System.Net.Mail.SmtpClient

    smtp.Host = "smtp.live.com"
    smtp.Port = 25
    smtp.EnableSsl = True

    'I pass the credentials for the account and password from which you sent
    = New System.Net.NetworkCredential smtp.Credentials ("luisfernandop.s @ hotmail.com", "/ n @ ndo/6724766887")

    Try
    smtp.Send (mymail)
    MsgBox ("Message sent." MsgBoxStyle.OKOnly)
    Catch ex As Exception
    MsgBox ("ERROR:")
    End Try
    End Sub

    Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    enviarcorreo ()

    End Sub
    End Class

    if someone could make me see my mistake I would greatly appreciate

  34. Chocobar Cristian says:

    Muyyyy good works of 10! ... Excellent contribution! Now I stick hand ... Thank you so much =)

  35. Edgar says:

    Shoots through Pharsat ..
    Your contribution helped me a lot ...

    Thanks to all who contribute with their help both code q

  36. Edgar says:

    Hello xfa eg someone could say as I do when I want to send messages from other mail than hotmail ..

    eg yahoo, gmail, etc. ... or my company server ...

  37. leboche says:

    Discula a dudami this is my code:

    Dim As New System.Net.Mail.MailMessage mymail
    Dim mail As New System.Net.Mail.MailMessage
    micorreo.IsBodyHtml = False
    New System.Net.Mail.MailAddress micorreo.From = ("leboche@hotmail.com")
    correo.To.Add ("leboche@hotmail.com")
    correo.Subject = "message prueva"

    micorreo.Body = TextBox1.Text

    micorreo.Priority = Net.Mail.MailPriority.Normal

    Dim smtp As New System.Net.Mail.SmtpClient
    smtp.Host = "smtp.live.com"
    smtp.Port = 587
    smtp.EnableSsl = True
    = New System.Net.NetworkCredential smtp.Credentials ("info@solovb.net", "123456")

    Try
    smtp.Send (mymail)
    MsgBox ("Message sent." MsgBoxStyle.OKOnly, "info@solovb.net")
    Catch ex As Exception
    MsgBox ("Error:")

    End Try

    but do not know if this code in the textbox or from or on a button you could help me?
    I am a beginner in ESOT

    • Elisha says:

      I would put this code in a send button after validating that all data entered necessary.

      Luck

  38. Melesio Salas says:

    Everything went great!

    Thanks for the input

    A Greeting

  39. Gilmer 20 says:

    interesting sobretod your example and easy to understand but I have a question, I want you in the mail body is the content of a Report
    as dificio is part, I hope your help

  40. CarlosH says:

    Thank you very much. A small contribution.
    To send with a copy (CC) =
    correo.CC.Add ("Address")

  41. Javier says:

    Hello, I have this problem:

    I said "No name has been declared amp"
    and
    "Character is not valid" by the "" next to amp.

    who can help me thank you very much.

  42. Javier says:

    Muuuuchas thanks .. I have found the solucioen :)

  43. Javier says:

    You could add as sending or element or as adding private archives?

  44. Jordi says:

    Thank you all for the code!

    fable works!

  45. Marce says:

    Hello,
    very good page,
    quisisera whether through the code for sending mail can send a blind copy Bcc to a square, where there are several more emails, is that possible?

  46. jhoanna0627 says:

    hello:

    thank you very much served me wonderfully, thank you, I was finally able to send mail, but I have a duditas with respect to how you put a link in the body, that way you customize with html tags and how to send a thank adjunto.muchas, the I worked perfect code.

Comment