locked
Easiest way to send email?

    Question

  •  

    What is the easiest way to send emails? I had found a way a couple weeks ago, and searching the site doesn't do me any good, because none of the examples work
    Wednesday, November 14, 2007 7:22 PM

Answers

  • here you go. if u use gmail, works fine!

    Code Block

    Imports System.Net.Mail
    Imports System.Web

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

     Dim theMailMessage As MailMessage = New MailMessage("from", "To")

                With theMailMessage
                    .Body = RichTextBox1.Text
                    .Subject = TextBox3.Text
                End With


                'E-Mail Credentials and Sending
                Dim theClient As SmtpClient = New SmtpClient("SMTPprovider", 587)
                theClient.Timeout = 30000
                theClient.EnableSsl = True

                Dim theCredential As System.Net.NetworkCredential = New System.Net.NetworkCredential("userName", "password")
                theClient.Credentials = theCredential

                'Send the email
                theClient.Send(theMailMessage)
                MessageBox.Show(("E-Mail Sent Successfully!" _
                                + (Environment.NewLine + "Thank You")), "Confirmation", MessageBoxButtons.OK)

            Catch smtpex As SmtpException
                MessageBox.Show(("An error occured when trying to send a mail.  You appear to be disconnected from the Internet.  Pleas" & _
                    "e confirm that you are connected to the Internet and Resubmit all the data.  " + smtpex.Message))
            Catch ex As System.Exception
                MessageBox.Show(("An unexpected error occured. " + ex.Message))


            End Try

        End Sub


    Wednesday, November 14, 2007 7:40 PM
  • Hello:

     

    I use Microsoft Outlook, and the code I use is:

     

    Imports Outlook = Microsoft.Office.Interop.Outlook  ' At the General Section of the Form

     

    Private Sub cmdMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMail.Click

    Dim oOutlook As New Outlook.Application

    Dim olNs As Outlook.NameSpace

    Dim oMail As Outlook.MailItem

     

    oOutlook = CreateObject("Outlook.Application")

    olNs = oOutlook.GetNamespace("MAPI")

    olNs.Logon()

    oMail = oOutlook.CreateItem(Outlook.OlItemType.olMailItem)

    oMail.Subject = "Subject of the mail"

    oMail.Body = "Text of the mail"

    oMail.To = "adress@mail.com"

    oMail.ReadReceiptRequested = True

    oMail.Save()  ' Or oMail.Send() if you wish to send inmediatly

     

     

    I hope this will be useful to you

     

     

    Rafael Altungy

    SPAIN

    Wednesday, November 14, 2007 7:31 PM
  • The System.Net.SmtpClient is the way to go.  It does not require Outlook to be installed, but you do need an SMTP server.  Documentation is here: http://msdn2.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

    -Ryan / Kardax
    Wednesday, November 14, 2007 9:52 PM
  • Here is one template. You need an available SMTP server to send mail.

    Code Block

    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 mailInstance As MailMessage = New MailMessage("FromMailAdress", "ToMailAdress", "Subject", "Body")

     

            mailInstance.Attachments.Add(New Attachment("filename")) 'Optional

     

            Dim mailSenderInstance As SmtpClient = New SmtpClient("smtpHostAdress", 25) '25 is the port of the SMTP host

     

            mailSenderInstance.Credentials = New System.Net.NetworkCredential("LoginAccout", "Password")

     

            mailSenderInstance.Send(mailInstance)

     

            mailInstance.Dispose()

     

        End Sub

    End Class

     

    Friday, November 16, 2007 5:52 AM

