In Send .Net Runtime Error messages with Email article i used sample code for sending mail in .net 1.1 version. Then I realize that it does not work in upper versions. I found a new way to send mail. It works in .net 2.0 and upper versions. This function has standard definitions in it. You can improve this function by convert some variables to function parameters. For example message body, from user and to user. It will be more flexible.
System.Web.Mail.MailMessage class is obsolete in .net 2.0 and upper versions. You must use System.Net.Mail.MailMessage class.
1 Public Shared Function SendMail() As Boolean
2
3 Dim iMsg As Object
4 Dim iConf As Object
5 Dim Flds As Object
6 Dim strHTML As String
7 Dim strSmartHost As String
8 Dim blnStatus As Boolean = True
9
10 Const cdoSendUsingPort As Integer = 25
11 strSmartHost = "mail_host" 'modify
12
13 iMsg = CreateObject("CDO.Message")
14 iConf = CreateObject("CDO.Configuration")
15
16 Flds = iConf.Fields
17
18 ' set the CDOSYS configuration fields to use port 25 on the SMTP server
19
20 With Flds
21 .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
22 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
23 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSmartHost
24 .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
25 .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user" 'modify
26 .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "pass" 'modify
27 '.Update()
28 End With
29
30 ' build HTML for message body. 'modify
31 strHTML = ""
32 strHTML = strHTML & ""
33 strHTML = strHTML & ""
34 strHTML = strHTML & " This is the test HTML message body
" & DateTime.Now 35 strHTML = strHTML & ""
36 strHTML = strHTML & ""
37
38 ' apply the settings to the message
39 Try
40 With iMsg
41 .Configuration = iConf
42 .To = "to_user_mail_address" 'modify
43 .From = "from_user_mail_address" 'modify
44 .Subject = "This is a test CDOSYS message (Sent via Port 25)"
45 .HTMLBody = strHTML
46 .Send()
47 End With
48 Catch ex As Exception
49 blnStatus = False
50 End Try
51
52 ' cleanup of variables
53 iMsg = Nothing
54 iConf = Nothing
55 Flds = Nothing
56
57 Return blnStatus
58
59 End Function
0 comments:
Post a Comment