Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
3.00/5 (1 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





C#
public void SendMail()
   {

       Random rn = new Random();
       code = rn.Next(10, 100000);
       checkcode = code;

       // Gmail Address from where you send the mail
       var fromAddress = "its.manak@gmail.com";
       // 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.gmail.com";
           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);
   }



C#
protected void Button1_Click(object sender, EventArgs e)
   {

       try
       {

           //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 == "@hotmail.com")
           //    Sendmail2();
           //else
               SendMail();
           //send("9211975256","jahangir",msg,TextBox4.Text);
           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.Focus();
           Button3.Visible=true;
          // TextBox1.Text = code.ToString();

                   }
       catch (Exception ex)
       {
           Label10.Text = ex.Message;
       }

   }
Posted

1 solution

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

VB
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", "<sender@domain.com>")
netMail.From = new MailAddress("website", "<sender@domain.com>")
netMail.ReplyToList.Add(new MailAddress("website", "<reply@domain.com>"))
netMail.to(new MailAddress("website", "<to@domain.com>"))
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
mailClient.Send(netMail)
netMail.Dispose


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.

[Chris]

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.
 
Share this answer
 
Comments
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?

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

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
<pre>

Try

Catch ex as SMTPException

End Try
</pre>

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900