ASPでCDOSYSを使用してメールを送信する方法
CDOSYSを使用してASPで電子メールを送信する手順は次のとおりです。
- CDOSYSオブジェクトをASPページに取り込む:
<%
Set objEmail = Server.CreateObject("CDO.Message")
%>
- メールの設定を指定する。送信者、宛先、件名、文章など。
<%
objEmail.From = "sender@example.com"
objEmail.To = "recipient@example.com"
objEmail.Subject = "Sample Email"
objEmail.HTMLBody = "Hello, this is a sample email!"
%>
- SMTPサーバー的相关設定
<%
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objEmail.Configuration.Fields.Update
%>
SMTPサーバーアドレス、ポート番号、認証情報などを、実際に使用している値に置き換えてください。
- メールを送る:
<%
objEmail.Send
%>
完全なサンプルコードは次のとおりです。
<%
Set objEmail = Server.CreateObject("CDO.Message")
objEmail.From = "sender@example.com"
objEmail.To = "recipient@example.com"
objEmail.Subject = "Sample Email"
objEmail.HTMLBody = "Hello, this is a sample email!"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objEmail.Configuration.Fields.Update
objEmail.Send
%>
上記のコード内の SMTP サーバは認証情報を必要としていることにご注意ください。[username] と [password] にはご自身の SMTP サーバのユーザー名とパスワードを置き換えてください。さらに、CDOSYS コンポーネントがサーバ上に正しくインストールされており、構成されていることを確認してください。