Skip to content

Commit 3bdc492

Browse files
authored
Add VB examples for MailMessage (#4944)
1 parent b3b0945 commit 3bdc492

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
' NclMailSync
2+
3+
Imports System.Net
4+
Imports System.Net.Mail
5+
Imports System.Net.Mime
6+
7+
Public Class Mail
8+
9+
' <snippet2>
10+
Public Shared Sub CreateTestMessage2(ByVal server As String)
11+
Dim [to] As String = "[email protected]"
12+
Dim from As String = "[email protected]"
13+
Dim message As MailMessage = New MailMessage(from, [to])
14+
message.Subject = "Using the new SMTP client."
15+
message.Body = "Using this new feature, you can send an email message from an application very easily."
16+
Dim client As SmtpClient = New SmtpClient(server)
17+
' Credentials are necessary if the server requires the client
18+
' to authenticate before it will send email on the client's behalf.
19+
client.UseDefaultCredentials = True
20+
21+
Try
22+
client.Send(message)
23+
Catch ex As Exception
24+
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString())
25+
End Try
26+
End Sub
27+
' </snippet2>
28+
29+
' <snippet3>
30+
Public Shared Sub CreateTimeoutTestMessage(ByVal server As String)
31+
Dim [to] As String = "[email protected]"
32+
Dim from As String = "[email protected]"
33+
Dim subject As String = "Using the new SMTP client."
34+
Dim body As String = "Using this new feature, you can send an email message from an application very easily."
35+
Dim message As MailMessage = New MailMessage(from, [to], subject, body)
36+
Dim client As SmtpClient = New SmtpClient(server)
37+
Console.WriteLine("Changing time out from {0} to 100.", client.Timeout)
38+
client.Timeout = 100
39+
' Credentials are necessary if the server requires the client
40+
' to authenticate before it will send email on the client's behalf.
41+
client.Credentials = CredentialCache.DefaultNetworkCredentials
42+
client.Send(message)
43+
End Sub
44+
' </snippet3>
45+
46+
' <snippet4>
47+
Public Shared Sub CreateTestMessage3()
48+
Dim [to] As MailAddress = New MailAddress("[email protected]")
49+
Dim from As MailAddress = New MailAddress("[email protected]")
50+
Dim message As MailMessage = New MailMessage(from, [to])
51+
message.Subject = "Using the new SMTP client."
52+
message.Body = "Using this new feature, you can send an email message from an application very easily."
53+
'Use the application or machine configuration to get the
54+
' host, port, And credentials.
55+
Dim client As SmtpClient = New SmtpClient()
56+
Console.WriteLine("Sending an email message to {0} at {1} by using the SMTP host={2}.", [to].User, [to].Host, client.Host)
57+
client.Send(message)
58+
End Sub
59+
' </snippet4>
60+
61+
' <snippet5>
62+
Public Shared Sub CreateMessageWithMultipleViews(ByVal server As String, ByVal recipients As String)
63+
' Create a message and set up the recipients.
64+
Dim message As MailMessage = New MailMessage(
65+
66+
recipients,
67+
"This email message has multiple views.",
68+
"This is some plain text.")
69+
70+
' Construct the alternate body as HTML.
71+
Dim body As String = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"
72+
body += "<HTML><HEAD><META http-equiv=Content-Type content=""text/html; charset=iso-8859-1"">"
73+
body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text"
74+
body += "</FONT></DIV></BODY></HTML>"
75+
76+
Dim mimeType As ContentType = New System.Net.Mime.ContentType("text/html")
77+
78+
' Add the alternate body to the message.
79+
Dim alternate As AlternateView = AlternateView.CreateAlternateViewFromString(body, mimeType)
80+
message.AlternateViews.Add(alternate)
81+
82+
' Send the message.
83+
Dim client As SmtpClient = New SmtpClient(server)
84+
client.Credentials = CredentialCache.DefaultNetworkCredentials
85+
86+
Try
87+
client.Send(message)
88+
Catch ex As Exception
89+
Console.WriteLine("Exception caught in CreateMessageWithMultipleViews(): {0}", ex.ToString())
90+
End Try
91+
92+
' Display the values in the ContentType for the attachment.
93+
Dim c As ContentType = alternate.ContentType
94+
Console.WriteLine("Content type")
95+
Console.WriteLine(c.ToString())
96+
Console.WriteLine("Boundary {0}", c.Boundary)
97+
Console.WriteLine("CharSet {0}", c.CharSet)
98+
Console.WriteLine("MediaType {0}", c.MediaType)
99+
Console.WriteLine("Name {0}", c.Name)
100+
Console.WriteLine("Parameters: {0}", c.Parameters.Count)
101+
102+
For Each d As DictionaryEntry In c.Parameters
103+
Console.WriteLine("{0} = {1}", d.Key, d.Value)
104+
Next
105+
106+
Console.WriteLine()
107+
alternate.Dispose()
108+
End Sub
109+
' </snippet5>
110+
111+
' <snippet6>
112+
Public Shared Sub CreateMessageWithAttachment(ByVal server As String)
113+
' Specify the file to be attached And sent.
114+
' This example assumes that a file named Data.xls exists in the
115+
' current working directory.
116+
Dim file As String = "data.xls"
117+
' Create a message and set up the recipients.
118+
Dim message As MailMessage = New MailMessage(
119+
120+
121+
"Quarterly data report.",
122+
"See the attached spreadsheet.")
123+
124+
' Create the file attachment for this email message.
125+
Dim data As Attachment = New Attachment(file, MediaTypeNames.Application.Octet)
126+
' Add time stamp information for the file.
127+
Dim disposition As ContentDisposition = data.ContentDisposition
128+
disposition.CreationDate = System.IO.File.GetCreationTime(file)
129+
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file)
130+
disposition.ReadDate = System.IO.File.GetLastAccessTime(file)
131+
' Add the file attachment to this email message.
132+
message.Attachments.Add(data)
133+
134+
' Send the message
135+
Dim client As SmtpClient = New SmtpClient(server)
136+
' Add credentials if the SMTP server requires them.
137+
client.Credentials = CredentialCache.DefaultNetworkCredentials
138+
139+
Try
140+
client.Send(message)
141+
Catch ex As Exception
142+
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString())
143+
End Try
144+
145+
' Display the values in the ContentDisposition for the attachment.
146+
Dim cd As ContentDisposition = data.ContentDisposition
147+
Console.WriteLine("Content disposition")
148+
Console.WriteLine(cd.ToString())
149+
Console.WriteLine("File {0}", cd.FileName)
150+
Console.WriteLine("Size {0}", cd.Size)
151+
Console.WriteLine("Creation {0}", cd.CreationDate)
152+
Console.WriteLine("Modification {0}", cd.ModificationDate)
153+
Console.WriteLine("Read {0}", cd.ReadDate)
154+
Console.WriteLine("Inline {0}", cd.Inline)
155+
Console.WriteLine("Parameters: {0}", cd.Parameters.Count)
156+
157+
For Each d As DictionaryEntry In cd.Parameters
158+
Console.WriteLine("{0} = {1}", d.Key, d.Value)
159+
Next
160+
161+
data.Dispose()
162+
End Sub
163+
' </snippet6>
164+
165+
' <snippet7>
166+
Public Shared Sub CreateTestMessage4(ByVal server As String)
167+
Dim from As MailAddress = New MailAddress("[email protected]")
168+
Dim [to] As MailAddress = New MailAddress("[email protected]")
169+
Dim message As MailMessage = New MailMessage(from, [to])
170+
message.Subject = "Using the SmtpClient class."
171+
message.Body = "Using this feature, you can send an email message from an application very easily."
172+
Dim client As SmtpClient = New SmtpClient(server)
173+
Console.WriteLine("Sending an email message to {0} by using SMTP host {1} port {2}.", [to].ToString(), client.Host, client.Port)
174+
175+
Try
176+
client.Send(message)
177+
Catch ex As Exception
178+
Console.WriteLine("Exception caught in CreateTestMessage4(): {0}", ex.ToString())
179+
End Try
180+
End Sub
181+
' </snippet7>
182+
183+
' <snippet9>
184+
Public Shared Sub CreateBccTestMessage(ByVal server As String)
185+
Dim from As MailAddress = New MailAddress("[email protected]", "Ben Miller")
186+
Dim [to] As MailAddress = New MailAddress("[email protected]", "Jane Clayton")
187+
Dim message As MailMessage = New MailMessage(from, [to])
188+
message.Subject = "Using the SmtpClient class."
189+
message.Body = "Using this feature, you can send an email message from an application very easily."
190+
Dim bcc As MailAddress = New MailAddress("[email protected]")
191+
message.Bcc.Add(bcc)
192+
Dim client As SmtpClient = New SmtpClient(server)
193+
client.Credentials = CredentialCache.DefaultNetworkCredentials
194+
Console.WriteLine("Sending an email message to {0} and {1}.", [to].DisplayName, message.Bcc.ToString())
195+
196+
Try
197+
client.Send(message)
198+
Catch ex As Exception
199+
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}", ex.ToString())
200+
End Try
201+
End Sub
202+
' </snippet9>
203+
204+
' <snippet10>
205+
Public Shared Sub CreateCopyMessage(ByVal server As String)
206+
Dim from As MailAddress = New MailAddress("[email protected]", "Ben Miller")
207+
Dim [to] As MailAddress = New MailAddress("[email protected]", "Jane Clayton")
208+
Dim message As MailMessage = New MailMessage(from, [to])
209+
message.Subject = "Using the SmtpClient class."
210+
message.Body = "Using this feature, you can send an email message from an application very easily."
211+
' Add a carbon copy recipient.
212+
Dim copy As MailAddress = New MailAddress("[email protected]")
213+
message.CC.Add(copy)
214+
Dim client As SmtpClient = New SmtpClient(server)
215+
' Include credentials if the server requires them.
216+
client.Credentials = CredentialCache.DefaultNetworkCredentials
217+
Console.WriteLine("Sending an email message to {0} by using the SMTP host {1}.", [to].Address, client.Host)
218+
219+
Try
220+
client.Send(message)
221+
Catch ex As Exception
222+
Console.WriteLine("Exception caught in CreateCopyMessage(): {0}", ex.ToString())
223+
End Try
224+
End Sub
225+
' </snippet10>
226+
227+
' <snippet24>
228+
Public Shared Sub CreateMessageWithAttachment4(ByVal server As String, ByVal [to] As String)
229+
' Specify the file to be attached And sent.
230+
' This example uses a file on a UNC share.
231+
Dim file As String = "\\share3\c$\reports\data.xls"
232+
Dim message As MailMessage = New MailMessage("[email protected]", [to], "Quarterly data report", "See the attached spreadsheet.")
233+
' Create the file attachment for this email message.
234+
Dim data As Attachment = New Attachment("qtr3.xls", MediaTypeNames.Application.Octet)
235+
' Add time stamp information for the file.
236+
Dim disposition As ContentDisposition = data.ContentDisposition
237+
disposition.CreationDate = System.IO.File.GetCreationTime(file)
238+
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file)
239+
disposition.ReadDate = System.IO.File.GetLastAccessTime(file)
240+
disposition.DispositionType = DispositionTypeNames.Attachment
241+
' Add the file attachment to this email message.
242+
message.Attachments.Add(data)
243+
'Send the message.
244+
Dim client As SmtpClient = New SmtpClient(server)
245+
' Add credentials if the SMTP server requires them.
246+
client.Credentials = CType(CredentialCache.DefaultNetworkCredentials, ICredentialsByHost)
247+
client.Send(message)
248+
' Display the message headers.
249+
Dim keys As String() = message.Headers.AllKeys
250+
Console.WriteLine("Headers")
251+
252+
For Each s As String In keys
253+
Console.WriteLine("{0}:", s)
254+
Console.WriteLine(" {0}", message.Headers(s))
255+
Next
256+
257+
data.Dispose()
258+
End Sub
259+
' </snippet24>
260+
261+
End Class

