I have a VBScript file to send the emails It works fine, but when I created a C# program (.Net 4.5) with the same CDO settings I got
The transport failed to connect to the server.
Please check what is the problem with my C#.
VBScript:
Dim objMessage
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = Subj
objMessage.From = Sender
objMessage.To = Receivers
objMessage.TextBody = Body
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ServerName
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 2
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = ""
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = ""
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
objMessage.Configuration.Fields.Update
objMessage.Send
C#:
using System.Net.Mail;
...
CDO.Message objMessage = new CDO.Message();
objMessage.Subject = Subj;
objMessage.From = Sender;
objMessage.To = Receivers;
objMessage.TextBody = Body;
ADODB.Field FieldSMTPSendUsing = objMessage.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
FieldSMTPSendUsing.Value = 2;
ADODB.Field FieldSMTPServer = objMessage.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
FieldSMTPServer.Value = ServerName;
ADODB.Field FieldSMTPServerPort = objMessage.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
FieldSMTPServerPort.Value = 25;
ADODB.Field FieldSMTPconnectiontimeout = objMessage.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"];
FieldSMTPconnectiontimeout.Value = 60 ;
ADODB.Field FieldSMTPauthenticate = objMessage.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];
FieldSMTPauthenticate.Value = 2;
ADODB.Field FieldSMTPsendusername = objMessage.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
FieldSMTPsendusername.Value = "";
ADODB.Field FieldSMTPsendpassword = objMessage.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
FieldSMTPsendpassword.Value = "";
ADODB.Field FieldSMTPusessl = objMessage.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
FieldSMTPusessl.Value = false;
objMessage.Configuration.Fields.Update();
objMessage.Send();
Don't use CDO in C#. Use the MailMessage
class for composing messages, and the SmtpClient
class for sending them.
MailMessage message = new MailMessage(Sender, Receivers, Subj, Body);
SmtpClient client = new SmtpClient(ServerName);
try {
client.Send(message);
} catch (Exception ex) {
Console.WriteLine("Cannot send message.");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.