Tuesday, July 3, 2012

Mengirim Email dalam Visual Basic


Contoh Aplikasi Send Mail
Adapun source code untuk vb.net-nya :

01Public Class frmMain
02'
03'This will contain the actual message data to send.
04Dim message As System.Net.Mail.MailMessage
05'
06'The Simple Mail Tranfer Protocol client with the Host and Port number to use. You will
07'want to change these settings to what you need to use. The host, smtp.gmail.com and port
08'will work if you have a gmail account.
09Dim smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
10'
11'Will contain the attachment info to send with the message.
12Dim attach As System.Net.Mail.Attachment
13'
14'
15Private Sub btnAttach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttach.Click
16'
17'Create a openDialog object to be able to select a attachment to the mail message.
18Dim openDLG As New OpenFileDialog
19 
20openDLG.AddExtension = True
21openDLG.ReadOnlyChecked = True
22openDLG.Title = "Select the file you want added to the message..."
23 
24If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
25 
26txtAttachment.Text = openDLG.FileName
27 
28attach = New System.Net.Mail.Attachment(openDLG.FileName)
29 
30End If
31 
32End Sub
33 
34Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
35'
36'Setup the mail message with the info in the textboxes...
37message = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text, _
38txtSubject.Text, txtMessage.Text)
39'
40'Need to make sure the attachment exists before adding it to the message.
41If My.Computer.FileSystem.FileExists(txtAttachment.Text) Then _
42message.Attachments.Add(attach)
43'
44'Use Secure Socket Layer to Encrypt the connection for sending the mail. This needs
45'be set to "True" if you plan on using GMail as your host.
46smtp.EnableSsl = True
47'
48'Setup your account information for authorization...
49smtp.Credentials = New System.Net.NetworkCredential(txtUserName.Text, _
50txtPass.Text)
51 
52Try
53'
54'Send the mail message that was setup.
55smtp.Send(message)
56'
57'Usually it was successful in sending the message if it makes it to here :)
58'Otherwise it should throw a error message from the Catch exception below.
59MessageBox.Show("Well, the mail message appears to have been a success!", _
60"  Successful?", MessageBoxButtons.OK, MessageBoxIcon.Information)
61 
62Catch exc As Net.Mail.SmtpException
63 
64MessageBox.Show(exc.StatusCode.ToString, "  Something Happened?", _
65MessageBoxButtons.OK, MessageBoxIcon.Error)
66 
67End Try
68 
69End Sub
70 
71End Class      

Semoga bisa diterapkan diaplikasi vb.net teman-teman. Sekali lg program aplikasi ini menggunakan GMAIL credentials!

Source:  http://herosetyanofario.wordpress.com/

HOT INFO

Anda ingin mencari refrensi dan contoh program lengkap ? Kami ada. Sekarang Anda bisa mencari PDF Visual Basic di situs ini : www.panduanSkripsi.net. Koleksi program lengkap di sana, proyek PHP dan MySQL, juga jQuery dan Framework. Bukunya juga ada.

No comments:

Post a Comment