<<Back To Faq 4.3


Complete FAQ Listing


4.3 How do I change the SMTP port number? Printer Friendly

Changing the SMTP port (the port email will be sent to), is also much easier than it was in System.Web.Mail. In System.Net.Mail, you simply set the .Port property on the SmtpClient object.

Below is an example that changes the SMTP port to the lesser used port number 587.

[ C# ]

static void ChangePort()
{
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");

//to change the port (default is 25), we set the port property
smtp.Port = 587;
smtp.Send(mail);
}



[ VB.NET ]
Sub ChangePort()
'create the mail message
Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")

'set the content
mail.Subject = "This is an email"
mail.Body = "this is the body content of the email."

'send the message
Dim smtp As New SmtpClient("127.0.0.1")

'to change the port (default is 25), we set the port property
smtp.Port = 587
smtp.Send(mail)
End Sub 'ChangePort



 



The formatted version of this faq can be found at http://www.SystemNetMail.com/faq/4.3.aspx