CDOSys Mail Script Example

CDOSys Mail Script Example

Here is a working example of -----------====CDOSYS====---------- without the opening ASP tags:
  1. Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
  2. Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 
  3. Const cdoAnonymous = 0 'Do not authenticate 
  4. Const cdoBasic = 1 'basic (clear-text) authentication 
  5. Const cdoNTLM = 2 'NTLM 
  6. Set objMessage = Server.CreateObject("CDO.Message") 
  7. objMessage.Subject = "Example CDO Message" 
  8. objMessage.Sender = "me@mydomain.com" 
  9. objMessage.From = "me@mydomain.com" 
  10. objMessage.To = "recipient@test.com" 
  11. objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication." 

  12. 'This section provides the configuration information for the remote SMTP server. 
  13. objMessage.Configuration.Fields.Item _ 
  14. ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

  15. 'Name or IP of Remote SMTP Server 
  16. objMessage.Configuration.Fields.Item _ 
  17. ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com" 

  18. 'Type of authentication, NONE, Basic (Base64 encoded), NTLM 
  19. objMessage.Configuration.Fields.Item _ 
  20. ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic 

  21. 'Your UserID on the SMTP server 
  22. objMessage.Configuration.Fields.Item _ 
  23. ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "you@your.com" 

  24. 'Your password on the SMTP server 
  25. objMessage.Configuration.Fields.Item _ 
  26. ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password_of_you@your.com" 

  27. 'Server port (typically 25) 
  28. objMessage.Configuration.Fields.Item _ 
  29. ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587 

  30. 'Use SSL for the connection (False or True) 
  31. objMessage.Configuration.Fields.Item _ 
  32. ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 

  33. 'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server) 
  34. objMessage.Configuration.Fields.Item _ 
  35. ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 
  36. objMessage.Configuration.Fields.Update 
  37. objMessage.Send 

Here is a working example of -----------====CDOSYS (with simple asp.net) or System.Net.Mail in C# ====---------- without the opening ASP tags: 
  1. //using System.Net.Mail; 
  2. public string SendEmail(string in_from, string in_to, string in_subject, string in_body, byte[] attachmentBytes, string attachmentName) 
  3. string errorMessage; 

  4. try 
  5. //create the mail message 
  6. MailMessage mail = new MailMessage(); 
  7. SmtpClient smtp = new SmtpClient("mail.DOMAINNAME.com"); 
  8. System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(in_from, "in_froms_PASSWORD"); 
  9. smtp.UseDefaultCredentials = false; 
  10. smtp.Credentials = SMTPUserInfo; 

  11. //set the addresses 
  12. mail.From = new MailAddress(in_from); 
  13. mail.To.Add(in_to); 

  14. //set the content 
  15. mail.Subject = in_subject; 
  16. mail.Body = in_body; 
  17. mail.IsBodyHtml = true; 

  18. //set the attachment 
  19. if (attachmentBytes != null) 
  20. MemoryStream ms = new MemoryStream(attachmentBytes); 
  21. mail.Attachments.Add(new Attachment(ms, attachmentName)); 

  22. //send the message 
  23. smtp.Send(mail); 

  24. errorMessage = ""; 

  25. catch (Exception err) 
  26. errorMessage = "An error has occurred in sending the email. Please contact support.<br />" + err.Message.ToString(); 

  27. return errorMessage;