Click here to Skip to main content
13,147,044 members (63,074 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Email sent through localhost but not sent when I try to send from my Server ... What to Do.. Please Help

i have used this code

public void SendMail()
       Random rn = new Random();
       code = rn.Next(10, 100000);
       checkcode = code;
       // Gmail Address from where you send the mail
       var fromAddress = "";
       // any address where the email will be sending
       var toAddress = email.Text.ToString();
       //Password of your gmail address
       const string fromPassword = "itsmanak";
       // Passing the values and make a email formate to display
       string subject = "Verification code for lodging complaint".ToString();
       string body = "From: " + email.Text + "\n";
       body += "Subject: " + subject + "\n";
       body += "Your Code is: " + code.ToString() + "\n";
       // smtp settings
       var smtp = new System.Net.Mail.SmtpClient();
           smtp.Host = "";
           smtp.Port = 587;
           smtp.EnableSsl = true;
           smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
           //smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
           smtp.Credentials = new System.Net.NetworkCredential(fromAddress, fromPassword);
           smtp.Timeout = 20000;
       // Passing values to smtp object
       smtp.Send(fromAddress, toAddress, subject, body);

protected void Button1_Click(object sender, EventArgs e)
           //string ext = email.Text;
           //int len = ext.Length;
           //for (int i = 0; i <= ext.Length; i++)
           //    if (ext[i] == '@')
           //        newext = ext.Substring(i, len);
           //if (newext == "")
           //    Sendmail2();
           string msg = "Your Code is " + checkcode.ToString();
           string num = "9211975256";
           string pas = "jahangir";
          // send(num, pas, msg, phone_no.Text);
           ShowMessage("Check your email account for the code");
           TextBox1.Enabled = true;
          // TextBox1.Text = code.ToString();
       catch (Exception ex)
           Label10.Text = ex.Message;
Posted 22-Nov-12 18:30pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your code is quite crude or sending email using
You have to make 4 objects to send a message in the order I put them in below.

Dim netMail As New MailMessage
Dim mailClient As New SmtpClient
Dim smtp_credentials As Net.NetworkCredential
Dim mailException As New SmtpException

netMail is the Sender/Body stuff
mailClient is for the credentials
smtp_credentials is for connecting to the mail server
mailException is for diagnostics, figure out whats wrong

netMail.Sender = new MailAddress("website", "<>")
netMail.From = new MailAddress("website", "<>")
netMail.ReplyToList.Add(new MailAddress("website", "<>")) MailAddress("website", "<>"))
netMail.Subject = "blah"
netmail.Body = "text"
mailClient.UseDefaultCredentials = False
smtp_credentials = New Net.NetworkCredential
smtp_credentials.Domain = "DomainName"
smtp_credentials.UserName = "LoginID"
smtp_credentials.Password = "Password"
mailClient.Credentials = smtp_credentials
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network
mailClient.Host = "ServerAddress"
mailClient.Port = "SMTP_PortNum"

The mailClient sends the netmail, which is everthing needed to transmit

Your going to have to play around with the credentials. The above sends to a virvirtual SMTP server

You have to wrap the email address in brackets, for other servers to pick it up

Sending email is tricky, and takes alot of testing to make sure it works for all email addresses.


I expanded the edit box, and the overflow disappeared on firefox. so I lost focus on the text to the right and can't get it back.
aiskrishan 26-Nov-12 0:58am
it is showing an error
"The specified String is not a valid email address."

Even i wrote correct address and place your given code
jkirkerx 26-Nov-12 13:05pm
The code I posted is just a broad foundation for how it works, it does not work.
Did you format your mailaddress like the example below?

= new mailaddress("Website Name", "<" & emailAddress & ">")

I would of posted a working example, but my current code is too complicated to follow and understand.

Did you wrap your code in so you can trap the SMTP Error


Catch ex as SMTPException

End Try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100