All replies

  • Hello:

     

    I use Microsoft Outlook, and the code I use is:

     

    Imports Outlook = Microsoft.Office.Interop.Outlook  ' At the General Section of the Form

     

    Private Sub cmdMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMail.Click

    Dim oOutlook As New Outlook.Application

    Dim olNs As Outlook.NameSpace

    Dim oMail As Outlook.MailItem

     

    oOutlook = CreateObject("Outlook.Application")

    olNs = oOutlook.GetNamespace("MAPI")

    olNs.Logon()

    oMail = oOutlook.CreateItem(Outlook.OlItemType.olMailItem)

    oMail.Subject = "Subject of the mail"

    oMail.Body = "Text of the mail"

    oMail.To = "adress@mail.com"

    oMail.ReadReceiptRequested = True

    oMail.Save()  ' Or oMail.Send() if you wish to send inmediatly

     

     

    I hope this will be useful to you

     

     

    Rafael Altungy

    SPAIN

    Wednesday, November 14, 2007 7:31 PM
  • here you go. if u use gmail, works fine!

    Code Block

    Imports System.Net.Mail
    Imports System.Web

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

     Dim theMailMessage As MailMessage = New MailMessage("from", "To")

                With theMailMessage
                    .Body = RichTextBox1.Text
                    .Subject = TextBox3.Text
                End With


                'E-Mail Credentials and Sending
                Dim theClient As SmtpClient = New SmtpClient("SMTPprovider", 587)
                theClient.Timeout = 30000
                theClient.EnableSsl = True

                Dim theCredential As System.Net.NetworkCredential = New System.Net.NetworkCredential("userName", "password")
                theClient.Credentials = theCredential

                'Send the email
                theClient.Send(theMailMessage)
                MessageBox.Show(("E-Mail Sent Successfully!" _
                                + (Environment.NewLine + "Thank You")), "Confirmation", MessageBoxButtons.OK)

            Catch smtpex As SmtpException
                MessageBox.Show(("An error occured when trying to send a mail.  You appear to be disconnected from the Internet.  Pleas" & _
                    "e confirm that you are connected to the Internet and Resubmit all the data.  " + smtpex.Message))
            Catch ex As System.Exception
                MessageBox.Show(("An unexpected error occured. " + ex.Message))


            End Try

        End Sub


    Wednesday, November 14, 2007 7:40 PM
  • Sorry, but neither of those work. Anyone else have an example?

     

    Wednesday, November 14, 2007 8:05 PM
  • Sorry I forgot to say that you need to add the reference to Microsoft Outlook Library in the COM TAB

     

    Regards

     

    Rafael Altungy

    SPAIN

    Wednesday, November 14, 2007 8:11 PM
  • The System.Net.SmtpClient is the way to go.  It does not require Outlook to be installed, but you do need an SMTP server.  Documentation is here: http://msdn2.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

    -Ryan / Kardax
    Wednesday, November 14, 2007 9:52 PM
  • None of this is working. Can anyone help me???

    Thursday, November 15, 2007 3:13 AM
  • do u catch any error?  try with a diferent SMTP provider....
    Thursday, November 15, 2007 3:27 PM
  • Here is one template. You need an available SMTP server to send mail.

    Code Block

    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 mailInstance As MailMessage = New MailMessage("FromMailAdress", "ToMailAdress", "Subject", "Body")

     

            mailInstance.Attachments.Add(New Attachment("filename")) 'Optional

     

            Dim mailSenderInstance As SmtpClient = New SmtpClient("smtpHostAdress", 25) '25 is the port of the SMTP host

     

            mailSenderInstance.Credentials = New System.Net.NetworkCredential("LoginAccout", "Password")

     

            mailSenderInstance.Send(mailInstance)

     

            mailInstance.Dispose()

     

        End Sub

    End Class

     

    Friday, November 16, 2007 5:52 AM
  • What Email provider are you using?  If they require Implicit SSL I do not think system.net.mail will work.
    Wednesday, August 12, 2009 7:52 PM
  • here you go. if u use gmail, works fine!

    Code Block

    Imports System.Net.Mail
    Imports System.Web

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

     Dim theMailMessage As MailMessage = New MailMessage("from", "To")

                With theMailMessage
                    .Body = RichTextBox1.Text
                    .Subject = TextBox3.Text
                End With


                'E-Mail Credentials and Sending
                Dim theClient As SmtpClient = New SmtpClient("SMTPprovider", 587)
                theClient.Timeout = 30000
                theClient.EnableSsl = True

                Dim theCredential As System.Net.NetworkCredential = New System.Net.NetworkCredential("userName", "password")
                theClient.Credentials = theCredential

                'Send the email
                theClient.Send(theMailMessage)
                MessageBox.Show(("E-Mail Sent Successfully!" _
                                + (Environment.NewLine + "Thank You")), "Confirmation", MessageBoxButtons.OK)

            Catch smtpex As SmtpException
                MessageBox.Show(("An error occured when trying to send a mail.  You appear to be disconnected from the Internet.  Pleas" & _
                    "e confirm that you are connected to the Internet and Resubmit all the data.  " + smtpex.Message))
            Catch ex As System.Exception
                MessageBox.Show(("An unexpected error occured. " + ex.Message))


            End Try

        End Sub


    oh...! thnx buddy... this is the only code i found working...
    i tried many codes...even frm other forums... none worked...
     thnx a lot again

    theres nothing better than prime nos...!!
    Thursday, October 15, 2009 5:02 PM
  • when i use this code..

    this not work on me..

    I receive message

    "An error occured wher trying to send a mail. You appear to be disconnected

    from the internet. Please confirm that you are connected to the internet and resubmit all the data. Failure sending mail"

     

    please help me how to fix it..

     

     

    ASAP..

    Monday, January 09, 2012 1:36 PM
  • Thank You... I thought it was not working..

    but i'm wrong...

    Monday, January 09, 2012 2:39 PM
  • I tend to create a mail handling class in my applications. The class below includes a method/function that sends mail asynchronously.

    class MailHandler { public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body, string attachment, MailPriority priority) { string server = Properties.Settings.Default.SMTPServer; string username = Properties.Settings.Default.SMTPUsername; string password = Properties.Settings.Default.SMTPPassword; int port = Properties.Settings.Default.SMTPPort; bool useDefaultCredentials = Properties.Settings.Default.SMTPUseDefaultCredentials; bool useSSL = Properties.Settings.Default.SMTPUseSSL; bool asHTML = Properties.Settings.Default.SMTPAsHTML; MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(from); mailMessage.To.Add(new MailAddress(to)); if ((bcc != null) && (bcc != string.Empty)) { mailMessage.Bcc.Add(new MailAddress(bcc)); } if ((cc != null) && (cc != string.Empty)) { mailMessage.CC.Add(new MailAddress(cc)); } mailMessage.Subject = subject; mailMessage.Body = body; mailMessage.IsBodyHtml = asHTML; mailMessage.Priority = priority; SmtpClient smtpClient = new SmtpClient(server); smtpClient.Port = port; if (useDefaultCredentials) { smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials; } else { smtpClient.Credentials = new System.Net.NetworkCredential(username, password); } smtpClient.EnableSsl = useSSL; object userState = mailMessage; smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted); smtpClient.SendAsync(mailMessage, userState); } static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { MailMessage mailMessage = (MailMessage)e.UserState; string subject = mailMessage.Subject; if (e.Cancelled) { Console.WriteLine("Send canceled for mail with subject [{0}].", subject); } if (e.Error != null) { Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString()); } else { Console.WriteLine("Message [{0}] sent.", subject); } } }


    Sunday, October 21, 2012 5:22 PM