xml/System.Net.Mail/MailMessage.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
110110
[!code-cpp[NclMailSync#6](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#6)]
111111
[!code-csharp[NclMailSync#6](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#6)]
112+
[!code-vb[NclMailSync#6](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#6)]
112113
113114
]]></format>
114115
</remarks>
@@ -209,6 +210,7 @@
209210
210211
[!code-cpp[NclMailSync#4](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#4)]
211212
[!code-csharp[NclMailSync#4](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#4)]
213+
[!code-vb[NclMailSync#4](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#4)]
212214
213215
]]></format>
214216
</remarks>
@@ -269,6 +271,7 @@
269271
270272
[!code-cpp[NclMailSync#2](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#2)]
271273
[!code-csharp[NclMailSync#2](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#2)]
274+
[!code-vb[NclMailSync#2](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#2)]
272275
273276
]]></format>
274277
</remarks>
@@ -349,6 +352,7 @@
349352
350353
[!code-cpp[NclMailSync#3](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#3)]
351354
[!code-csharp[NclMailSync#3](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#3)]
355+
[!code-vb[NclMailSync#3](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#3)]
352356
353357
]]></format>
354358
</remarks>
@@ -415,6 +419,7 @@
415419
416420
[!code-cpp[NclMailSync#5](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#5)]
417421
[!code-csharp[NclMailSync#5](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#5)]
422+
[!code-vb[NclMailSync#5](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#5)]
418423
419424
]]></format>
420425
</remarks>
@@ -467,6 +472,7 @@
467472
468473
[!code-cpp[NclMailSync#6](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#6)]
469474
[!code-csharp[NclMailSync#6](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#6)]
475+
[!code-vb[NclMailSync#6](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#6)]
470476
471477
]]></format>
472478
</remarks>
@@ -519,6 +525,7 @@
519525
520526
[!code-cpp[NclMailSync#9](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#9)]
521527
[!code-csharp[NclMailSync#9](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#9)]
528+
[!code-vb[NclMailSync#9](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#9)]
522529
523530
]]></format>
524531
</remarks>
@@ -571,6 +578,7 @@
571578
572579
[!code-cpp[NclMailSync#2](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#2)]
573580
[!code-csharp[NclMailSync#2](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#2)]
581+
[!code-vb[NclMailSync#2](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#2)]
574582
575583
]]></format>
576584
</remarks>
@@ -723,6 +731,7 @@
723731
724732
[!code-cpp[NclMailSync#10](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#10)]
725733
[!code-csharp[NclMailSync#10](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#10)]
734+
[!code-vb[NclMailSync#10](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#10)]
726735
727736
]]></format>
728737
</remarks>
@@ -938,6 +947,7 @@
938947
939948
[!code-cpp[NclMailSync#10](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#10)]
940949
[!code-csharp[NclMailSync#10](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#10)]
950+
[!code-vb[NclMailSync#10](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#10)]
941951
942952
]]></format>
943953
</remarks>
@@ -1040,6 +1050,7 @@
10401050
10411051
[!code-cpp[NclMailSync#24](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#24)]
10421052
[!code-csharp[NclMailSync#24](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#24)]
1053+
[!code-vb[NclMailSync#24](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#24)]
10431054
10441055
]]></format>
10451056
</remarks>
@@ -1362,6 +1373,7 @@
13621373
13631374
[!code-cpp[NclMailSync#2](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#2)]
13641375
[!code-csharp[NclMailSync#2](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#2)]
1376+
[!code-vb[NclMailSync#2](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#2)]
13651377
13661378
]]></format>
13671379
</remarks>
@@ -1468,6 +1480,7 @@
14681480
14691481
[!code-cpp[NclMailSync#7](~/samples/snippets/cpp/VS_Snippets_Remoting/NCLMailSync/CPP/NclMailSync.cpp#7)]
14701482
[!code-csharp[NclMailSync#7](~/samples/snippets/csharp/VS_Snippets_Remoting/NCLMailSync/CS/mail.cs#7)]
1483+
[!code-vb[NclMailSync#7](~/samples/snippets/visualbasic/VS_Snippets_Remoting/NCLMailSync/vb/mail.vb#7)]
14711484
14721485
]]></format>
14731486
</remarks>

0 commit comments

Comments
 (0)