|
| 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 |
0 commit comments