Sending mail through a remote SMTP server

deadseasquirrels

Board Regular
Joined
Dec 30, 2004
Messages
232
I've been trying to send emails through an SMTP server in various ways. I've been able to send email using CDO going directly through an SMTP server on my same local network (not requiring a username and password). But now I am trying to send email from my home with the SMTP server I am using that is hosted by some commercial company. I used the code below, which is sponsored by Paul Sadowski:
Code:
Sub sendMailRemote()
    'Sending a text email using authentication against a  remote SMTP server

    Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
    Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
    Const cdoAnonymous = 0 'Do not authenticate
    Const cdoBasic = 1 'basic (clear-text) authentication
    Const cdoNTLM = 2 'NTLM
    
    Dim objMessage As Object

    Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = "Example CDO Message"
    objMessage.Sender = """Me"" <me@my.com>"
    objMessage.To = "me@hotmail.com"
    objMessage.TextBody = "This is some sample message text.." & vbCrLf & "It was sent using SMTP authentication."

    '==This section provides the configuration information for the remote SMTP server.

    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.myserver.com"

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

    'Your UserID on the SMTP server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "deadesasquirrels"

    'Your password on the SMTP server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "squirrels"

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

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

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

    objMessage.Configuration.Fields.Update

    '==End remote SMTP server configuration section==

    objMessage.Send

End Sub

So this code doesn't work. It says "Transport failed to connect to server" which is pretty general. But the thing is I do use the SMTP server from outlook, so port 25 is not blocked, I'm positive I am using the correct username and password (again, because outlook works). Also I know the SMTP server requires authentication...so I'm not sure what else is wrong. If Mr. Sadowski is on this forum maybe you can reply as well. Anybody have any experience with this?
 
Hello,

I've been trying to send mail using the gmail smtp server but I didn't succeed. I still kep getting the following error: "Transport failed to connect to server"

Does anybody know a solution ? Any help is much apprecitaed !

Here is my code:

public string sendMail (string from, string to, string cc, string bcc, string subject, string body) {

// Mail initialization MailMessage mail = new MailMessage();
mail.From = from;
mail.To = to;
mail.Cc = cc;
mail.Bcc = bcc;
mail.Subject = subject;
mail.BodyFormat = MailFormat.Text;
mail.Body = body;

// Smtp configuration
SmtpMail.SmtpServer = "smtp.gmail.com";

// - smtp.gmail.com use smtp authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myemail@gmail.com");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");

// - smtp.gmail.com use port 465 or 587
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);

// - smtp.gmail.com use STARTTLS (some clients call this SSL)
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);

// Mail sending
try {
SmtpMail.Send(mail);
return "";

} catch (Exception ex) {
return ex.Message;
}
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Sorry... I havent done mine like this... So have no idea but I had two secure connection settings that I had to do to get mine to work... I unfortunately dont have the copy of mine with me... But Ill post it on here if you want.
 
Upvote 0
I would really appreciate it if you could post your working solution. I'm sure the solution for my problem is something simple but I can't find out what.

My example uses the System.Web.Mail namespace in .NET but under the hood that's the same as CDO so the configuration settings will be similar I hope.
 
Upvote 0
Hey, I have lost the file for whatever reason...
Ill keep looking and let you know.
Perhaps Stefu can post his solution.
 
Upvote 0
Thanks a lot for all the work you did so far.

I'm building a website where people must register to use it, in a way you could compare it to this forum. When somebody registers he or she recieves his password in his mailbox. Therefore I need to be able to send e-mail.

I could set up my own SMTP server (which I already did on the development environment to try) but I rather use a gmailaccount for it. Then I don't have to worry about mail security issue's etc.

For some reason all other smtp servers that I've tried work fine (my provider smtp, the one I installed on the development environment) I just can't get gmail working ! :(

I wonder if google disabled it on purpose to prevent spammers from using their gmail service?
 
Upvote 0
And Stefu is back from vacation ;)
Here is a piece of code which worked in my case:

MailMessage mail = new MailMessage();

mail.From = "user@gmail.com";
mail.To = "some@thing"';

mail.Subject = "gmail";
mail.BodyFormat = MailFormat.Text;
mail.Body = "Hello";
SmtpMail.SmtpServer = "smtp.gmail.com";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "username");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");


SmtpMail.Send(mail);
 
Upvote 0
Hehehehehe Hmmm... the code does basically the same as the code I had. I tried it and it works now, I also tried the code that didn't work before and it also works now..... very weird.... Maybe the gmail smtp server was down for whatever reason last time I tried. This could explain the error message: "Transport failed to connect to server". I'll have to add some special errorhandling here I guess.

But anyway thanks for all the help you guys gave me ! I have a working solution now and thats what counts :biggrin: :biggrin:
 
Upvote 0
Ok.... Me again, sorry

I was just browsing and noticed that your version has a 'mail.from'
Does that allow for a reply to address?
Because I cant get yours to work...

It gets struck right from :
MailMessage mail = new MailMessage();

Below is the code that Im currently using (All those looking here, this WORKS for gmail) but am wanting a reply to address...
Ive tried changing this - objMessage.Sender = "replyto@gmail.com"
But it doesnt work.

Any ideas how I can get the emails that are sent to have a different reply to address? (its not SPAM I promise...) I want to send from one account and receive to another.

---------------------------------------------------------

'Sending a text email using authentication against a remote SMTP server

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Dim objMessage As Object

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "SUBJECT" 'EmailMsg.Subject
objMessage.Sender = "replyto@gmail.com"
objMessage.To = "to@gmail.com" ' EmailMsg.Email
objMessage.TextBody = "BODY" ' EmailMsg.Body

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

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

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user@gmail.com"

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

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

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

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

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send
 
Upvote 0

Forum statistics

Threads
1,215,067
Messages
6,122,949
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top