Knowledgebase: ASP .NET / ASP
Send a mail from .NET
Posted by - NA - on 07 July 2010 09:30 PM
Related documents:
  1. CDOSYS Parameters that work for sending mail
As an Arvixe customer and a our forum user, janwillemb was kind enough to provide everyone with this piece of code to send mail, using SMTP Authentication (the only way to send emails out from our servers in order to reduce spam). Thank you!

To send an email from .NET, use this code:

      Dim msg As New MailMessage("[email protected]", "<emailaddress in your domain>")
      msg.Subject = "Subject here"
      msg.Body = "Body here"

      Dim client As New SmtpClient("localhost")
      client.Credentials = New Net.NetworkCredential("<email address in your domain>", "<password of this emailaccount>")
(26 vote(s))
Not helpful

Comments (18)
24 September 2010 04:44 PM
Here is the C# Version

System.Net.Mail.MailMessage eMail = new System.Net.Mail.MailMessage();
eMail.IsBodyHtml = true;
eMail.Body = body;
eMail.From = new System.Net.Mail.MailAddress(fromEmail);
eMail.Subject = subject;
System.Net.Mail.SmtpClient SMTP = new System.Net.Mail.SmtpClient();

SMTP.Credentials = new System.Net.NetworkCredential("user","pass");
SMTP.Host = "localhost";
Alex Ford
21 June 2011 12:30 PM
How do I use this code? I am currently using Google Apps for my email. What do I need to do to get this setup? I've tried a bunch of different things, including adding a user to my arvixe email accounts.
Arvand Sabetian
24 June 2011 04:39 AM
You would replace localhost with the SMTP server for gmail. Email address and password would be your gmail credentials.
Bryan Swan
01 July 2011 05:50 PM
I see in my Webmail settings the Incoming/Outgoing host name is localhost. Shouldn't this be along the lines of smtp.<domainname>?

I tried this code for C# and I get an Unable to connect to the remote server exception. Do i replace localhost with an smtp server? I set up my SMTP username and password in my webmail.
Arvand Sabetian
05 July 2011 09:59 AM
It is only localhost if the application is sitting on the same server as the IMAP service. If you are connecting externally you would need .
Dusan Janosik
27 March 2012 06:02 AM
Is it possible to use PickupDirectoryFromIis delivery method?
Michael Carr
05 April 2012 06:34 PM
Our windows servers are not using the SMTP service from IIS so I do not believe the PickupDirectoryFromIis method will function properly. Instead our windows servers use hMail.
07 August 2012 07:07 AM
Credentials username should be the full email address, not just the user:
SMTP.Credentials = new System.Net.NetworkCredential("user@domain","pass");

SMTP.Credentials = new System.Net.NetworkCredential("user","pass");
13 October 2012 06:34 AM
Or you can setup the credentials / send host in your web.config file:

<smtp from="[email protected]">
<network host="" userName="[email protected]" password="yourpassword" />
15 January 2013 05:26 AM
Trying to get sending emails to work, however I cannot get the connection working properly.

I have the following at the moment:-

<smtp from="username">
<network host="" userName="username" password="password" />

This is not sending out emails at the moment

Any help?
Michael Carr
17 January 2013 08:23 AM
Your usernames should be full email addresses. Try that and if you still have trouble please open a support ticket and we will be glad to help.
29 May 2013 09:25 PM
I've gotten this working using Framework 4.0, but was curious about the use of a specific port, such as 465 for SSL?

When I've added the following code I always time out. Am I missing something?
('client' is my local SmtpClient object name, previously created and configured)
client.Port = 465;
client.EnableSsl = true;

Anyone have any thoughts about this?
Michael Carr
30 May 2013 09:24 AM
You can also try port 587. For local to local connections though, those that are occurring from the server to the servers SMTP service, an SSL encrypted session is not really needed.
08 November 2013 10:30 AM
Hi, I've a problen sending email.
In localhost works fine, but when I publish the web in the server, i receive this error:
mailbox unavailable. the server response was 5.7.3 requested action aborted user not authenticated
My function:
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

msg.To.Add("[email protected]");
string remitente = "[email protected]";
msg.From = new MailAddress(remitente, "OFERSALUD", System.Text.Encoding.UTF8);
msg.Subject = "Invitación a ofersalud";

msg.SubjectEncoding = System.Text.Encoding.UTF8;

msg.Body = "holaaaa";// emailHtml.ToString();

msg.BodyEncoding = System.Text.Encoding.UTF8;

SmtpClient client = new SmtpClient();

client.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxxxx");
client.Port = 25;
client.Host = "";
client.EnableSsl = true;
Michael Carr
20 November 2013 11:00 AM
The error reads as though the smtp server is rejecting your authentication. Hotmail accounts may not allow relaying from web servers. Normally these types of scripts are setup to relay from a local email account on the server. You also have SSL enabled but your connecting on port 25, the non-ssl port. I would try changing that to 587 or disabling SSL for testing.
stacy williams
15 November 2013 02:25 PM
i am trying to update my web.config file to reflect my new marketing class email host info. but have no idea where the code you guys are suggesting would go. of course i am not a programmer but can make minor changes. i can not afford to have a real programer update this for me. can anyone suggest how i would update this file? i seem to be missing half of the fields that all of you are using above........

<add key="MailServer" value="localhost" />
<add key="MailUser" value="[email protected]" />
<add key="MailPassword" value="-----------" />
Michael Carr
18 November 2013 12:41 PM
These are just your connection details for the mailer. So long as you setup the same email address and password on the new server, you should not have to change these.
Eric Yeoman
15 December 2014 02:51 AM
Just thought I'd post this as I had difficulties connecting mail. Setting hosts and ports does not seem to work. This was the code I finally got going.

MailMessage message = new MailMessage(FROM_EMAIL_ADDRESS, TO_EMAIL_ADDRESS, "Test Connection", "This is the body of the test email");

var smtpClient = new SmtpClient(YOUR_HOST);
smtpClient.Credentials = new NetworkCredential(USER_EMAIL_ADDRESS, PASSWORD);
Post a new comment
Full Name:
CAPTCHA Verification 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).